forked from hacktoberfest17/programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
108 lines (87 loc) · 2.03 KB
/
index.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
107
108
// identity functor
const Box = x =>
({
map: f => Box(f(x)),
fold: f => f(x),
inspect: () => `Box(${x})`
})
//useage
const nextCharForNumberString = str =>
Box(str)
.map(s => s.trim())
.map(r => parseInt(r))
.map(i => i + 1)
.map(i => String.fromCharCode(i))
.fold(c => c.toLowerCase())
const basicBox = nextCharForNumberString(' 64 ')
console.log(basicBox)
/***********************/
// enforce null checks with code spliting
const Right = x =>
({
map: f => Right(f(x)),
fold: (f, g) => g(x),
inspect: () => `Right(${x})`
})
const Left = x =>
({
map: f => Left(x),
fold: (f, g) => f(x),
inspect: () => `Left(${x})`
})
const fromNullable = x =>
x != null ? Right(x) : Left(null)
const findColor = name =>
fromNullable({red: '#ff4444', blue: '#3b5998', yellow: '#fff68f'}[name])
const codeSplit = findColor('blue')
.map(c => c.slice(1))
.map(c => c.toUpperCase())
.fold(e => 'no color', x => x)
console.log(codeSplit)
/************************/
// Semi-Groups
const Sum = x =>
({
x,
concat: ({x: y}) =>
Sum(x + y),
inspect: () =>
`Sum(${x})`,
toString: () =>
`Sum(${x})`
})
//const res = Sum(1).concat(Sum(2))
const All = x =>
({
x,
concat: ({x: y}) =>
All(x && y),
inspect: () =>
`All(${x})`,
toString: () =>
`All(${x})`
})
//const res = All(false).concat(All(true))
const First = x =>
({
x,
concat: _ =>
First(x),
inspect: () =>
`First(${x})`,
toString: () =>
`First(${x})`
})
const semiGroups = First("blah").concat(First("ice cream")).concat(First('meta programming'))
console.log(semiGroups)
/****************************************/
// failsafe combination using monoids
Sum.empty = () => Sum(0)
All.empty = () => All(true)
const sum = xs =>
xs.reduce((acc, x) => acc + x, 0)
const all = xs =>
xs.reduce((acc, x) => acc && x, true)
const first = xs =>
xs.reduce((acc, x) => acc)
console.log(first([1,2,3]))