forked from KovaletsIvan/js-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (106 loc) · 2.19 KB
/
index.js
File metadata and controls
143 lines (106 loc) · 2.19 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// 10
const count = (obj) => Object.keys(obj).length;
// 9
function filter(arr, fn) {
return arr.slice().filter((elem) => fn(elem));
}
// 8
function pluck(objects, fieldName) {
return objects.map((elem) => elem[fieldName]);
}
// 7
var ctx = { x: 7 };
function testThis(a) {
console.log("x=" + this.x + ", a=" + a);
}
const bind = (fn, context) => {
return fn.bind(context);
};
const bondFn = bind(testThis, ctx);
bondFn(500);
// 6
const test = (a, b, c, s) => {
return "a=" + a + ",b=" + b + ",c=" + c;
};
const partialAny = (fn, ...args) => {
return function (d) {
const undefElem = args.map((elem) => {
return elem === undefined ? d : elem;
});
return fn(...undefElem);
};
};
const test1 = partialAny(test, 1, undefined, 3);
console.log(test1(5));
// 5
function add(a, b) {
return a + b;
}
function mult(a, b, c, d) {
return a * b * c * d;
}
const partial = (fn, ...n) => {
return function (...args) {
const arr = n.concat(args);
return fn(...arr);
};
};
const add5 = partial(add, 5);
const mult1 = partial(mult, 2, 3);
console.log(add5(3));
console.log(mult1(4, 5));
4
function square(x) {
return x * x;
}
function add(a, b) {
return a + b;
}
const fmap = (a, gen) => {
return function (...args) {
const y = gen(...args);
return a(y);
};
};
const squareAdd1 = fmap(square, add);
console.log(squareAdd1(5, 7));
3
function square(x) {
return x * x;
}
function map(fn, array) {
return array.map((elem) => fn(elem));
}
console.log(map(square, [1, 2, 3, 4]));
2
const newArr = [];
function take(fn, count) {
newArr.push(fn());
return count > 1 ? take(fn, count - 1) : newArr;
}
// const take = (fn, count) => {
// const newArr = [];
// let result = fn();
// for (let i = 0; i < count; i++) {
// result = fn();
// newArr.push(result);
// }
// return newArr;
// };
const addNum = (a) => {
return function () {
return (a += 1);
};
};
const add1 = addNum(3);
console.log(take(add1, 5));
// 1
function sequence(start = 0, step = 1) {
return function () {
return (start += step);
};
}
const generator1 = sequence(5, 2);
console.log(generator1());
console.log(generator1());
console.log(generator1());