rambda 8.1.0 → 8.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,64 @@
1
+ export function _arity(n, fn){
2
+ switch (n){
3
+ case 0:
4
+ return function (){
5
+ return fn.apply(this, arguments)
6
+ }
7
+ case 1:
8
+ return function (_1){
9
+ return fn.apply(this, arguments)
10
+ }
11
+ case 2:
12
+ return function (_1, _2){
13
+ return fn.apply(this, arguments)
14
+ }
15
+ case 3:
16
+ return function (
17
+ _1, _2, _3
18
+ ){
19
+ return fn.apply(this, arguments)
20
+ }
21
+ case 4:
22
+ return function (
23
+ _1, _2, _3, _4
24
+ ){
25
+ return fn.apply(this, arguments)
26
+ }
27
+ case 5:
28
+ return function (
29
+ _1, _2, _3, _4, _5
30
+ ){
31
+ return fn.apply(this, arguments)
32
+ }
33
+ case 6:
34
+ return function (
35
+ _1, _2, _3, _4, _5, _6
36
+ ){
37
+ return fn.apply(this, arguments)
38
+ }
39
+ case 7:
40
+ return function (
41
+ _1, _2, _3, _4, _5, _6, _7
42
+ ){
43
+ return fn.apply(this, arguments)
44
+ }
45
+ case 8:
46
+ return function (
47
+ _1, _2, _3, _4, _5, _6, _7, _8
48
+ ){
49
+ return fn.apply(this, arguments)
50
+ }
51
+ case 9:
52
+ return function (
53
+ _1, _2, _3, _4, _5, _6, _7, _8, _9
54
+ ){
55
+ return fn.apply(this, arguments)
56
+ }
57
+ default:
58
+ return function (
59
+ _1, _2, _3, _4, _5, _6, _7, _8, _9, _10
60
+ ){
61
+ return fn.apply(this, arguments)
62
+ }
63
+ }
64
+ }
@@ -0,0 +1,21 @@
1
+ export function _concat(set1, set2){
2
+ set1 = set1 || []
3
+ set2 = set2 || []
4
+ let idx
5
+ const len1 = set1.length
6
+ const len2 = set2.length
7
+ const result = []
8
+
9
+ idx = 0
10
+ while (idx < len1){
11
+ result[ result.length ] = set1[ idx ]
12
+ idx += 1
13
+ }
14
+ idx = 0
15
+ while (idx < len2){
16
+ result[ result.length ] = set2[ idx ]
17
+ idx += 1
18
+ }
19
+
20
+ return result
21
+ }
@@ -0,0 +1,23 @@
1
+ import { _concat } from './_internals/utils.js'
2
+ import { curryN } from './curryN.js'
3
+
4
+ export function addIndex(
5
+ originalFunction,
6
+ initialIndexFn = () => 0,
7
+ loopIndexChange = x => x + 1
8
+ ){
9
+ return curryN(originalFunction.length, function (){
10
+ const origFn = arguments[ 0 ]
11
+ const list = arguments[ arguments.length - 1 ]
12
+ let idx = initialIndexFn(list.length)
13
+ const args = Array.prototype.slice.call(arguments, 0)
14
+ args[ 0 ] = function (){
15
+ const result = origFn.apply(this, _concat(arguments, [ idx, list ]))
16
+ idx = loopIndexChange(idx)
17
+
18
+ return result
19
+ }
20
+
21
+ return originalFunction.apply(this, args)
22
+ })
23
+ }
@@ -0,0 +1,9 @@
1
+ import { addIndex } from './addIndex.js'
2
+
3
+ export function addIndexRight(originalFunction){
4
+ return addIndex(
5
+ originalFunction,
6
+ listLength => listLength - 1,
7
+ x => x - 1
8
+ )
9
+ }
package/src/ap.js ADDED
@@ -0,0 +1,7 @@
1
+ export function ap(functions, input){
2
+ if (arguments.length === 1){
3
+ return _inputs => ap(functions, _inputs)
4
+ }
5
+
6
+ return functions.reduce((acc, fn) => [ ...acc, ...input.map(fn) ], [])
7
+ }
@@ -0,0 +1,15 @@
1
+ export function aperture(step, list){
2
+ if (arguments.length === 1){
3
+ return _list => aperture(step, _list)
4
+ }
5
+ if (step > list.length) return []
6
+ let idx = 0
7
+ const limit = list.length - (step - 1)
8
+ const acc = new Array(limit)
9
+ while (idx < limit){
10
+ acc[ idx ] = list.slice(idx, idx + step)
11
+ idx += 1
12
+ }
13
+
14
+ return acc
15
+ }
package/src/applyTo.js ADDED
@@ -0,0 +1,7 @@
1
+ export function applyTo(input, fn){
2
+ if (arguments.length === 1){
3
+ return _fn => applyTo(input, _fn)
4
+ }
5
+
6
+ return fn(input)
7
+ }
package/src/ascend.js ADDED
@@ -0,0 +1,23 @@
1
+ export function createCompareFunction(
2
+ a, b, winner, loser
3
+ ){
4
+ if (a === b) return 0
5
+
6
+ return a < b ? winner : loser
7
+ }
8
+
9
+ export function ascend(
10
+ getFunction, a, b
11
+ ){
12
+ if (arguments.length === 1){
13
+ return (_a, _b) => ascend(
14
+ getFunction, _a, _b
15
+ )
16
+ }
17
+ const aValue = getFunction(a)
18
+ const bValue = getFunction(b)
19
+
20
+ return createCompareFunction(
21
+ aValue, bValue, -1, 1
22
+ )
23
+ }
package/src/binary.js ADDED
@@ -0,0 +1,5 @@
1
+ export function binary(fn){
2
+ if (fn.length <= 2) return fn
3
+
4
+ return (a, b) => fn(a, b)
5
+ }
package/src/call.js ADDED
@@ -0,0 +1 @@
1
+ export const call = (fn, ...inputs) => fn(...inputs)
@@ -0,0 +1,27 @@
1
+ import { reduce } from './reduce.js'
2
+
3
+ export function collectBy(fn, list){
4
+ if (arguments.length === 1){
5
+ return _list => collectBy(fn, _list)
6
+ }
7
+
8
+ const group = reduce(
9
+ (o, x) => {
10
+ const tag = fn(x)
11
+ if (o[ tag ] === undefined){
12
+ o[ tag ] = []
13
+ }
14
+ o[ tag ].push(x)
15
+
16
+ return o
17
+ },
18
+ {},
19
+ list
20
+ )
21
+ const newList = []
22
+ for (const tag in group){
23
+ newList.push(group[ tag ])
24
+ }
25
+
26
+ return newList
27
+ }
@@ -0,0 +1,5 @@
1
+ export function comparator(fn){
2
+ return function (a, b){
3
+ return fn(a, b) ? -1 : fn(b, a) ? 1 : 0
4
+ }
5
+ }
@@ -0,0 +1,33 @@
1
+ import { _arity } from './_internals/_arity.js'
2
+ import { head } from './head.js'
3
+ import { identity } from './identity.js'
4
+ import { reduce } from './reduce.js'
5
+ import { reverse } from './reverse.js'
6
+ import { tail } from './tail.js'
7
+
8
+ export function pipeWith(xf, list){
9
+ if (list.length <= 0){
10
+ return identity
11
+ }
12
+
13
+ const headList = head(list)
14
+ const tailList = tail(list)
15
+
16
+ return _arity(headList.length, function (){
17
+ return reduce(
18
+ function (result, f){
19
+ return xf.call(
20
+ this, f, result
21
+ )
22
+ },
23
+ headList.apply(this, arguments),
24
+ tailList
25
+ )
26
+ })
27
+ }
28
+
29
+ export function composeWith(xf, list){
30
+ if (arguments.length === 1) return _list => composeWith(xf, _list)
31
+
32
+ return pipeWith.apply(this, [ xf, reverse(list) ])
33
+ }
package/src/curryN.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { _arity } from './_internals/_arity.js'
2
+
1
3
  function _curryN(
2
4
  n, cache, fn
3
5
  ){
@@ -25,71 +27,6 @@ function _curryN(
25
27
  }
26
28
  }
