-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_implementation.js
More file actions
39 lines (26 loc) · 1.32 KB
/
final_implementation.js
File metadata and controls
39 lines (26 loc) · 1.32 KB
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
/* es6 refactor of fourthImplementation*/
const truthy = (t, f) => t()
const falsey = (t, f) => f()
const ifelse = (cond, then, el) => cond(then, el)
const nil = _ => {}
const empty = selector => selector(nil, nil, truthy)
const isEmpty = list => list((h, t, e) => e)
const prepend = (head, tail) => (selector) => selector(head, tail, falsey)
const head = list => list((head, tail) => head)
const tail = list => list((head, tail) => tail)
/***** Create List Helper ******/
const createList = (...args) => args.reverse().reduce((acc, el) => prepend(el, acc), empty)
/*******************************/
/***** More List Helper Functions ******/
const length = list => ifelse(isEmpty(list),
_ => 0,
_ => 1 + length(tail(list)))
const map = (f, list) => ifelse(isEmpty(list),
_ => empty,
_ => prepend(f(head(list)), map(f, tail(list))))
const filter = (f, list) => ifelse(isEmpty(list),
_ => empty,
_ => ifelse(f(head(list)),
_ => prepend(head(list), filter(f, tail(list))),
_ => filter(f, tail(list))))
/**************************************/