-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathramda.js
More file actions
5542 lines (5099 loc) · 179 KB
/
ramda.js
File metadata and controls
5542 lines (5099 loc) · 179 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
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
// ramda.js
// https://github.com/ramda/ramda
// (c) 2013-2014 Scott Sauyet and Michael Hurley
// Ramda may be freely distributed under the MIT license.
// Ramda
// -----
// A practical functional library for Javascript programmers. Ramda is a collection of tools to make it easier to
// use Javascript as a functional programming language. (The name is just a silly play on `lambda`.)
// Basic Setup
// -----------
// Uses a technique from the [Universal Module Definition][umd] to wrap this up for use in Node.js or in the browser,
// with or without an AMD-style loader.
//
// [umd]: https://github.com/umdjs/umd/blob/master/returnExports.js
(function(factory) {
if (typeof exports === 'object') {
module.exports = factory(this);
} else if (typeof define === 'function' && define.amd) {
define(factory);
} else {
this.R = this.ramda = factory(this);
}
}(function() {
'use strict';
// This object is what is actually returned, with all the exposed functions attached as properties.
/**
* A practical functional library for Javascript programmers.
*
* @namespace R
*/
// jscs:disable disallowQuotedKeysInObjects
var R = {'version': '0.6.0'};
// jscs:enable disallowQuotedKeysInObjects
// Internal Functions and Properties
// ---------------------------------
/**
* Creates an exception about calling a function with no arguments.
*
* @private
* @category Internal
* @return {TypeError} A no arguments exception.
*/
function noArgsException() {
return new TypeError('Function called with no arguments');
}
/**
* An optimized, private array `slice` implementation.
*
* @private
* @category Internal
* @param {Arguments|Array} args The array or arguments object to consider.
* @param {number} [from=0] The array index to slice from, inclusive.
* @param {number} [to=args.length] The array index to slice to, exclusive.
* @return {Array} A new, sliced array.
* @example
*
* _slice([1, 2, 3, 4, 5], 1, 3); //=> [2, 3]
*
* var firstThreeArgs = function(a, b, c, d) {
* return _slice(arguments, 0, 3);
* };
* firstThreeArgs(1, 2, 3, 4); //=> [1, 2, 3]
*/
function _slice(args, from, to) {
switch (arguments.length) {
case 0: throw noArgsException();
case 1: return _slice(args, 0, args.length);
case 2: return _slice(args, from, args.length);
default:
var length = to - from, list = new Array(length), idx = -1;
while (++idx < length) {
list[idx] = args[from + idx];
}
return list;
}
}
/**
* Private `concat` function to merge two array-like objects.
*
* @private
* @category Internal
* @param {Array|Arguments} [set1=[]] An array-like object.
* @param {Array|Arguments} [set2=[]] An array-like object.
* @return {Array} A new, merged array.
* @example
*
* concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3]
*/
var concat = function _concat(set1, set2) {
set1 = set1 || [];
set2 = set2 || [];
var length1 = set1.length,
length2 = set2.length,
result = new Array(length1 + length2);
for (var idx = 0; idx < length1; idx++) {
result[idx] = set1[idx];
}
for (idx = 0; idx < length2; idx++) {
result[idx + length1] = set2[idx];
}
return result;
};
// Private reference to toString function.
var toString = Object.prototype.toString;
/**
* Tests whether or not an object is an array.
*
* @private
* @category Internal
* @param {*} val The object to test.
* @return {boolean} `true` if `val` is an array, `false` otherwise.
* @example
*
* isArray([]); //=> true
* isArray(true); //=> false
* isArray({}); //=> false
*/
var isArray = Array.isArray || function _isArray(val) {
return val && val.length >= 0 && toString.call(val) === '[object Array]';
};
/**
* Tests whether or not an object is similar to an array.
*
* @func
* @memberOf R
* @category Type
* @category List
* @param {*} x The object to test.
* @return {boolean} `true` if `x` has a numeric length property; `false` otherwise.
* @example
*
* R.isArrayLike([]); //=> true
* R.isArrayLike(true); //=> false
* R.isArrayLike({}); //=> false
* R.isArrayLike({length: 10}); //=> true
*/
R.isArrayLike = function isArrayLike(x) {
return isArray(x) || (
!!x &&
typeof x === 'object' &&
!(x instanceof String) &&
(
!!(x.nodeType === 1 && x.length) ||
x.length >= 0
)
);
};
/**
* Optimized internal two-arity curry function.
*
* @private
* @category Function
* @param {Function} fn The function to curry.
* @return {Function} curried function
* @example
*
* var addTwo = function(a, b) {
* return a + b;
* };
*
* var curriedAddTwo = curry2(addTwo);
*/
function curry2(fn) {
return function(a, b) {
switch (arguments.length) {
case 0:
throw noArgsException();
case 1:
return function(b) {
return fn(a, b);
};
default:
return fn(a, b);
}
};
}
/**
* Optimized internal three-arity curry function.
*
* @private
* @category Function
* @param {Function} fn The function to curry.
* @return {Function} curried function
* @example
*
* var addThree = function(a, b, c) {
* return a + b + c;
* };
*
* var curriedAddThree = curry3(addThree);
*/
function curry3(fn) {
return function(a, b, c) {
switch (arguments.length) {
case 0:
throw noArgsException();
case 1:
return curry2(function(b, c) {
return fn(a, b, c);
});
case 2:
return function(c) {
return fn(a, b, c);
};
default:
return fn(a, b, c);
}
};
}
var __; // This is intentionally left `undefined`.
try {
Object.defineProperty(R, '__', {writable: false, value: __});
} catch (e) {
R.__ = __;
}
/**
* Uses a placeholder to convert a binary function into something like an infix operation.
* When called with an `undefined` placeholder (e.g. `R.__`) the second argument is applied to the
* second position, and it returns a function waiting for its first argument.
* This can allow for more natural processing of functions which are really binary operators.
*
* @func
* @memberOf R
* @category Function
* @param {function} fn The binary operation to adjust
* @return {function} A new function that acts somewhat like an infix operator.
* @example
*
* var div = R.op(function (a, b) {
* return a / b;
* });
*
* div(6, 3); //=> 2
* div(6)(3); //=> 2
* div(__, 3)(6); //=> 2 // note: `__` here is just an `undefined` value. You could use `void 0` instead
* div(__)(3, 6); //=> 2
* div(__)(3)(6); //=> 2
*/
var op = R.op = function op(fn) {
var length = fn.length;
if (length !== 2) {throw new Error('Expected binary function.');}
return function _op(a, b) {
switch (arguments.length) {
case 0: throw noArgsException();
case 1:
if (a === __) {
return R.flip(_op);
}
return R.lPartial(fn, a);
default:
if (a === __) {
return R.rPartial(fn, b);
}
return fn(a, b);
}
};
};
/**
* Creates a new version of `fn` with given arity that, when invoked,
* will return either:
* - A new function ready to accept one or more of `fn`'s remaining arguments, if all of
* `fn`'s expected arguments have not yet been provided
* - `fn`'s result if all of its expected arguments have been provided
*
* This function is useful in place of `curry`, when the arity of the
* function to curry cannot be determined from its signature, e.g. if it's
* a variadic function.
*
* @func
* @memberOf R
* @category core
* @category Function
* @sig Number -> (* -> a) -> (* -> a)
* @param {number} fnArity The arity for the returned function.
* @param {Function} fn The function to curry.
* @return {Function} A new, curried function.
* @see R.curry
* @example
*
* var addFourNumbers = function() {
* return R.sum([].slice.call(arguments, 0, 4));
* };
*
* var curriedAddFourNumbers = R.curryN(4, addFourNumbers);
* var f = curriedAddFourNumbers(1, 2);
* var g = f(3);
* g(4);//=> 10
*/
var curryN = R.curryN = function curryN(length, fn) {
return (function recurry(args) {
return arity(Math.max(length - (args && args.length || 0), 0), function() {
if (arguments.length === 0) { throw noArgsException(); }
var newArgs = concat(args, arguments);
if (newArgs.length >= length) {
return fn.apply(this, newArgs);
} else {
return recurry(newArgs);
}
});
}([]));
};
/**
* Creates a new version of `fn` that, when invoked, will return either:
* - A new function ready to accept one or more of `fn`'s remaining arguments, if all of
* `fn`'s expected arguments have not yet been provided
* - `fn`'s result if all of its expected arguments have been provided
*
* @func
* @memberOf R
* @category core
* @category Function
* @sig (* -> a) -> (* -> a)
* @param {Function} fn The function to curry.
* @return {Function} A new, curried function.
* @see R.curryN
* @example
*
* var addFourNumbers = function(a, b, c, d) {
* return a + b + c + d;
* };
*
* var curriedAddFourNumbers = R.curry(addFourNumbers);
* var f = curriedAddFourNumbers(1, 2);
* var g = f(3);
* g(4);//=> 10
*/
var curry = R.curry = function curry(fn) {
return curryN(fn.length, fn);
};
/**
* Private function that determines whether or not a provided object has a given method.
* Does not ignore methods stored on the object's prototype chain. Used for dynamically
* dispatching Ramda methods to non-Array objects.
*
* @private
* @category Internal
* @param {string} methodName The name of the method to check for.
* @param {Object} obj The object to test.
* @return {boolean} `true` has a given method, `false` otherwise.
* @example
*
* var person = { name: 'John' };
* person.shout = function() { alert(this.name); };
*
* hasMethod('shout', person); //=> true
* hasMethod('foo', person); //=> false
*/
var hasMethod = function _hasMethod(methodName, obj) {
return obj && !isArray(obj) && typeof obj[methodName] === 'function';
};
/**
* Similar to hasMethod, this checks whether a function has a [methodname]
* function. If it isn't an array it will execute that function otherwise it will
* default to the ramda implementation.
*
* @private
* @category Internal
* @param {Function} fn ramda implemtation
* @param {String} methodname property to check for a custom implementation
* @return {Object} whatever the return value of the method is
*/
function checkForMethod(methodname, fn) {
return function(a, b, c) {
var length = arguments.length;
var obj = arguments[length - 1],
callBound = obj && !isArray(obj) && typeof obj[methodname] === 'function';
switch (arguments.length) {
case 0: return fn();
case 1: return callBound ? obj[methodname]() : fn(a);
case 2: return callBound ? obj[methodname](a) : fn(a, b);
case 3: return callBound ? obj[methodname](a, b) : fn(a, b, c);
}
};
}
/**
* Wraps a function of any arity (including nullary) in a function that accepts exactly `n`
* parameters. Any extraneous parameters will not be passed to the supplied function.
*
* @func
* @memberOf R
* @category Function
* @sig Number -> (* -> a) -> (* -> a)
* @param {number} n The desired arity of the new function.
* @param {Function} fn The function to wrap.
* @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
* arity `n`.
* @example
*
* var takesTwoArgs = function(a, b) {
* return [a, b];
* };
* takesTwoArgs.length; //=> 2
* takesTwoArgs(1, 2); //=> [1, 2]
*
* var takesOneArg = R.nAry(1, takesTwoArgs);
* takesOneArg.length; //=> 1
* // Only `n` arguments are passed to the wrapped function
* takesOneArg(1, 2); //=> [1, undefined]
*/
var nAry = R.nAry = function(n, fn) {
switch (n) {
case 0: return function() {return fn.call(this);};
case 1: return function(a0) {return fn.call(this, a0);};
case 2: return function(a0, a1) {return fn.call(this, a0, a1);};
case 3: return function(a0, a1, a2) {return fn.call(this, a0, a1, a2);};
case 4: return function(a0, a1, a2, a3) {return fn.call(this, a0, a1, a2, a3);};
case 5: return function(a0, a1, a2, a3, a4) {return fn.call(this, a0, a1, a2, a3, a4);};
case 6: return function(a0, a1, a2, a3, a4, a5) {return fn.call(this, a0, a1, a2, a3, a4, a5);};
case 7: return function(a0, a1, a2, a3, a4, a5, a6) {return fn.call(this, a0, a1, a2, a3, a4, a5, a6);};
case 8: return function(a0, a1, a2, a3, a4, a5, a6, a7) {return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7);};
case 9: return function(a0, a1, a2, a3, a4, a5, a6, a7, a8) {return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7, a8);};
case 10: return function(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {return fn.call(this, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);};
default: return fn; // TODO: or throw?
}
};
/**
* Wraps a function of any arity (including nullary) in a function that accepts exactly 1
* parameter. Any extraneous parameters will not be passed to the supplied function.
*
* @func
* @memberOf R
* @category Function
* @sig (* -> b) -> (a -> b)
* @param {Function} fn The function to wrap.
* @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
* arity 1.
* @example
*
* var takesTwoArgs = function(a, b) {
* return [a, b];
* };
* takesTwoArgs.length; //=> 2
* takesTwoArgs(1, 2); //=> [1, 2]
*
* var takesOneArg = R.unary(takesTwoArgs);
* takesOneArg.length; //=> 1
* // Only 1 argument is passed to the wrapped function
* takesOneArg(1, 2); //=> [1, undefined]
*/
R.unary = function _unary(fn) {
return nAry(1, fn);
};
/**
* Wraps a function of any arity (including nullary) in a function that accepts exactly 2
* parameters. Any extraneous parameters will not be passed to the supplied function.
*
* @func
* @memberOf R
* @category Function
* @sig (* -> c) -> (a, b -> c)
* @param {Function} fn The function to wrap.
* @return {Function} A new function wrapping `fn`. The new function is guaranteed to be of
* arity 2.
* @example
*
* var takesThreeArgs = function(a, b, c) {
* return [a, b, c];
* };
* takesThreeArgs.length; //=> 3
* takesThreeArgs(1, 2, 3); //=> [1, 2, 3]
*
* var takesTwoArgs = R.binary(takesThreeArgs);
* takesTwoArgs.length; //=> 2
* // Only 2 arguments are passed to the wrapped function
* takesTwoArgs(1, 2, 3); //=> [1, 2, undefined]
*/
var binary = R.binary = function _binary(fn) {
return nAry(2, fn);
};
/**
* Wraps a function of any arity (including nullary) in a function that accepts exactly `n`
* parameters. Unlike `nAry`, which passes only `n` arguments to the wrapped function,
* functions produced by `arity` will pass all provided arguments to the wrapped function.
*
* @func
* @memberOf R
* @sig (Number, (* -> *)) -> (* -> *)
* @category Function
* @param {number} n The desired arity of the returned function.
* @param {Function} fn The function to wrap.
* @return {Function} A new function wrapping `fn`. The new function is
* guaranteed to be of arity `n`.
* @example
*
* var takesTwoArgs = function(a, b) {
* return [a, b];
* };
* takesTwoArgs.length; //=> 2
* takesTwoArgs(1, 2); //=> [1, 2]
*
* var takesOneArg = R.arity(1, takesTwoArgs);
* takesOneArg.length; //=> 1
* // All arguments are passed through to the wrapped function
* takesOneArg(1, 2); //=> [1, 2]
*/
var arity = R.arity = function(n, fn) {
switch (n) {
case 0: return function() {return fn.apply(this, arguments);};
case 1: return function(a0) {void a0; return fn.apply(this, arguments);};
case 2: return function(a0, a1) {void a1; return fn.apply(this, arguments);};
case 3: return function(a0, a1, a2) {void a2; return fn.apply(this, arguments);};
case 4: return function(a0, a1, a2, a3) {void a3; return fn.apply(this, arguments);};
case 5: return function(a0, a1, a2, a3, a4) {void a4; return fn.apply(this, arguments);};
case 6: return function(a0, a1, a2, a3, a4, a5) {void a5; return fn.apply(this, arguments);};
case 7: return function(a0, a1, a2, a3, a4, a5, a6) {void a6; return fn.apply(this, arguments);};
case 8: return function(a0, a1, a2, a3, a4, a5, a6, a7) {void a7; return fn.apply(this, arguments);};
case 9: return function(a0, a1, a2, a3, a4, a5, a6, a7, a8) {void a8; return fn.apply(this, arguments);};
case 10: return function(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {void a9; return fn.apply(this, arguments);};
default: return fn; // TODO: or throw?
}
};
/**
* Turns a named method of an object (or object prototype) into a function
* that can be called directly.
*
* The returned function is curried and accepts `len + 1` parameters and
* the final parameter is the target object.
*
* @func
* @memberOf R
* @category Function
* @sig (Number, (a... -> b)) -> (a... -> c -> b)
* @param {number} len Number of arguments the returned function should take
* before the target object.
* @param {Function} method The method to wrap.
* @return {Function} A new curried function.
* @see R.invoker
* @example
*
* var sliceFrom = R.invokerN(1, String.prototype.slice);
* sliceFrom(6, 'abcdefghijklm'); //=> 'ghijklm'
*/
var invokerN = R.invokerN = function invokerN(len, method) {
return curryN(len + 1, function() {
var target = arguments[len];
return method.apply(target,
Array.prototype.slice.call(arguments, 0, len));
});
};
/**
* Turns a named method of an object (or object prototype) into a function that can be
* called directly.
*
* The returned function is curried and accepts `method.length + 1` parameters
* and the final parameter is the target object.
*
* @func
* @memberOf R
* @category Function
* @sig (a... -> b) -> (a... -> c -> b)
* @param {Function} method The method to wrap.
* @return {Function} A new curried function.
* @see R.invokerN
* @example
*
* var charAt = R.invoker(String.prototype.charAt);
* charAt(6, 'abcdefghijklm'); //=> 'g'
*
* var join = R.invoker(Array.prototype.join);
* var firstChar = charAt(0);
* join('', R.map(firstChar, ['light', 'ampliifed', 'stimulated', 'emission', 'radiation']));
* //=> 'laser'
*/
var invoker = R.invoker = function invoker(method) {
return invokerN(method.length, method);
};
/**
* Accepts a function `fn` and any number of transformer functions and returns a new
* function. When the new function is invoked, it calls the function `fn` with parameters
* consisting of the result of calling each supplied handler on successive arguments to the
* new function. For example:
*
* ```javascript
*
* var between = R.useWith(R.and,R.lt,R.gt)
*
* //This invocation
* var isTeen = between(12,20)
*
* //Is functionally equivalent to:
* var isTeen = R.and(R.gt(12), R.lt(20))
*
* isTeen(13) //=> true
* isTeen(45) //=> false
*
* ```
*
* If more arguments are passed to the returned function than transformer functions, those
* arguments are passed directly to `fn` as additional parameters. If you expect additional
* arguments that don't need to be transformed, although you can ignore them, it's best to
* pass an identity function so that the new function reports the correct arity.
*
* @func
* @memberOf R
* @category Function
* @sig ((* -> *), (* -> *)...) -> (* -> *)
* @param {Function} fn The function to wrap.
* @param {...Function} transformers A variable number of transformer functions
* @return {Function} The wrapped function.
* @example
*
* var double = function(y) { return y * 2; };
* var square = function(x) { return x * x; };
* var add = function(a, b) { return a + b; };
* // Adds any number of arguments together
* var addAll = function() {
* return R.reduce(add, 0, arguments);
* };
*
* // Basic example
* var addDoubleAndSquare = R.useWith(addAll, double, square);
*
* //≅ addAll(double(10), square(5));
* addDoubleAndSquare(10, 5); //=> 45
*
* // Example of passing more arguments than transformers
* //≅ addAll(double(10), square(5), 100);
* addDoubleAndSquare(10, 5, 100); //=> 145
*
* // But if you're expecting additional arguments that don't need transformation, it's best
* // to pass transformer functions so the resulting function has the correct arity
* var addDoubleAndSquareWithExtraParams = R.useWith(addAll, double, square, R.identity);
* //≅ addAll(double(10), square(5), R.identity(100));
* addDoubleAndSquare(10, 5, 100); //=> 145
*/
var useWith = R.useWith = function _useWith(fn /*, transformers */) {
var transformers = _slice(arguments, 1);
var tlen = transformers.length;
return curry(arity(tlen, function() {
var args = [], idx = -1;
while (++idx < tlen) {
args.push(transformers[idx](arguments[idx]));
}
return fn.apply(this, args.concat(_slice(arguments, tlen)));
}));
};
/**
* Iterate over an input `list`, calling a provided function `fn` for each element in the
* list.
*
* `fn` receives one argument: *(value)*.
*
* Note: `R.forEach` does not skip deleted or unassigned indices (sparse arrays), unlike
* the native `Array.prototype.forEach` method. For more details on this behavior, see:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Description
*
* Also note that, unlike `Array.prototype.forEach`, Ramda's `forEach` returns the original
* array. In some libraries this function is named `each`.
*
* @func
* @memberOf R
* @category List
* @sig (a -> *) -> [a] -> [a]
* @param {Function} fn The function to invoke. Receives one argument, `value`.
* @param {Array} list The list to iterate over.
* @return {Array} The original list.
* @example
*
* var printXPlusFive = function(x) { console.log(x + 5); };
* R.forEach(printXPlusFive, [1, 2, 3]); //=> [1, 2, 3]
* //-> 6
* //-> 7
* //-> 8
*/
function forEach(fn, list) {
var idx = -1, len = list.length;
while (++idx < len) {
fn(list[idx]);
}
// i can't bear not to return *something*
return list;
}
R.forEach = curry2(forEach);
/**
* Like `forEach`, but but passes additional parameters to the predicate function.
*
* `fn` receives three arguments: *(value, index, list)*.
*
* Note: `R.forEach.idx` does not skip deleted or unassigned indices (sparse arrays),
* unlike the native `Array.prototype.forEach` method. For more details on this behavior,
* see:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Description
*
* Also note that, unlike `Array.prototype.forEach`, Ramda's `forEach` returns the original
* array. In some libraries this function is named `each`.
*
* @func
* @memberOf R
* @category List
* @sig (a, i, [a] -> ) -> [a] -> [a]
* @param {Function} fn The function to invoke. Receives three arguments:
* (`value`, `index`, `list`).
* @param {Array} list The list to iterate over.
* @return {Array} The original list.
* @alias forEach.idx
* @example
*
* // Note that having access to the original `list` allows for
* // mutation. While you *can* do this, it's very un-functional behavior:
* var plusFive = function(num, idx, list) { list[idx] = num + 5 };
* R.forEach.idx(plusFive, [1, 2, 3]); //=> [6, 7, 8]
*/
R.forEach.idx = curry2(function forEachIdx(fn, list) {
var idx = -1, len = list.length;
while (++idx < len) {
fn(list[idx], idx, list);
}
// i can't bear not to return *something*
return list;
});
/**
* Creates a shallow copy of an array.
*
* @func
* @memberOf R
* @category core
* @category List
* @sig [a] -> [a]
* @param {Array} list The list to clone.
* @return {Array} A new copy of the original list.
* @example
*
* var numbers = [1, 2, 3];
* var numbersClone = R.clone(numbers); //=> [1, 2, 3]
* numbers === numbersClone; //=> false
*
* // Note that this is a shallow clone--it does not clone complex values:
* var objects = [{}, {}, {}];
* var objectsClone = R.clone(objects);
* objects[0] === objectsClone[0]; //=> true
*/
var clone = R.clone = function _clone(list) {
return _slice(list);
};
// Core Functions
// --------------
//
/**
* Reports whether an array is empty.
*
* @func
* @memberOf R
* @category List
* @sig [a] -> Boolean
* @param {Array} list The array to consider.
* @return {boolean} `true` if the `list` argument has a length of 0 or
* if `list` is a falsy value (e.g. undefined).
* @example
*
* R.isEmpty([1, 2, 3]); //=> false
* R.isEmpty([]); //=> true
* R.isEmpty(); //=> true
* R.isEmpty(null); //=> true
*/
function isEmpty(list) {
return !list || !list.length;
}
R.isEmpty = isEmpty;
/**
* Returns a new list with the given element at the front, followed by the contents of the
* list.
*
* @func
* @memberOf R
* @category core
* @category List
* @sig a -> [a] -> [a]
* @param {*} el The item to add to the head of the output list.
* @param {Array} list The array to add to the tail of the output list.
* @return {Array} A new array.
* @example
*
* R.prepend('fee', ['fi', 'fo', 'fum']); //=> ['fee', 'fi', 'fo', 'fum']
*/
R.prepend = curry2(function prepend(el, list) {
return concat([el], list);
});
/**
* @func
* @memberOf R
* @category List
* @see R.prepend
*/
R.cons = R.prepend;
/**
* Returns the first element in a list.
* In some libraries this function is named `first`.
*
* @func
* @memberOf R
* @category core
* @category List
* @sig [a] -> a
* @param {Array} [list=[]] The array to consider.
* @return {*} The first element of the list, or `undefined` if the list is empty.
* @example
*
* R.head(['fi', 'fo', 'fum']); //=> 'fi'
*/
R.head = function head(list) {
list = list || [];
return list[0];
};
/**
* @func
* @memberOf R
* @category List
* @see R.head
*/
R.car = R.head;
/**
* Returns the last element from a list.
*
* @func
* @memberOf R
* @category List
* @sig [a] -> a
* @param {Array} [list=[]] The array to consider.
* @return {*} The last element of the list, or `undefined` if the list is empty.
* @example
*
* R.last(['fi', 'fo', 'fum']); //=> 'fum'
*/
R.last = function _last(list) {
list = list || [];
return list[list.length - 1];
};
/**
* Returns all but the first element of a list. If the list provided has the `tail` method,
* it will instead return `list.tail()`.
*
* @func
* @memberOf R
* @category core
* @category List
* @sig [a] -> [a]
* @param {Array} [list=[]] The array to consider.
* @return {Array} A new array containing all but the first element of the input list, or an
* empty list if the input list is a falsy value (e.g. `undefined`).
* @example
*
* R.tail(['fi', 'fo', 'fum']); //=> ['fo', 'fum']
*/
R.tail = checkForMethod('tail', function(list) {
list = list || [];
return (list.length > 1) ? _slice(list, 1) : [];
});
/**
* @func
* @memberOf R
* @category List
* @see R.tail
*/
R.cdr = R.tail;
/**
* Returns a new list containing the contents of the given list, followed by the given
* element.
*
* @func
* @memberOf R
* @category core
* @category List
* @sig a -> [a] -> [a]
* @param {*} el The element to add to the end of the new list.
* @param {Array} list The list whose contents will be added to the beginning of the output
* list.
* @return {Array} A new list containing the contents of the old list followed by `el`.
* @example
*
* R.append('tests', ['write', 'more']); //=> ['write', 'more', 'tests']
* R.append('tests', []); //=> ['tests']
* R.append(['tests'], ['write', 'more']); //=> ['write', 'more', ['tests']]
*/
var append = R.append = curry2(function _append(el, list) {
return concat(list, [el]);
});
/**
* @func
* @memberOf R
* @category List
* @see R.append
*/
R.push = R.append;
/**
* Returns a new list consisting of the elements of the first list followed by the elements
* of the second.
*
* @func
* @memberOf R
* @category core
* @category List
* @sig [a] -> [a] -> [a]
* @param {Array} list1 The first list to merge.
* @param {Array} list2 The second set to merge.
* @return {Array} A new array consisting of the contents of `list1` followed by the
* contents of `list2`. If, instead of an Array for `list1`, you pass an
* object with a `concat` method on it, `concat` will call `list1.concat`
* and pass it the value of `list2`.
* @example
*
* R.concat([], []); //=> []
* R.concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3]
* R.concat('ABC', 'DEF'); // 'ABCDEF'
*/
R.concat = curry2(function(set1, set2) {
if (isArray(set2)) {
return concat(set1, set2);
} else if (hasMethod('concat', set1)) {
return set1.concat(set2);
} else {
throw new TypeError("can't concat " + typeof set1);
}
});
/**
* A function that does nothing but return the parameter supplied to it. Good as a default
* or placeholder function.
*
* @func
* @memberOf R
* @category Core
* @sig a -> a
* @param {*} x The value to return.
* @return {*} The input value, `x`.
* @example
*
* R.identity(1); //=> 1