@tesla809/

functor - fp - Fun Fun Function

JavaScript

Notes on functors from Fun Fun Function's series on functional programing.

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// What are functors?

// from fun fun function
// functors - Part 10 of Functional Programming in JavaScript
// https://www.youtube.com/watch?v=YLIH8TKbAh4
// no video 9 in series

// functional programming - the process of building software by composing pure functions, avoiding shared state, mutable data, and side-effects. You can avoid lots of bugs this way.

// functors- 

// in a mathematical context, its waaay more abstract.
// They are from a branch of category theory.

// examples are array.map, array.filter.
// array.forEach() is not a functor, it doesn't return anything in a structured form.

// array.map and array.filter are functors
function plus1(value){
  return value + 1;
}

console.log(plus1(4)) // 3

// Ideally, we would like to get back [4,5]
console.log(plus1([3, 4])) // logic error- 3,41

// ok, so lets modify our function then
function plus1v2(value){
  if (Array.isArray(value)) {
    let newArray = value.map(x => x + 1);
    return newArray;
  }
  return value + 1;
}

console.log(plus1v2([3, 4])) // [4,5]

// how about if we need to modify strings too
console.log(plus1('ABC')) // should be BCD

// ok, so lets modify our function again
// its sort of annoying that the function does
// way more than just adding up.
// Its big but it works
function plus1v3(value){
  if (Array.isArray(value)) {
    let newArray = []
    for(let i = 0; i < value.length; i++){
      newArray[i] = value[i] + 1
    }
    return newArray;
  }

  // increments the chars
  if(typeof value === 'string') {
    let chars = value.split('');
    let newCharArray = [];
    for(let i = 0; i < chars.length; i++){
      newCharArray[i] = String.fromCharCode(
        chars[i].charCodeAt(0) + 1
      )
    }
    return newCharArray.join('');
  }
  return value + 1;
}

console.log(plus1v3('ABC')) // BCD

// Shit, now we have to write a minus1 function
// Now we will have to duplicate the whole thing and replace with -1.

// Can we generalize the upper part of function?
// The string and array parts of function?
// It doesn't feel like it belongs in the plus function anyway.

// JS has a build in functor already. Its called .map()
// we can keep it simple

// It should make things easier
console.log([3,4].map(plus1)) // [4,5] What!

// What about plusing strings
// Lets make a string functor
// fn is the functor
// its like a variable for a function we can 
// swap out for modular functionlity
function stringFunctor(value, fn) {
  var chars = value.split('');
  return chars.map(function(char){
    return String.fromCharCode(fn(char.charCodeAt(0)))
  }).join('')
}

function plus1(value) {
  return value + 1;
}

function minus1(value) {
  return value - 1; 
}

console.log(stringFunctor('ABC', plus1));  // BCD
console.log(stringFunctor('XYZ', minus1)); // WYZ
Native Browser JavaScript