-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathexamples.js
36 lines (28 loc) · 1.22 KB
/
examples.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
/**
Códigos de exemplo do mestre Halan Pinheiro https://github.com/halan
https://gist.github.com/halan/c3c0ec1142b8d1bbf242939c238fbcab
*/
const reduce = (reducer, initial, [head, ...tail]) =>
head // condition to go or stop
? reduce(reducer, reducer(initial, head), tail) // recursion
: initial // stop
const map = (mapper, [head, ...tail]) =>
head // condition to go or stop
? [ mapper(head), ...map(mapper, tail) ] //recursion
: [] // stop
const filter = (predicate, [head, ...tail]) =>
head // condition to go or stop
? [ ...(predicate(head) ? [head] : []), ...filter(predicate, tail) ] // recursion
: [] // stop
const zip = ([head, ...tail], [head2, ...tail2]) =>
head // condition to go or stop
? [ [head, head2], ...zip(tail, tail2)] // recursion
: [] // stop
const find = (predicate, [head, ...tail]) =>
head // condition to go or stop
? predicate(head) ? head : find(predicate, tail) // recursion
: undefined // stop
const sort = ([head, ...tail]) => // quick sort (https://pt.wikipedia.org/wiki/Quicksort)
head // condition to go or stop
? [...sort(filter(n => n <= head, tail)), head, ...sort(filter(n => n > head, tail))] // recursion (depends of filter)
: [] // stop