27
29
 
28
- function _arity(n, fn){
29
- switch (n){
30
- case 0:
31
- return function (){
32
- return fn.apply(this, arguments)
33
- }
34
- case 1:
35
- return function (_1){
36
- return fn.apply(this, arguments)
37
- }
38
- case 2:
39
- return function (_1, _2){
40
- return fn.apply(this, arguments)
41
- }
42
- case 3:
43
- return function (
44
- _1, _2, _3
45
- ){
46
- return fn.apply(this, arguments)
47
- }
48
- case 4:
49
- return function (
50
- _1, _2, _3, _4
51
- ){
52
- return fn.apply(this, arguments)
53
- }
54
- case 5:
55
- return function (
56
- _1, _2, _3, _4, _5
57
- ){
58
- return fn.apply(this, arguments)
59
- }
60
- case 6:
61
- return function (
62
- _1, _2, _3, _4, _5, _6
63
- ){
64
- return fn.apply(this, arguments)
65
- }
66
- case 7:
67
- return function (
68
- _1, _2, _3, _4, _5, _6, _7
69
- ){
70
- return fn.apply(this, arguments)
71
- }
72
- case 8:
73
- return function (
74
- _1, _2, _3, _4, _5, _6, _7, _8
75
- ){
76
- return fn.apply(this, arguments)
77
- }
78
- case 9:
79
- return function (
80
- _1, _2, _3, _4, _5, _6, _7, _8, _9
81
- ){
82
- return fn.apply(this, arguments)
83
- }
84
- default:
85
- return function (
86
- _1, _2, _3, _4, _5, _6, _7, _8, _9, _10
87
- ){
88
- return fn.apply(this, arguments)
89
- }
90
- }
91
- }
92
-
93
30
  export function curryN(n, fn){
94
31
  if (arguments.length === 1) return _fn => curryN(n, _fn)
95
32
 
package/src/descend.js ADDED
@@ -0,0 +1,17 @@
1
+ import { createCompareFunction } from './ascend.js'
2
+
3
+ export function descend(
4
+ getFunction, a, b
5
+ ){
6
+ if (arguments.length === 1){
7
+ return (_a, _b) => descend(
8
+ getFunction, _a, _b
9
+ )
10
+ }
11
+ const aValue = getFunction(a)
12
+ const bValue = getFunction(b)
13
+
14
+ return createCompareFunction(
15
+ aValue, bValue, 1, -1
16
+ )
17
+ }
package/src/pipe.js CHANGED
@@ -1,72 +1,6 @@
1
+ import { _arity } from './_internals/_arity.js'
1
2
  import { reduceFn } from './reduce.js'
2
3
 
3
- export function _arity(n, fn){
4
- switch (n){
5
- case 0:
6
- return function (){
7
- return fn.apply(this, arguments)
8
- }
9
- case 1:
10
- return function (a0){
11
- return fn.apply(this, arguments)
12
- }
13
- case 2:
14
- return function (a0, a1){
15
- return fn.apply(this, arguments)
16
- }
17
- case 3:
18
- return function (
19
- a0, a1, a2
20
- ){
21
- return fn.apply(this, arguments)
22
- }
23
- case 4:
24
- return function (
25
- a0, a1, a2, a3
26
- ){
27
- return fn.apply(this, arguments)
28
- }
29
- case 5:
30
- return function (
31
- a0, a1, a2, a3, a4
32
- ){
33
- return fn.apply(this, arguments)
34
- }
35
- case 6:
36
- return function (
37
- a0, a1, a2, a3, a4, a5
38
- ){
39
- return fn.apply(this, arguments)
40
- }
41
- case 7:
42
- return function (
43
- a0, a1, a2, a3, a4, a5, a6
44
- ){
45
- return fn.apply(this, arguments)
46
- }
47
- case 8:
48
- return function (
49
- a0, a1, a2, a3, a4, a5, a6, a7
50
- ){
51
- return fn.apply(this, arguments)
52
- }
53
- case 9:
54
- return function (
55
- a0, a1, a2, a3, a4, a5, a6, a7, a8
56
- ){
57
- return fn.apply(this, arguments)
58
- }
59
- case 10:
60
- return function (
61
- a0, a1, a2, a3, a4, a5, a6, a7, a8, a9
62
- ){
63
- return fn.apply(this, arguments)
64
- }
65
- default:
66
- throw new Error('First argument to _arity must be a non-negative integer no greater than ten')
67
- }
68
- }
69
-
70
4
  export function _pipe(f, g){
71
5
  return function (){
72
6
  return g.call(this, f.apply(this, arguments))