-
Notifications
You must be signed in to change notification settings - Fork 4
/
ramda.js
1210 lines (1026 loc) · 48.7 KB
/
ramda.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
// ramda.js 0.0.1
// https://github.com/CrossEye/ramda
// (c) 2013 Scott Sauyet and Michael Hurley
// Ramda may be freely distributed under the MIT license.
// Ramda
// -----
// A practical functional library for Javascript programmers. This 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`, even though we're
// not actually involved in lambda expressions.)
// 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 (root, factory) {if (typeof exports === 'object') {module.exports = factory(root);} else if (typeof define === 'function' && define.amd) {define(factory);} else {root.ramda = factory(root);}}(this, function (global) {
return (function() {
// This object is what is actually returned, with all the exposed functions attached as properties.
var R = {};
// Internal Functions and Properties
// ---------------------------------
var undef = (function(){})(), EMPTY;
// Makes a public alias for one of the public functions:
var aliasFor = function(oldName) {
var fn = function(newName) {R[newName] = R[oldName]; return fn;};
return (fn.is = fn.are = fn.and = fn);
};
// `slice` implemented iteratively for performance
var slice = function (args, from, to) {
var i, arr = [];
from = from || 0;
to = to || args.length;
for (i = from; i < to; i++) {
arr[arr.length] = args[i];
}
return arr;
};
var isArray = function(val) {return Object.prototype.toString.call(val) === "[object Array]";};
// Returns a curried version of the supplied function. For example:
//
// var discriminant = function(a, b, c) {
// return b * b - 4 * a * c;
// };
// var f = curry(discriminant);
// var g = f(3), h = f(3, 7) i = g(7);
// i(4) ≅ h(4) == g(7, 4) == f(3, 7, 4) == 1
//
// Almost all exposed functions of more than one parameter already have curry applied to them.
var _ = R.curry = function(fn) {
var fnArity = fn.length;
var f = function(args) {
return arity(Math.max(fnArity - (args && args.length || 0), 0), function () {
var newArgs = (args || []).concat(slice(arguments, 0));
if (newArgs.length >= fnArity) {
return fn.apply(this, newArgs);
}
else {return f(newArgs);}
});
};
return f([]);
};
var mkArgStr = function(n) {
var arr = [], idx = -1;
while(++idx < n) {
arr[idx] = "arg" + idx;
}
return arr.join(", ");
};
// Wraps a function that may be nullary, or may take fewer than or more than `n` parameters, in a function that
// specifically takes exactly `n` parameters. Any extraneous parameters will not be passed on to the function
// supplied
var nAry = R.nAry = (function() {
var cache = {};
// For example:
// cache[3] = function(func) {
// return function(arg0, arg1, arg2) {
// return func.call(this, arg0, arg1, arg2);
// }
// };
var makeN = function(n) {
var fnArgs = mkArgStr(n);
var body = [
" return function(" + fnArgs + ") {",
" return func.call(this" + (fnArgs ? ", " + fnArgs : "") + ");",
" }"
].join("\n");
return new Function("func", body);
};
return function(n, fn) {
return (cache[n] || (cache[n] = makeN(n)))(fn);
};
}());
// Wraps a function that may be nullary, or may take fewer than or more than `n` parameters, in a function that
// specifically takes exactly `n` parameters. Note, though, that all parameters supplied will in fact be
// passed along, in contrast with `nAry`, which only passes along the exact number specified.
var arity = R.arity = (function() {
var cache = {};
// For example:
// cache[3] = function(func) {
// return function(arg0, arg1, arg2) {
// return func.apply(this, arguments);
// }
// };
var makeN = function(n) {
var fnArgs = mkArgStr(n);
var body = [
" return function(" + fnArgs + ") {",
" return func.apply(this, arguments);",
" }"
].join("\n");
return new Function("func", body);
};
return function(n, fn) {
return (cache[n] || (cache[n] = makeN(n)))(fn);
};
}());
// Turns a named method of an object (or object prototype) into a function that can be called directly.
// The object becomes the last parameter to the function, and the function is automatically curried.
var invoker = R.invoker = function(name, obj) {
var method = obj[name];
return method && _(nAry(method.length + 1, function() {
if(arguments.length) {
var target = Array.prototype.pop.call(arguments);
var targetMethod = target[name];
if (targetMethod == method) {
return targetMethod.apply(target, arguments);
}
}
return undef;
}));
};
// Creates a new function that calls the function `fn` with parameters consisting of the result of the
// calling each supplied handler on successive arguments, followed by all unmatched arguments.
//
// If there are extra _expected_ arguments that don't need to be transformed, although you can ignore
// them, it might be best to pass in and identity function so that the new function correctly reports arity.
// See for example, the definition of `project`, below.
var useWith = R.useWith = function(fn /*, transformers */) {
var transformers = slice(arguments, 1);
var tlen = transformers.length;
return _(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)));
}));
};
// A two-step version of the `useWith` function. This would allow us to write `project`, currently written
// as `useWith(map, pickAll, identity)`, as, instead, `use(map).over(pickAll, identity)`, which is a bit
// more explicit.
// TODO: One of these versions should be eliminated eventually. So not worrying about the duplication for now.
R.use = function(fn) {
return {
over: function(/*transformers*/) {
var transformers = slice(arguments, 0);
var tlen = transformers.length;
return _(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)));
}));
}
}
};
// Fills out an array to the specified length. Internal private function.
var expand = function(a, len) {
var arr = a ? isArray(a) ? a : slice(a) : [];
while(arr.length < len) {arr[arr.length] = undef;}
return arr;
};
// Internal version of `forEach`. Possibly to be exposed later.
var each = _(function(fn, arr) {
for (var i = 0, len = arr.length; i < len; i++) {
fn(arr[i]);
}
});
// Shallow copy of an array.
var clone = R.clone = function(list) {
return list.concat();
};
// Core Functions
// --------------
//
// Prototypical (or only) empty list
EMPTY = [];
// Boolean function which reports whether a list is empty.
var isEmpty = R.isEmpty = function(arr) {return !arr || !arr.length;};
// Returns a new list with the new element at the front and the existing elements following
var prepend = R.prepend = function(el, arr) {return [el].concat(arr);};
aliasFor("prepend").is("cons");
// Returns the first element of a list
var head = R.head = function(arr) {
arr = arr || EMPTY;
return (arr.length) ? arr[0] : EMPTY; // TODO: shouldn't head(EMPTY) return null?
};
aliasFor("head").is("car");
// Returns the rest of the list after the first element.
// If the passed-in list is a Generator, it will return the
// next iteration of the Generator.
var tail = R.tail = function(arr) {
arr = arr || EMPTY;
if (arr.length === Infinity) {
return arr.tail();
}
return (arr.length > 1) ? slice(arr, 1) : EMPTY;
};
aliasFor("tail").is("cdr");
// Boolean function which is `true` for non-list, `false` for a list.
R.isAtom = function(x) {
return (x !== null) && (x !== undef) && Object.prototype.toString.call(x) !== "[object Array]";
};
// Returns a new list with the new element at the end of a list following all the existing ones.
R.append = function(el, list) {
var newList = clone(list);
newList.push(el);
return newList;
};
aliasFor("append").is("push");
// Returns a new list consisting of the elements of the first list followed by the elements of the second.
var merge = R.merge = _(function(list1, list2) {
if (isEmpty(list1)) {
return clone(list2);
} else {
return list1.concat(list2);
}
});
aliasFor("merge").is("concat");
// A surprisingly useful function that does nothing but return the parameter supplied to it.
var identity = R.identity = function(x) {return x;};
aliasFor("identity").is("I");
// Generators
// ----------
//
// Support for infinite lists, using an initial seed, a function that calculates the head from the seed and
// a function that creates a new seed from the current seed. Generator objects have this structure:
//
// {
// "0": someValue,
// tail: someFunction() {},
// length: Infinity
// }
//
// Generator objects also have such functions as `take`, `skip`, `map`, and `filter`, but the equivalent
// functions from Ramda will work with them as well.
//
// ### Example ###
//
// var fibonacci = generator(
// [0, 1],
// function(pair) {return pair[0];},
// function(pair) {return [pair[1], pair[0] + pair[1]];}
// );
// var even = function(n) {return (n % 2) === 0;};
//
// take(5, filter(even, fibonacci)) //=> [0, 2, 8, 34, 144]
//
// Note that the `take(5)` call is necessary to get a finite list out of this. Otherwise, this would still
// be an infinite list.
R.generator = (function() {
// partial shim for Object.create
var create = (function() {
var F = function() {};
return function(src) {
F.prototype = src;
return new F();
};
}());
// Trampolining to support recursion in Generators
var trampoline = function(fn) {
var result = fn.apply(this, tail(arguments));
while (typeof result === "function") {
result = result();
}
return result;
};
// Internal Generator constructor
var G = function(seed, current, step) {
this["0"] = current(seed);
this.tail = function() {
return new G(step(seed), current, step);
};
};
// Generators can be used with OO techniques as well as our standard functional calls. These are the
// implementations of those methods and other properties.
G.prototype = {
constructor: G,
// All generators are infinite.
length: Infinity,
// `take` implementation for generators.
take: function(n) {
var take = function(ctr, g, ret) {
return (ctr === 0) ? ret : take(ctr - 1, g.tail(), ret.concat([g[0]]));
};
return trampoline(take, n, this, []);
},
// `skip` implementation for generators.
skip: function(n) {
var skip = function(ctr, g) {
return (ctr <= 0) ? g : skip(ctr - 1, g.tail());
};
return trampoline(skip, n, this);
},
// `map` implementation for generators.
map: function(fn, gen) {
var g = create(G.prototype);
g[0] = fn(gen[0]);
g.tail = function() { return this.map(fn, gen.tail()); };
return g;
},
// `filter` implementation for generators.
filter: function(fn) {
var gen = this, head = gen[0];
while (!fn(head)) {
gen = gen.tail();
head = gen[0];
}
var g = create(G.prototype);
g[0] = head;
g.tail = function() {return filter(fn, gen.tail());};
return g;
}
};
// The actual public `generator` function.
return function(seed, current, step) {
return new G(seed, current, step);
};
}());
// Function functions :-)
// ----------------------
//
// These functions make new functions out of old ones.
// --------
// Creates a new function that runs each of the functions supplied as parameters in turn, passing the output
// of each one to the next one, starting with whatever arguments were passed to the initial invocation.
// Note that if `var h = compose(f, g)`, `h(x)` calls `g(x)` first, passing the result of that to `f()`.
var compose = R.compose = function() { // TODO: type check of arguments?
var fns = slice(arguments);
return function() {
return foldr(function(fn, args) {return [fn.apply(this, args)];}, slice(arguments), fns)[0];
};
};
aliasFor("compose").is("fog"); // TODO: really?
// Similar to `compose`, but processes the functions in the reverse order so that if if `var h = pipe(f, g)`,
// `h(x)` calls `f(x)` first, passing the result of that to `g()`.
R.pipe = function() { // TODO: type check of arguments?
return compose.apply(this, slice(arguments).reverse());
};
aliasFor("pipe").is("sequence");
// Returns a new function much like the supplied one except that the first two arguments are inverted.
var flip = R.flip = function(fn) {
return _(function(a, b) {
return fn.apply(this, [b, a].concat(slice(arguments, 2)));
});
};
// Creates a new function that acts like the supplied function except that the left-most parameters are
// pre-filled.
R.lPartial = function (fn) {
var args = slice(arguments, 1);
return arity(Math.max(fn.length - args.length, 0), function() {
return fn.apply(this, args.concat(slice(arguments)));
});
};
aliasFor("lPartial").is("applyLeft");
// Creates a new function that acts like the supplied function except that the right-most parameters are
// pre-filled.
R.rPartial =function (fn) {
var args = slice(arguments, 1);
return arity(Math.max(fn.length - args.length, 0), function() {
return fn.apply(this, slice(arguments).concat(args));
});
};
aliasFor("rPartial").is("applyRight");
// Creates a new function that stores the results of running the supplied function and returns those
// stored value when the same request is made. **Note**: this really only handles string and number parameters.
R.memoize = function(fn) {
var cache = {};
return function() {
var position = foldl(function(cache, arg) {return cache[arg] || (cache[arg] = {});}, cache,
slice(arguments, 0, arguments.length - 1));
var arg = arguments[arguments.length - 1];
return (position[arg] || (position[arg] = fn.apply(this, arguments)));
};
};
// Wraps a function up in one that will only call the internal one once, no matter how many times the outer one
// is called. ** Note**: this is not really pure; it's mostly meant to keep side-effects from repeating.
R.once = function(fn) {
var called = false, result;
return function() {
if (called) {return result;}
called = true;
return (result = fn.apply(this, arguments));
};
};
// Wrap a function inside another to allow you to make adjustments to the parameters or do other processing
// either before the internal function is called or with its results.
R.wrap = function(fn, wrapper) {
return function() {
return wrapper.apply(this, [fn].concat(slice(arguments)));
};
};
// Wraps a constructor function inside a (curried) plain function that can be called with the same arguments
// and returns the same type. Allows, for instance,
//
// var Widget = function(config) { /* ... */ }; // Constructor
// Widget.prototype = { /* ... */ }
// map(construct(Widget), allConfigs); //=> list of Widgets
R.construct = function(fn) {
var f = function() {
var obj = new fn();
fn.apply(obj, arguments);
return obj;
};
return fn.length > 1 ? _(nAry(fn.length, f)) : f;
};
// List Functions
// --------------
//
// These functions operate on logical lists, here plain arrays. Almost all of these are curried, and the list
// parameter comes last, so you can create a new function by supplying the preceding arguments, leaving the
// list parameter off. For instance:
//
// // skip third parameter
// var checkAllPredicates = reduce(andFn, alwaysTrue);
// // ... given suitable definitions of odd, lt20, gt5
// var test = checkAllPredicates([odd, lt20, gt5]);
// // test(7) => true, test(9) => true, test(10) => false,
// // test(3) => false, test(21) => false,
// --------
// Returns a single item, by successively calling the function with the current element and the the next
// element of the list, passing the result to the next call. We start with the `acc` parameter to get
// things going. The function supplied should accept this running value and the latest element of the list,
// and return an updated value.
var foldl = R.foldl = _(function(fn, acc, list) {
var idx = -1, len = list.length;
while(++idx < len) {
acc = fn(acc, list[idx]);
}
return acc;
});
aliasFor("foldl").is("reduce");
// Much like `foldl`/`reduce`, except that this takes as its starting value the first element in the list.
R.foldl1 = _(function (fn, list) {
if (isEmpty(list)) {
throw new Error("foldl1 does not work on empty lists");
}
return foldl(fn, head(list), tail(list));
});
// Similar to `foldl`/`reduce` except that it moves from right to left on the list.
var foldr = R.foldr =_(function(fn, acc, list) {
var idx = list.length;
while(idx--) {
acc = fn(list[idx], acc);
}
return acc;
});
aliasFor("foldr").is("reduceRight");
// Much like `foldr`/`reduceRight`, except that this takes as its starting value the last element in the list.
R.foldr1 = _(function (fn, list) {
if (isEmpty(list)) {
throw new Error("foldr1 does not work on empty lists");
}
var newList = clone(list), acc = newList.pop();
return foldr(fn, acc, newList);
});
// Builds a list from a seed value, using a function that returns falsy to quit and a pair otherwise,
// consisting of the current value and the seed to be used for the next value.
R.unfoldr = _(function(fn, seed) {
var pair = fn(seed), result = [];
while (pair && pair.length) {
result.push(pair[0]);
pair = fn(pair[1]);
}
return result;
});
// Returns a new list constructed by applying the function to every element of the list supplied.
var map = R.map = _(function(fn, list) {
if (list && list.length === Infinity) {
return list.map(fn, list);
}
var idx = -1, len = list.length, result = new Array(len);
while (++idx < len) {
result[idx] = fn(list[idx]);
}
return result;
});
// Reports the number of elements in the list
R.size = function(arr) {return arr.length;};
// Returns a new list containing only those items that match a given predicate function.
var filter = R.filter = _(function(fn, list) {
if (list && list.length === Infinity) {
return list.filter(fn);
}
var idx = -1, len = list.length, result = [];
while (++idx < len) {
if (fn(list[idx])) {
result.push(list[idx]);
}
}
return result;
});
// Similar to `filter`, except that it keeps only those that **don't** match the given predicate functions.
R.reject = _(function(fn, list) {
return filter(notFn(fn), list);
});
// Returns a new list containing the elements of the given list up until the first one where the function
// supplied returns `false` when passed the element.
R.takeWhile = _(function(fn, list) {
var idx = -1, len = list.length, taking = true, result = [];
while (taking) {
++idx;
if (idx < len && fn(list[idx])) {
result.push(list[idx]);
} else {
taking = false;
}
}
return result;
});
// Returns a new list containing the first `n` elements of the given list.
R.take = _(function(n, list) {
if (list && list.length === Infinity) {
return list.take(n);
}
var ls = clone(list);
ls.length = n;
return ls;
});
// Returns a new list containing the elements of the given list starting with the first one where the function
// supplied returns `false` when passed the element.
R.skipUntil = _(function(fn, list) {
var idx = -1, len = list.length, taking = false, result = [];
while (!taking) {
++idx;
if (idx >= len || fn(list[idx])) {
taking = true;
}
}
while (idx < len) {
result.push(list[idx++]);
}
return result;
});
// Returns a new list containing all **but** the first `n` elements of the given list.
R.skip = _(function(n, list) {
if (list && list.length === Infinity) {
return list.skip(n);
}
return slice(list, n);
});
aliasFor('skip').is('drop');
// Returns the first element of the list which matches the predicate, or `false` if no element matches.
R.find = _(function(fn, list) {
var idx = -1, len = list.length;
while (++idx < len) {
if (fn(list[idx])) {
return list[idx];
}
}
return false;
});
// Returns `true` if all elements of the list match the predicate, `false` if there are any that don't.
var all = R.all = _(function (fn, list) {
var i = -1;
while (++i < list.length) {
if (!fn(list[i])) {
return false;
}
}
return true;
});
aliasFor("all").is("every");
// Returns `true` if any elements of the list match the predicate, `false` if none do.
var any = R.any = _(function(fn, list) {
var i = -1;
while (++i < list.length) {
if (fn(list[i])) {
return true;
}
}
return false;
});
aliasFor("any").is("some");
// Returns `true` if the list contains the sought element, `false` if it does not. Equality is strict here,
// meaning reference equality for objects and non-coercing equality for primitives.
var contains = R.contains = _(function(a, list) {
return list.indexOf(a) > -1;
});
// Returns `true` if the list contains the sought element, `false` if it does not, based upon the value
// returned by applying the supplied predicated to two list elements. Equality is strict here, meaning
// reference equality for objects and non-coercing equality for primitives. Probably inefficient.
var containsWith = _(function(pred, x, list) {
var idx = -1, len = list.length;
while (++idx < len) {
if (pred(x, list[idx])) {return true;}
}
return false;
});
// Returns a new list containing only one copy of each element in the original list. Equality is strict here,
// meaning reference equality for objects and non-coercing equality for primitives.
var uniq = R.uniq = function(list) {
return foldr(function(x, acc) { return (contains(x, acc)) ? acc : prepend(x, acc); }, EMPTY, list);
};
// Returns a new list containing only one copy of each element in the original list, based upon the value
// returned by applying the supplied predicate to two list elements. Equality is strict here, meaning
// reference equality for objects and non-coercing equality for primitives.
var uniqWith = _(function(pred, list) {
return foldr(function(x, acc) {return (containsWith(pred, x, acc)) ? acc : prepend(x, acc); }, EMPTY, list);
});
// Returns a new list by plucking the same named property off all objects in the list supplied.
var pluck = R.pluck = _(function(p, list) {return map(prop(p), list);});
// Returns a list that contains a flattened version of the supplied list. For example:
//
// flatten([1, 2, [3, 4], 5, [6, [7, 8, [9, [10, 11], 12]]]]);
// // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var flatten = R.flatten = function(list) {
var idx = -1, len = list ? list.length : 0, result = [], push = result.push, val;
while (++idx < len) {
val = list[idx];
push.apply(result, isArray(val) ? flatten(val) : [val]);
}
return result;
};
// Creates a new list out of the two supplied by applying the function to each equally-positioned pair in the
// lists. For example,
//
// zipWith(f, [1, 2, 3], ['a', 'b', 'c'])
// // => [f(1, 'a'), f(2, 'b'), f(3, 'c')];
//
// Note that the output list will only be as long as the length os the first list passed in.
R.zipWith = _(function(fn, a, b) {
var rv = [], i = -1, len = a.length;
while(++i < len) {
rv[i] = fn(a[i], b[i]);
}
return rv;
});
// Creates a new list out of the two supplied by yielding the pair of each equally-positioned pair in the
// lists. For example,
//
// zip([1, 2, 3], ['a', 'b', 'c'])
// // => [[1, 'a'], [2, 'b'], [3, 'c']];
R.zip = _(function(a, b) { // = zipWith(prepend);
var rv = [], i = -1, len = a.length;
while (++i < len) {
rv[i] = [a[i], b[i]];
}
return rv;
});
// Creates a new list out of the two supplied by applying the function to each possible pair in the lists.
// For example,
//
// xProdWith(f, [1, 2], ['a', 'b'])
// // => [f(1, 'a'), f(1, 'b'), f(2, 'a'), f(2, 'b')];
R.xprodWith = _(function(fn, a, b) {
if (isEmpty(a) || isEmpty(b)) {return EMPTY;}
var i = -1, ilen = a.length, j, jlen = b.length, result = []; // better to push them all or to do `new Array(ilen * jlen)` and calculate indices?
while (++i < ilen) {
j = -1;
while (++j < jlen) {
result.push(fn(a[i], b[j]));
}
}
return result;
});
// Creates a new list out of the two supplied by yielding the pair of each possible pair in the lists.
// For example,
//
// xProd([1, 2], ['a', 'b'])
// // => [[1, 'a'], [1, 'b')], [2, 'a'], [2, 'b']];
R.xprod = _(function(a, b) { // = xprodWith(prepend); (takes about 3 times as long...)
if (isEmpty(a) || isEmpty(b)) {return EMPTY;}
var i = -1, ilen = a.length, j, jlen = b.length, result = []; // better to push them all or to do `new Array(ilen * jlen)` and calculate indices?
while (++i < ilen) {
j = -1;
while (++j < jlen) {
result.push([a[i], b[j]]);
}
}
return result;
});
// Returns a new list with the same elements as the original list, just in the reverse order.
R.reverse = function(list) {
return clone(list || []).reverse();
};
// // Returns a list of numbers from `from` (inclusive) to `to` (exclusive).
// For example,
//
// range(1, 5) // => [1, 2, 3, 4]
// range(50, 53) // => [50, 51, 52]
R.range = _(function(from, to) {
if (from >= to) {return EMPTY;}
var idx, result = new Array(to - from);
for (idx = 0; from < to; idx++, from++) {
result[idx] = from;
}
return result;
});
// Returns the first zero-indexed position of an object in a flat list
R.indexOf = _(function(obj, list) {
return list.indexOf(obj);
});
// Returns the last zero-indexed position of an object in a flat list
R.lastIndexOf = _(function(obj, list) {
return list.lastIndexOf(obj);
});
// Returns the elements of the list as a string joined by a separator.
R.join = _(function(sep, list) {
return list.join(sep);
});
// ramda.splice has a different contract than Array.splice. Array.splice mutates its array
// and returns the removed elements. ramda.splice does not mutate the passed in list (well,
// it makes a shallow copy), and returns a new list with the specified elements removed.
R.splice = _(function(start, len, list) {
var ls = slice(list, 0);
ls.splice(start, len);
return ls;
});
// Returns the nth element of a list (zero-indexed)
R.nth = _(function(n, list) {
return (list[n] === undef) ? null : list[n];
});
// Makes a comparator function out of a function that reports whether the first element is less than the second.
//
// var cmp = comparator(function(a, b) {
// return a.age < b.age;
// };
// sort(cmp, people);
var comparator = R.comparator = function(pred) {
return function(a, b) {
return pred(a, b) ? -1 : pred(b, a) ? 1 : 0;
};
};
// Returns a copy of the list, sorted according to the comparator function, which should accept two values at a
// time and return a negative number if the first value is smaller, a positive number if it's larger, and zero
// if they are equal. Please note that this is a **copy** of the list. It does not modify the original.
var sort = R.sort = _(function(comparator, list) {
return clone(list).sort(comparator);
});
// Object Functions
// ----------------
//
// These functions operate on plain Javascript object, adding simple functions to test properties on these
// objects. Many of these are of most use in conjunction with the list functions, operating on lists of
// objects.
// --------
// Runs the given function with the supplied object, then returns the object.
R.tap = _(function(x, fn) {
if (typeof fn === "function") {
fn(x);
}
return x;
});
aliasFor("tap").is("K"); // TODO: are we sure? Not necessary, but convenient, IMHO.
// Tests if two items are equal. Equality is strict here, meaning reference equality for objects and
// non-coercing equality for primitives.
R.eq = _(function(a, b) {
return a === b;
});
// Returns a function that when supplied an object returns the indicated property of that object, if it exists.
var prop = R.prop = _(function(p, obj) {return obj[p];});
aliasFor("prop").is("get"); // TODO: are we sure? Matches some other libs, but might want to reserve for other use.
// Returns a function that when supplied an object returns the result of running the indicated function on
// that object, if it has such a function.
R.func = _(function(fn, obj) {return obj[fn].apply(obj, slice(arguments, 2));});
// Returns a function that when supplied a property name returns that property on the indicated object, if it
// exists.
R.props = _(function(obj, prop) {return obj && obj[prop];});
// Returns a function that always returns the given value.
var always = R.always = function(val) {
return function() {return val;};
};
var anyBlanks = any(function(val) {return val === null || val === undef;});
// Returns a function that will only call the indicated function if the correct number of (defined, non-null)
// arguments are supplied, returning `undefined` otherwise.
R.maybe = function (fn) {
return function () {
return (arguments.length === 0 || anyBlanks(expand(arguments, fn.length))) ? undef : fn.apply(this, arguments);
};
};
// Returns a list containing the names of all the enumerable own
// properties of the supplied object.
var keys = R.keys = function (obj) {
var prop, ks = [];
for (prop in obj) {
if (obj.hasOwnProperty(prop)) {
ks.push(prop);
}
}
return ks;
};
// Returns a list of all the enumerable own properties of the supplied object.
R.values = function (obj) {
var prop, vs = [];
for (prop in obj) {
if (obj.hasOwnProperty(prop)) {
vs.push(obj[prop]);
}
}
return vs;
};
var partialCopy = function(test, obj) {
var copy = {};
each(function(key) {if (test(key, obj)) {copy[key] = obj[key];}}, keys(obj));
return copy;
};
// Returns a partial copy of an object containing only the keys specified. If the key does not exist, the
// property is ignored
var pick = R.pick = _(function(names, obj) {
return partialCopy(function(key) {return contains(key, names);}, obj);
});
// Similar to `pick` except that this one includes a `key: undefined` pair for properties that don't exist.
var pickAll = R.pickAll = _(function(names, obj) {
var copy = {};
each(function(name) { copy[name] = obj[name]; }, names);
return copy;
});
// Returns a partial copy of an object omitting the keys specified.
R.omit = _(function(names, obj) {
return partialCopy(function(key) {return !contains(key, names);}, obj);
});
// Logic Functions
// ---------------
//
// These functions are very simple wrappers around the built-in logical operators, useful in building up
// more complex functional forms.
// --------
// A function wrapping the boolean `&&` operator. Note that unlike the underlying operator, though, it
// aways returns `true` or `false`.
R.and = _(function (a, b) {
return !!(a && b);
});
// A function wrapping the boolean `||` operator. Note that unlike the underlying operator, though, it
// aways returns `true` or `false`.
R.or = _(function (a, b) {
return !!(a || b);
});
// A function wrapping the boolean `!` operator. It returns `true` if the parameter is false-y and `false` if
// the parameter is truth-y
R.not = function (a) {
return !a;
};
// A function wrapping calls to the two functions in an `&&` operation, returning `true` or `false`. Note that
// this is short-circuited, meaning that the second function will not be invoked if the first returns a false-y
// value.
R.andFn = _(function(f, g) { // TODO: arity?
return function() {return !!(f.apply(this, arguments) && g.apply(this, arguments));};
});
// A function wrapping calls to the two functions in an `||` operation, returning `true` or `false`. Note that
// this is short-circuited, meaning that the second function will not be invoked if the first returns a truth-y
// value. (Note also that at least Oliver Twist can pronounce this one...)
R.orFn = _(function(f, g) { // TODO: arity?
return function() {return !!(f.apply(this, arguments) || g.apply(this, arguments));};
});
// A function wrapping a call to the given function in a `!` operation. It will return `true` when the
// underlying function would return a false-y value, and `false` when it would return a truth-y one.
var notFn = R.notFn = function (f) {
return function() {return !f.apply(this, arguments);};
};
// Arithmetic Functions