-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.js
2020 lines (1728 loc) · 33.9 KB
/
functions.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
Write a function `identity` that
takes an argument and returns
that argument
@example
identity(3) // 3
@param {any} x
@return {any}
*/
function identity(x) {
return x;
}
/**
Write a binary function `addb`
that takes two numbers and returns
their sum
@example
addb(3, 4) // 3 + 4 = 7
@param {number} a
@param {number} b
@return {number}
*/
function addb(a, b) {
return a + b;
}
/**
Write a binary function `subb`
that takes two numbers and returns
their difference
@example
subb(3, 4) // 3 - 4 = -1
@param {number} a
@param {number} b
@return {number}
*/
function subb(a, b) {
return a - b;
}
/**
Write a binary function `mulb`
that takes two numbers and returns
their product
@example
mulb(3, 4) // 3 * 4 = -1
@param {number} a
@param {number} b
@return {number}
*/
function mulb(a, b) {
return a * b;
}
/**
Write a binary function `minb`
that takes two numbers and returns
the smaller one
@example
minb(3, 4) // 3
@param {number} a
@param {number} b
@return {number}
*/
function minb(a, b) {
return a < b ? a : b;
}
/**
Write a binary function `maxb`
that takes two numbers and returns
the larger one
@example
maxb(3, 4) // 4
@param {number} a
@param {number} b
@return {number}
*/
function maxb(a, b) {
return a > b ? a : b;
}
/**
Write a function `add` that
is generalized for any
amount of arguments
@example
add(1, 2, 4) // 1 + 2 + 4 = 7
@param {...number} nums
@return {number}
*/
function add(...nums) {
return nums.reduce((total, curr) => {
return total + curr;
}, 0);
}
/**
Write a function `sub` that
is generalized for any
amount of arguments
@example
sub(1, 2, 4) // 1 - 2 - 4 = -5
@param {...number} nums
@return {number}
*/
function sub(first, ...rest) {
return rest.reduce((total, curr) => {
return total - curr;
}, first);
}
/**
Write a function `mul` that
is generalized for any
amount of arguments
@example
mul(1, 2, 4) // 1 * 2 * 4 = 8
@param {...number} nums
@return {number}
*/
function mul(...nums) {
return nums.reduce((total, curr) => {
return total * curr;
}, 1);
}
/**
Write a function `min` that
is generalized for any
amount of arguments
@example
min(1, 2, 4) // 1
@param {...number} nums
@return {number}
*/
function min(...nums) {
return nums.reduce((result, num) => {
return minb(result, num);
}, Number.MAX_VALUE);
}
/**
Write a function `max` that
is generalized for any
amount of arguments
@example
max(1, 2, 4) // 4
@param {...number} nums
@return {number}
*/
function max(...nums) {
return nums.reduce((result, num) => {
return maxb(result, num);
}, Number.MIN_VALUE);
}
/**
Write a function `addRecurse` that
is the generalized `add` function
but uses recursion
@example
addRecurse(1, 2, 4) // 1 + 2 + 4 = 7
@param {...number} nums
@return {number}
*/
function addRecurse(...nums) {
if (nums.length < 1) {
return 0;
}
if (nums.length === 1) {
return nums[0];
}
return nums[0] + addRecurse(...nums.slice(1));
}
/**
Write a function `mulRecurse` that
is the generalized `mul` function
but uses recursion
@example
mulRecurse(1, 2, 4) // 1 * 2 * 4 = 8
@param {...number} nums
@return {number}
*/
function mulRecurse(...nums) {
if (nums.length < 1) {
return 1;
}
if (nums.length === 1) {
return nums[0];
}
return nums[0] * mulRecurse(...nums.slice(1));
}
/**
Write a function `minRecurse` that
is the generalized `min` function
but uses recursion
@example
minRecurse(1, 2, 4) // 1
@param {...number} nums
@return {number}
*/
function minRecurse(...nums) {
if (nums.length < 1) {
return Number.MAX_VALUE;
}
if (nums.length === 1) {
return nums[0];
}
return minb(nums[0], minRecurse(...nums.slice(1)));
}
/**
Write a function `maxRecurse` that
is the generalized `max` function
but uses recursion
@example
maxRecurse(1, 2, 4) // 4
@param {...number} nums
@return {number}
*/
function maxRecurse(...nums) {
if (nums.length < 1) {
return Number.MIN_VALUE;
}
if (nums.length === 1) {
return nums[0];
}
return maxb(nums[0], maxRecurse(...nums.slice(1)));
}
/**
Write a function `not` that
takes a function and returns
the negation of its result
@example
const isOdd = x => x % 2 === 1;
const isEven = not(isOdd);
isEven(1) // false
isEven(2) // true
@param {function} func
@return {function}
*/
function not(func) {
return function(...args) {
return !func(...args);
};
}
/**
Write a function `acc` that
takes a function and an
initial value and returns
a function that runs the
initial function on each
argument, accumulating the
result
@example
let add = acc(addb, 0);
add(1, 2, 4) // 7
let mul = acc(mulb, 1);
mul(1, 2, 4) // 8
@param {function} func
@param {any} initial
@return {function}
*/
function acc(func, initial) {
return function(...args) {
return args.reduce((result, curr, idx) => {
return func(result, curr, idx)
}, initial);
};
}
/**
Write a function `accPartial` that
takes in a function, a start index,
and an end index, and returns a
function that accumulates a subset
of its arguments by applying the
given function to all elements
between start and end.
@example
const addSecondToThird = accPartial(add, 1, 3);
addSecondToThird(1, 2, 4, 8) // [ 1, 6, 8 ]
@param {function} func
@param {number} start
@param {number} end
@return {function}
*/
function accPartial(func, start, end) {
return function(...nums) {
return [
...nums.slice(0, start),
func(...nums.slice(start, end)),
...nums.slice(end, nums.length)
];
};
}
/**
Write a function `accRecurse` that
does what `acc` does but uses recursion
@example
let add = accRecurse(addb, 0);
add(1, 2, 4) // 7
let mul = accRecurse(mulb, 1);
mul(1, 2, 4) // 8
@param {function} func
@param {number} initial
@return {function}
*/
function accRecurse(func, initial) {
return function recurse(...nums) {
if (nums.length < 1) {
return initial;
}
if (nums.length === 1) {
return nums[0];
}
return func(nums[0], recurse(...nums.slice(1)));
}
}
/**
Write a function `fill` that
takes a number and returns
an array with that many
numbers equal to the given
number
@example
fill(3) // [ 3, 3, 3 ]
@param {number} num
@return {array}
*/
function fill(num) {
return new Array(num).fill(num);
}
/**
Write a function `fillRecurse` that
does what `fill` does but uses recursion
@example
fillRecurse(3) // [ 3, 3, 3 ]
@param {number} num
@return {array}
*/
function fillRecurse(num) {
function recurse(num, times) {
if (times === 0) {
return [];
}
return [ num ].concat(recurse(num, times - 1));
}
return recurse(num, num);
}
/**
Write a function `set` that
is given a list of arguments
and returns an array with
all duplicates removed
@example
let oneAndTwo = set(1, 1, 1, 2, 2, 2); // [ 1, 2 ]
@param {...any} args
@return {array}
*/
function set(...args) {
function predicate(result, curr) {
if (!result.includes(curr)) {
result.push(curr);
}
return result;
}
return acc(predicate, [])(...args);
}
/**
Write a function `identityf`
that takes an argument and
returns a function that
returns that argument
@example
let three = identityf(3);
three(); // 3
@param {any} x
@return {function}
*/
function identityf(x) {
return function() {
return identity(x);
};
}
/**
Write a function `addf` that
adds from two invocations
@example
addf(3)(4) // 7
@param {number} a
@return {function}
*/
function addf(a) {
return function(b) {
return addb(a, b);
};
}
/**
Write a function `liftf` that
takes a binary function, and
makes it callable with two
invocations
@example
let addf = liftf(add);
addf(3)(4); // 7
liftf(mul)(5)(6) // 30
@param {function} binary
@return {function}
*/
function liftf(binary) {
return function(a) {
return function(b) {
return binary(a, b);
};
};
}
/**
Write a [pure](https://en.wikipedia.org/wiki/Pure_function) function `pure` that
is a wrapper arround the impure
function `impure`
<pre>function impure(x) {
y++;
z = x * y;
}
var y = 5, z;
impure(20);
z; // 120
impure(25);
z; // 175</pre>
@example
pure(20, 5) // [ 6, 120 ]
pure(25, 6) // [ 7, 175 ]
@param {number} x
@param {number} y
@return {array} an array containing `y` and `z`
*/
function pure(x, y) {
var z;
function impure() {
y++;
z = x * y;
}
impure();
return [ y, z ];
}
/**
Write a function `curryb` that
takes a binary function and
an argument, and returns a
function that can take a
second argument
@example
let add3 = curryb(add, 3);
add3(4); // 7
curry(mul, 5)(6); // 30
@param {function} binary
@param {any} a
@return {function}
*/
function curryb(binary, a) {
return liftf(binary)(a);
}
/**
Write a function `curry` that
is generalized for any amount
of arguments
@example
curry(add, 1, 2, 4)(4, 2, 1) = 1 + 2 + 4 + 4 + 2 + 1 = 14
curry(sub, 1, 2, 4)(4, 2, 1) = 1 - 2 - 4 - 4 - 2 - 1 = 0
curry(mul, 1, 2, 4)(4, 2, 1) = 1 * 2 * 4 * 4 * 2 * 1 = 64
@param {function} func
@param {...any} outer
@return {function}
*/
function curry(func, ...outer) {
return function(...inner) {
return func(...outer, ...inner);
};
}
/**
Without writting any new functions,
show multiple ways to create the `inc`
function
@example
inc(5) // 6
inc(inc(5)) // 7
@param {number} x
@return {number}
*/
function inc(x) {
return add(1, x);
}
function inc2(x) {
return addf(1)(x);
}
function inc3(x) {
return liftf(add)(x)(1);
}
function inc4(x) {
return curry(add, x)(1);
}
/**
Write a function `twiceUnary`
that takes a binary function
and returns a unary function
that passes its argument to
the binary function twice
@example
let doubl = twiceUnary(addb);
doubl(11) // 22
let square = twiceUnary(mulb);
square(11) // 121
@param {function} binary
@return {function}
*/
function twiceUnary(binary) {
return function(x) {
return binary(x, x);
};
}
/**
Use the function `twiceUnary` to
create the `doubl` function
@example
doubl(11) // 22
@param {number} x
@return {number}
*/
function doubl(x) {
return twiceUnary(addb)(x);
}
/**
Use the function `twiceUnary` to
create the `square` function
@example
square(11) // 121
@param {number} x
@return {number}
*/
function square(x) {
return twiceUnary(mulb)(x);
}
/**
Write a function `twice` that
is generalized for any amount
of arguments
@example
let doubleSum = twice(add);
doubleSum(1, 2, 4) // 1 + 2 + 4 + 1 + 2 + 4 = 14
@param {function} x
@return {any}
*/
function twice(func) {
return function(...args) {
return func(...args, ...args);
};
}
/**
Write a function `reverseb` that
reverses the arguments of a
binary function
@example
let bus = reverseb(subb);
bus(3, 2) // -1
@param {function} binary
@return {function}
*/
function reverseb(binary) {
return function(a, b) {
return binary(b, a);
};
}
/**
Write a function `reverse` that
is generalized for any amount
of arguments
@example
reverse(sub)(1, 2, 4) // 4 - 2 - 1 = 1
@param {function} func
@return {function}
*/
function reverse(func) {
return function(...args) {
return func(...args.reverse());
};
}
/**
Write a function `composeuTwo` that
takes two unary functions and
returns a unary function that
calls them both
@example
composeuTwo(doubl, square)(5) // 100
@param {function} unary1
@param {function} unary2
@return {function}
*/
function composeuTwo(unary1, unary2) {
return function(x) {
return unary2(unary1(x));
};
}
/**
Write a function `composeu` that
is generalized for any amount
of arguments
@example
composeu(doubl, square, identity, curry(add, 1, 2))(5) // (5 + 5) * (5 + 5) + 1 + 2 = 103
@param {...function} funcs
@return {any}
*/
function composeu(...funcs) {
return function(x) {
return funcs.reduce((result, func) => {
return func(result);
}, x);
};
}
function composeu2(...funcs) {
return function(x) {
return funcs.reduce((prev, curr) => {
return composeuTwo(prev, curr);
}, identityf(x))();
};
}
/**
Write a function `composeb` that
takes two binary functions and
returns a function that calls
them both
@example
composeb(addb, mulb)(2, 3, 7) // 35
@param {function} binary1
@param {function} binary2
@return {function}
*/
function composeb(binary1, binary2) {
return function(a, b, c) {
return binary2(binary1(a, b), c);
};
}
/**
Write a function `composeTwo` that
takes two functions and returns a
function that calls them both
@example
composeTwo(add, square)(2, 3, 7) // (2 + 3 + 7)^2 = 144
@param {function} func1
@param {function} func2
@return {function}
*/
function composeTwo(func1, func2) {
return function(...args) {
return func2(func1(...args));
}
}
/**
Write a function `compose` that
takes any amount of functions
and returns a function that takes
any amount of arguments and gives
them to the first function, then
that result to the second function
and so on
@example
const f = compose(add, doubl, fill, max);
f(0, 1, 2)
// add(0, 1, 2) -> 3
// doubl(3) -> 6
// fill(6) -> [ 6, 6, 6, 6, 6, 6 ]
// max([ 6, 6, 6, 6, 6, 6 ]) -> 6
@param {...function} funcs
@return {function}
*/
function compose(...funcs) {
return function(...args) {
return funcs.reduce((result, func) => {
return Array.isArray(result) ? func(...result) : func(result);
}, args);
};
}
/**
Write a function `limitb`
that allows a binary function
to be called a limited number
of times
@example
let addLmtb = limitb(addb, 1);
addLmtb(3, 4) // 7
addLmtb(3, 5) // undefined
@param {function} binary
@param {number} lmt
@return {function}
*/
function limitb(binary, lmt) {
return function(a, b) {
if (lmt > 0) {
lmt -= 1;
return binary(a, b);
}
return undefined; // be explicit
};
}
/**
Write a function `limit` that
is generalized for any amount
of arguments
@example
let addLmt = limit(add, 1);
addLmt(1, 2, 4) // 7
addLmt(3, 5, 9, 2) // undefined
@param {function} func
@param {number} lmt
@return {function}
*/
function limit(func, lmt) {
return function(...args) {
if (lmt > 0) {
lmt -= 1;
return func(...args);
}
return undefined; // be explicit
};
}
/**
Write a function `genFrom` that
produces a generator that will
produces a series of values
@example
let index = genFrom(0);
index() // 0
index() // 1
index() // 2
@param {number} x
@return {function}
*/
function genFrom(x) {
return function() {
let next = x;
x += 1;
return next;
};
}
/**
Write a function `genTo` that
takes a generator and an end
limit, and returns a generator
that will produce numbers up
to that limit
@example
let index = genTo(genFrom(1), 3);
index() // 1
index() // 2
index() // undefined
@param {function} gen
@param {number} lmt
@return {function}
*/
function genTo(gen, lmt) {
return function(x) {
let next = gen(x);
if (next < lmt) {
return next;
}
return undefined; // be explicit
};
}
/**
Write a function `genFromTo` that
produces a generator that will
produce values in a range
@example
let index = genFromTo(0, 3);
index() // 0
index() // 1
index() // 2
index() // undefined
@param {number} start
@param {number} end
@return {function}
*/
function genFromTo(start, end) {
return genTo(genFrom(start), end);
}
/**
Write a function `elementGen` that
takes an array and a generator
and returns a generator that will
produce elements from the array
@example
let ele = elementGen([
'a', 'b', 'c', 'd'
], genFromTo(1, 3));
ele() // 'b'
ele() // 'c'
ele() // undefined
@param {array} array
@param {function} gen
@return {function}
*/
function elementGen(array, gen) {
return function() {
let index = gen();
return index !== undefined ? array[index] : undefined;
};
}
/**
Write a function `element` that is a
modified `elementGen` function so that
the generator argument is optional.
If a generator is not provided, then
each of the elements of the array
will be produced.
@example
let ele = element([
'a', 'b', 'c', 'd'
]);
ele() // 'a'
ele() // 'b'
ele() // 'c'
ele() // 'd'
ele() // undefined
@param {array} array
@param {function} gen
@return {function}
*/
function element(array, gen) {
if (typeof gen !== 'function') {
gen = genFromTo(0, array.length);
}
return elementGen(array, gen);
}
/**
Write a function `collect` that takes a
generator and an array and produces
a function that will collect the results
in the array
@example
let array = [];
let col = collect(genFromTo(0, 2), array);
col() // 0
col() // 1
col() // undefined
array // [0, 1]
@param {function} gen
@param {array} array
@return {function}
*/
function collect(gen, array) {