-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfunctors3.js
33 lines (28 loc) · 844 Bytes
/
functors3.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
let stringFunctor = (value, fn) => {
// value is 'ABC'
// value is 'YXZ'
let chars = value.split('')
// now the strings are an array ['A', 'B', 'C'] || [ 'Y', 'X', 'Z' ]
// .map takes each character 'A' || 'Y'
return chars.map(char => {
// 'A'.charCodeAt(0) /// 65
// 'A'.charCodeAt(0) /// 89
// fn(65) /// plus1(65) /// 66
// fn(89) /// minus1(89) /// 88
// String.fromCharCode(66) /// 'B'
// String.fromCharCode(88) /// 'Y'
return String.fromCharCode(fn(char.charCodeAt(0)))
}).join('')
// 'BCD'
// 'XW'
}
let plus1 = value => {
return value + 1
}
let minus1 = value => {
return value - 1
}
let a = [3, 4].map(plus1) // returns [4, 5]
let b = stringFunctor('ABC', plus1) // returns 'BCD'
let c = stringFunctor('XYZ', minus1) // returns 'WXY' /// example says 'RXY'?
console.log(a, b, c)