-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpropertiesArgumentsCurrying_4.js
74 lines (49 loc) · 1.44 KB
/
propertiesArgumentsCurrying_4.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
const {curry} = require('ramda')
// properties
// associative
// add(add(a, y), z) == add(a, add(y, z));
// commutative
// add(x, y) == add(y, x)
// identity
// add(x, 0) == x
// distributive
// add(multiply(x, y), multiply(x, z)) == multiply(x, add(y, z))
function add (a, b) {
return a + b
}
function multiply (a, b) {
return a * b
}
//
add([1, 2]) == add(1, 2);
const toPair = f =>
([x, y]) => f(x, y);
const fromPair = f =>
(x, y) => f([x, y]);
const flip = f =>
(y, x) => f(x, y);
// const curry = f =>x => y => f(x, y)
const unCurry = f =>(x, y) => f(x)(y);
const modulo = curry((x,y) => y % x);
const result= toPair(add)([1, 2]);
const result1= fromPair(toPair(add))(1, 2);
const result2 = flip(add)(1, 3);
const isOdd = modulo(2); // (2, y) => 2 % y
const confirmOdd = isOdd(3);
console.log(confirmOdd);
const curriedAdd = curry(add);
const increment = curriedAdd(1);
const result3 = increment(2);
console.log(result);
console.log(result1);
console.log(result2);
console.log(result3);
const filter = curry((f, xs) => xs.filter(f));
const getOdds = filter(isOdd);
const oddResult = getOdds([1, 2, 3, 4, 5, 6, 7]);
console.log(oddResult);
const replace = curry((regex, replacement, str) => str.replace(regex, replacement));
const replaceVowels = replace(/[AEUIO]/ig, '!'
);
const replaceRes = replaceVowels("Hey I have words");
console.log(replaceRes);