rubico 2.11.0 → 2.13.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.
- package/README.md +16 -20
- package/Transducer.js +87 -72
- package/__.js +3 -3
- package/_internal/functionArrayAllSeries.js +4 -3
- package/_internal/functionObjectAllSeries.js +65 -0
- package/_internal/genericTransform.js +0 -1
- package/all.js +36 -16
- package/always.js +4 -4
- package/and.js +34 -26
- package/assign.js +5 -5
- package/compose.js +7 -6
- package/curry.js +48 -28
- package/dist/Transducer.es.js +1 -1
- package/dist/Transducer.es.min.js +1 -1
- package/dist/Transducer.js +1 -1
- package/dist/Transducer.min.js +1 -1
- package/dist/Transducer.mjs +1 -1
- package/dist/__.es.js +1 -1
- package/dist/__.es.min.js +1 -1
- package/dist/__.js +1 -1
- package/dist/__.min.js +1 -1
- package/dist/__.mjs +1 -1
- package/dist/all.es.js +112 -8
- package/dist/all.es.min.js +2 -2
- package/dist/all.js +112 -8
- package/dist/all.min.js +2 -2
- package/dist/all.mjs +112 -8
- package/dist/always.es.js +1 -1
- package/dist/always.es.min.js +1 -1
- package/dist/always.js +1 -1
- package/dist/always.min.js +1 -1
- package/dist/always.mjs +1 -1
- package/dist/and.es.js +1 -1
- package/dist/and.es.min.js +1 -1
- package/dist/and.js +1 -1
- package/dist/and.min.js +1 -1
- package/dist/and.mjs +1 -1
- package/dist/assign.es.js +1 -1
- package/dist/assign.es.min.js +1 -1
- package/dist/assign.js +1 -1
- package/dist/assign.min.js +1 -1
- package/dist/assign.mjs +1 -1
- package/dist/compose.es.js +1 -1
- package/dist/compose.es.min.js +1 -1
- package/dist/compose.js +1 -1
- package/dist/compose.min.js +1 -1
- package/dist/compose.mjs +1 -1
- package/dist/curry.es.js +1 -1
- package/dist/curry.es.min.js +1 -1
- package/dist/curry.js +1 -1
- package/dist/curry.min.js +1 -1
- package/dist/curry.mjs +1 -1
- package/dist/eq.es.js +1 -1
- package/dist/eq.es.min.js +1 -1
- package/dist/eq.js +1 -1
- package/dist/eq.min.js +1 -1
- package/dist/eq.mjs +1 -1
- package/dist/every.es.js +1 -1
- package/dist/every.es.min.js +1 -1
- package/dist/every.js +1 -1
- package/dist/every.min.js +1 -1
- package/dist/every.mjs +1 -1
- package/dist/filter.es.js +113 -19
- package/dist/filter.es.min.js +2 -2
- package/dist/filter.js +113 -19
- package/dist/filter.min.js +2 -2
- package/dist/filter.mjs +113 -19
- package/dist/flatMap.es.js +1 -1
- package/dist/flatMap.es.min.js +1 -1
- package/dist/flatMap.js +1 -1
- package/dist/flatMap.min.js +1 -1
- package/dist/flatMap.mjs +1 -1
- package/dist/forEach.es.js +1 -1
- package/dist/forEach.es.min.js +1 -1
- package/dist/forEach.js +1 -1
- package/dist/forEach.min.js +1 -1
- package/dist/forEach.mjs +1 -1
- package/dist/get.es.js +1 -1
- package/dist/get.es.min.js +1 -1
- package/dist/get.js +1 -1
- package/dist/get.min.js +1 -1
- package/dist/get.mjs +1 -1
- package/dist/gt.es.js +1 -1
- package/dist/gt.es.min.js +1 -1
- package/dist/gt.js +1 -1
- package/dist/gt.min.js +1 -1
- package/dist/gt.mjs +1 -1
- package/dist/gte.es.js +1 -1
- package/dist/gte.es.min.js +1 -1
- package/dist/gte.js +1 -1
- package/dist/gte.min.js +1 -1
- package/dist/gte.mjs +1 -1
- package/dist/lt.es.js +1 -1
- package/dist/lt.es.min.js +1 -1
- package/dist/lt.js +1 -1
- package/dist/lt.min.js +1 -1
- package/dist/lt.mjs +1 -1
- package/dist/lte.es.js +1 -1
- package/dist/lte.es.min.js +1 -1
- package/dist/lte.js +1 -1
- package/dist/lte.min.js +1 -1
- package/dist/lte.mjs +1 -1
- package/dist/map.es.js +1 -1
- package/dist/map.es.min.js +1 -1
- package/dist/map.js +1 -1
- package/dist/map.min.js +1 -1
- package/dist/map.mjs +1 -1
- package/dist/not.es.js +1 -1
- package/dist/not.es.min.js +1 -1
- package/dist/not.js +1 -1
- package/dist/not.min.js +1 -1
- package/dist/not.mjs +1 -1
- package/dist/omit.es.js +1 -1
- package/dist/omit.es.min.js +1 -1
- package/dist/omit.js +1 -1
- package/dist/omit.min.js +1 -1
- package/dist/omit.mjs +1 -1
- package/dist/or.es.js +1 -1
- package/dist/or.es.min.js +1 -1
- package/dist/or.js +1 -1
- package/dist/or.min.js +1 -1
- package/dist/or.mjs +1 -1
- package/dist/pick.es.js +1 -1
- package/dist/pick.es.min.js +1 -1
- package/dist/pick.js +1 -1
- package/dist/pick.min.js +1 -1
- package/dist/pick.mjs +1 -1
- package/dist/pipe.es.js +1 -1
- package/dist/pipe.es.min.js +1 -1
- package/dist/pipe.js +1 -1
- package/dist/pipe.min.js +1 -1
- package/dist/pipe.mjs +1 -1
- package/dist/reduce.es.js +1 -1
- package/dist/reduce.es.min.js +1 -1
- package/dist/reduce.js +1 -1
- package/dist/reduce.min.js +1 -1
- package/dist/reduce.mjs +1 -1
- package/dist/rubico.es.js +168 -79
- package/dist/rubico.es.min.js +2 -2
- package/dist/rubico.global.js +168 -79
- package/dist/rubico.global.min.js +2 -2
- package/dist/rubico.js +168 -79
- package/dist/rubico.min.js +2 -2
- package/dist/rubico.mjs +168 -79
- package/dist/set.es.js +1 -1
- package/dist/set.es.min.js +1 -1
- package/dist/set.js +1 -1
- package/dist/set.min.js +1 -1
- package/dist/set.mjs +1 -1
- package/dist/some.es.js +1 -1
- package/dist/some.es.min.js +1 -1
- package/dist/some.js +1 -1
- package/dist/some.min.js +1 -1
- package/dist/some.mjs +1 -1
- package/dist/switchCase.es.js +1 -1
- package/dist/switchCase.es.min.js +1 -1
- package/dist/switchCase.js +1 -1
- package/dist/switchCase.min.js +1 -1
- package/dist/switchCase.mjs +1 -1
- package/dist/tap.es.js +1 -1
- package/dist/tap.es.min.js +1 -1
- package/dist/tap.js +1 -1
- package/dist/tap.min.js +1 -1
- package/dist/tap.mjs +1 -1
- package/dist/thunkify.es.js +1 -1
- package/dist/thunkify.es.min.js +1 -1
- package/dist/thunkify.js +1 -1
- package/dist/thunkify.min.js +1 -1
- package/dist/thunkify.mjs +1 -1
- package/dist/transform.es.js +1 -3
- package/dist/transform.es.min.js +2 -2
- package/dist/transform.js +1 -3
- package/dist/transform.min.js +1 -1
- package/dist/transform.mjs +1 -3
- package/dist/tryCatch.es.js +1 -1
- package/dist/tryCatch.es.min.js +1 -1
- package/dist/tryCatch.js +1 -1
- package/dist/tryCatch.min.js +1 -1
- package/dist/tryCatch.mjs +1 -1
- package/dist/x/append.es.js +1 -1
- package/dist/x/append.es.min.js +1 -1
- package/dist/x/append.js +1 -1
- package/dist/x/append.min.js +1 -1
- package/dist/x/append.mjs +1 -1
- package/dist/x/callProp.es.js +1 -1
- package/dist/x/callProp.es.min.js +1 -1
- package/dist/x/callProp.js +1 -1
- package/dist/x/callProp.min.js +1 -1
- package/dist/x/callProp.mjs +1 -1
- package/dist/x/defaultsDeep.es.js +1 -1
- package/dist/x/defaultsDeep.es.min.js +1 -1
- package/dist/x/defaultsDeep.js +1 -1
- package/dist/x/defaultsDeep.min.js +1 -1
- package/dist/x/defaultsDeep.mjs +1 -1
- package/dist/x/differenceWith.es.js +1 -1
- package/dist/x/differenceWith.es.min.js +1 -1
- package/dist/x/differenceWith.js +1 -1
- package/dist/x/differenceWith.min.js +1 -1
- package/dist/x/differenceWith.mjs +1 -1
- package/dist/x/filterOut.es.js +113 -19
- package/dist/x/filterOut.es.min.js +2 -2
- package/dist/x/filterOut.js +113 -19
- package/dist/x/filterOut.min.js +2 -2
- package/dist/x/filterOut.mjs +113 -19
- package/dist/x/find.es.js +1 -1
- package/dist/x/find.es.min.js +1 -1
- package/dist/x/find.js +1 -1
- package/dist/x/find.min.js +1 -1
- package/dist/x/find.mjs +1 -1
- package/dist/x/findIndex.es.js +1 -1
- package/dist/x/findIndex.es.min.js +1 -1
- package/dist/x/findIndex.js +1 -1
- package/dist/x/findIndex.min.js +1 -1
- package/dist/x/findIndex.mjs +1 -1
- package/dist/x/first.es.js +1 -1
- package/dist/x/first.es.min.js +1 -1
- package/dist/x/first.js +1 -1
- package/dist/x/first.min.js +1 -1
- package/dist/x/first.mjs +1 -1
- package/dist/x/flatten.es.js +1 -1
- package/dist/x/flatten.es.min.js +1 -1
- package/dist/x/flatten.js +1 -1
- package/dist/x/flatten.min.js +1 -1
- package/dist/x/flatten.mjs +1 -1
- package/dist/x/groupBy.es.js +1 -1
- package/dist/x/groupBy.es.min.js +1 -1
- package/dist/x/groupBy.js +1 -1
- package/dist/x/groupBy.min.js +1 -1
- package/dist/x/groupBy.mjs +1 -1
- package/dist/x/has.es.js +1 -1
- package/dist/x/has.es.min.js +1 -1
- package/dist/x/has.js +1 -1
- package/dist/x/has.min.js +1 -1
- package/dist/x/has.mjs +1 -1
- package/dist/x/identity.es.js +1 -1
- package/dist/x/identity.es.min.js +1 -1
- package/dist/x/identity.js +1 -1
- package/dist/x/identity.min.js +1 -1
- package/dist/x/identity.mjs +1 -1
- package/dist/x/includes.es.js +1 -1
- package/dist/x/includes.es.min.js +1 -1
- package/dist/x/includes.js +1 -1
- package/dist/x/includes.min.js +1 -1
- package/dist/x/includes.mjs +1 -1
- package/dist/x/isDeepEqual.es.js +1 -1
- package/dist/x/isDeepEqual.es.min.js +1 -1
- package/dist/x/isDeepEqual.js +1 -1
- package/dist/x/isDeepEqual.min.js +1 -1
- package/dist/x/isDeepEqual.mjs +1 -1
- package/dist/x/isEmpty.es.js +1 -1
- package/dist/x/isEmpty.es.min.js +1 -1
- package/dist/x/isEmpty.js +1 -1
- package/dist/x/isEmpty.min.js +1 -1
- package/dist/x/isEmpty.mjs +1 -1
- package/dist/x/isEqual.es.js +1 -1
- package/dist/x/isEqual.es.min.js +1 -1
- package/dist/x/isEqual.js +1 -1
- package/dist/x/isEqual.min.js +1 -1
- package/dist/x/isEqual.mjs +1 -1
- package/dist/x/isFunction.es.js +1 -1
- package/dist/x/isFunction.es.min.js +1 -1
- package/dist/x/isFunction.js +1 -1
- package/dist/x/isFunction.min.js +1 -1
- package/dist/x/isFunction.mjs +1 -1
- package/dist/x/isIn.es.js +1 -1
- package/dist/x/isIn.es.min.js +1 -1
- package/dist/x/isIn.js +1 -1
- package/dist/x/isIn.min.js +1 -1
- package/dist/x/isIn.mjs +1 -1
- package/dist/x/isObject.es.js +1 -1
- package/dist/x/isObject.es.min.js +1 -1
- package/dist/x/isObject.js +1 -1
- package/dist/x/isObject.min.js +1 -1
- package/dist/x/isObject.mjs +1 -1
- package/dist/x/isString.es.js +1 -1
- package/dist/x/isString.es.min.js +1 -1
- package/dist/x/isString.js +1 -1
- package/dist/x/isString.min.js +1 -1
- package/dist/x/isString.mjs +1 -1
- package/dist/x/keys.es.js +1 -1
- package/dist/x/keys.es.min.js +1 -1
- package/dist/x/keys.js +1 -1
- package/dist/x/keys.min.js +1 -1
- package/dist/x/keys.mjs +1 -1
- package/dist/x/last.es.js +1 -1
- package/dist/x/last.es.min.js +1 -1
- package/dist/x/last.js +1 -1
- package/dist/x/last.min.js +1 -1
- package/dist/x/last.mjs +1 -1
- package/dist/x/maxBy.es.js +1 -1
- package/dist/x/maxBy.es.min.js +1 -1
- package/dist/x/maxBy.js +1 -1
- package/dist/x/maxBy.min.js +1 -1
- package/dist/x/maxBy.mjs +1 -1
- package/dist/x/noop.es.js +1 -1
- package/dist/x/noop.es.min.js +1 -1
- package/dist/x/noop.js +1 -1
- package/dist/x/noop.min.js +1 -1
- package/dist/x/noop.mjs +1 -1
- package/dist/x/pluck.es.js +1 -1
- package/dist/x/pluck.es.min.js +1 -1
- package/dist/x/pluck.js +1 -1
- package/dist/x/pluck.min.js +1 -1
- package/dist/x/pluck.mjs +1 -1
- package/dist/x/prepend.es.js +1 -1
- package/dist/x/prepend.es.min.js +1 -1
- package/dist/x/prepend.js +1 -1
- package/dist/x/prepend.min.js +1 -1
- package/dist/x/prepend.mjs +1 -1
- package/dist/x/size.es.js +1 -1
- package/dist/x/size.es.min.js +1 -1
- package/dist/x/size.js +1 -1
- package/dist/x/size.min.js +1 -1
- package/dist/x/size.mjs +1 -1
- package/dist/x/trace.es.js +1 -1
- package/dist/x/trace.es.min.js +1 -1
- package/dist/x/trace.js +1 -1
- package/dist/x/trace.min.js +1 -1
- package/dist/x/trace.mjs +1 -1
- package/dist/x/unionWith.es.js +1 -1
- package/dist/x/unionWith.es.min.js +1 -1
- package/dist/x/unionWith.js +1 -1
- package/dist/x/unionWith.min.js +1 -1
- package/dist/x/unionWith.mjs +1 -1
- package/dist/x/uniq.es.js +1 -1
- package/dist/x/uniq.es.min.js +1 -1
- package/dist/x/uniq.js +1 -1
- package/dist/x/uniq.min.js +1 -1
- package/dist/x/uniq.mjs +1 -1
- package/dist/x/unless.es.js +1 -1
- package/dist/x/unless.es.min.js +1 -1
- package/dist/x/unless.js +1 -1
- package/dist/x/unless.min.js +1 -1
- package/dist/x/unless.mjs +1 -1
- package/dist/x/values.es.js +1 -1
- package/dist/x/values.es.min.js +1 -1
- package/dist/x/values.js +1 -1
- package/dist/x/values.min.js +1 -1
- package/dist/x/values.mjs +1 -1
- package/dist/x/when.es.js +1 -1
- package/dist/x/when.es.min.js +1 -1
- package/dist/x/when.js +1 -1
- package/dist/x/when.min.js +1 -1
- package/dist/x/when.mjs +1 -1
- package/eq.js +29 -30
- package/es.js +168 -79
- package/every.js +16 -22
- package/filter.js +41 -78
- package/flatMap.js +16 -16
- package/forEach.js +14 -14
- package/get.js +3 -3
- package/gt.js +18 -25
- package/gte.js +22 -29
- package/index.js +168 -79
- package/lt.js +33 -31
- package/lte.js +24 -31
- package/map.js +43 -46
- package/not.js +12 -17
- package/or.js +34 -26
- package/package.json +1 -1
- package/pipe.js +6 -6
- package/reduce.js +62 -69
- package/some.js +13 -36
- package/switchCase.js +9 -9
- package/tap.js +7 -12
- package/thunkify.js +42 -12
- package/transform.js +62 -61
- package/tryCatch.js +4 -7
- package/_internal/add.js +0 -12
package/reduce.js
CHANGED
|
@@ -23,24 +23,44 @@ const _reduce = function (collection, reducer, initial) {
|
|
|
23
23
|
* ```coffeescript [specscript]
|
|
24
24
|
* type Foldable = Array|Set|Map|Generator|AsyncGenerator|{ reduce: function }|Object
|
|
25
25
|
*
|
|
26
|
-
* type
|
|
26
|
+
* type Reducer = (
|
|
27
27
|
* accumulator any,
|
|
28
|
-
*
|
|
28
|
+
* item any,
|
|
29
29
|
* indexOrKey number|string|any,
|
|
30
30
|
* foldable Foldable
|
|
31
31
|
* )=>(nextAccumulator Promise|any)
|
|
32
32
|
*
|
|
33
|
-
* type
|
|
33
|
+
* type Resolver = any=>Promise|any
|
|
34
34
|
*
|
|
35
|
-
* reducer
|
|
36
|
-
*
|
|
35
|
+
* reduce(foldable Promise|Foldable, reducer Reducer) -> accumulator Promise|any
|
|
36
|
+
* reduce(foldable Promise|Foldable, reducer Reducer, initialValue Promise|any) -> accumulator Promise|any
|
|
37
|
+
* reduce(foldable Promise|Foldable, reducer Reducer, initialResolver Resolver) -> accumulator Promise|any
|
|
37
38
|
*
|
|
38
|
-
* reduce(foldable
|
|
39
|
-
* reduce(reducer,
|
|
39
|
+
* reduce(reducer Reducer)(foldable Foldable) -> accumulator Promise|any
|
|
40
|
+
* reduce(reducer Reducer, initialValue Promise|any)(foldable Foldable) -> accumulator Promise|any
|
|
41
|
+
* reduce(reducer Reducer, initialResolver Resolver)(foldable Foldable) -> accumulator Promise|any
|
|
40
42
|
* ```
|
|
41
43
|
*
|
|
42
44
|
* @description
|
|
43
|
-
* Reduces a foldable to
|
|
45
|
+
* Reduces a foldable to an accumulated value.
|
|
46
|
+
*
|
|
47
|
+
* ```javascript [playground]
|
|
48
|
+
* const max = (a, b) => a > b ? a : b
|
|
49
|
+
*
|
|
50
|
+
* const result = reduce([1, 3, 5, 4, 2], max)
|
|
51
|
+
*
|
|
52
|
+
* console.log(result)
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* `reduce` executes a reducer function for each item of a foldable in order. If an initial value is provided, `reduce` starts iterating from the first item of the foldable. If no initial value is provided, `reduce` uses the first item of the foldable as the initial value and starts iterating from the second item of the foldable.
|
|
56
|
+
*
|
|
57
|
+
* ```javascript [playground]
|
|
58
|
+
* const add = (a, b) => a + b
|
|
59
|
+
*
|
|
60
|
+
* const result = reduce([1, 2, 3, 4, 5], add, 10)
|
|
61
|
+
*
|
|
62
|
+
* console.log(result)
|
|
63
|
+
* ```
|
|
44
64
|
*
|
|
45
65
|
* The following data types are considered to be foldables:
|
|
46
66
|
* * `array`
|
|
@@ -51,11 +71,11 @@ const _reduce = function (collection, reducer, initial) {
|
|
|
51
71
|
* * `object with .reduce method`
|
|
52
72
|
* * `object`
|
|
53
73
|
*
|
|
54
|
-
* The reducing operation is
|
|
74
|
+
* The reducing operation is expressed by the reducer function and optional initial value, which defines a transformation between an accumulator and a given item of the foldable.
|
|
55
75
|
*
|
|
56
76
|
* ```javascript
|
|
57
|
-
* const reducer = function (accumulator,
|
|
58
|
-
* // nextAccumulator is the result of some operation between accumulator and
|
|
77
|
+
* const reducer = function (accumulator, item) {
|
|
78
|
+
* // nextAccumulator is the result of some operation between accumulator and item
|
|
59
79
|
* // and becomes the accumulator for the next iteration and invocation of the reducer
|
|
60
80
|
* return nextAccumulator
|
|
61
81
|
* }
|
|
@@ -67,7 +87,7 @@ const _reduce = function (collection, reducer, initial) {
|
|
|
67
87
|
* ```coffeescript [specscript]
|
|
68
88
|
* reducer(
|
|
69
89
|
* accumulator any,
|
|
70
|
-
*
|
|
90
|
+
* item any,
|
|
71
91
|
* index number,
|
|
72
92
|
* fold Array
|
|
73
93
|
* ) -> nextAccumulator Promise|any
|
|
@@ -77,7 +97,7 @@ const _reduce = function (collection, reducer, initial) {
|
|
|
77
97
|
* ```coffeescript [specscript]
|
|
78
98
|
* reducer(
|
|
79
99
|
* accumulator any,
|
|
80
|
-
*
|
|
100
|
+
* item any
|
|
81
101
|
* ) -> nextAccumulator Promise|any
|
|
82
102
|
* ```
|
|
83
103
|
*
|
|
@@ -85,7 +105,7 @@ const _reduce = function (collection, reducer, initial) {
|
|
|
85
105
|
* ```coffeescript [specscript]
|
|
86
106
|
* reducer(
|
|
87
107
|
* accumulator any,
|
|
88
|
-
*
|
|
108
|
+
* item any,
|
|
89
109
|
* key any,
|
|
90
110
|
* fold Map
|
|
91
111
|
* ) -> nextAccumulator Promise|any
|
|
@@ -95,7 +115,7 @@ const _reduce = function (collection, reducer, initial) {
|
|
|
95
115
|
* ```coffeescript [specscript]
|
|
96
116
|
* reducer(
|
|
97
117
|
* accumulator any,
|
|
98
|
-
*
|
|
118
|
+
* item any
|
|
99
119
|
* ) -> nextAccumulator Promise|any
|
|
100
120
|
* ```
|
|
101
121
|
*
|
|
@@ -103,47 +123,30 @@ const _reduce = function (collection, reducer, initial) {
|
|
|
103
123
|
* ```coffeescript [specscript]
|
|
104
124
|
* reducer(
|
|
105
125
|
* accumulator any,
|
|
106
|
-
*
|
|
126
|
+
* item any
|
|
107
127
|
* ) -> nextAccumulator Promise|any
|
|
108
128
|
* ```
|
|
109
129
|
*
|
|
110
|
-
* If the foldable is an object with a `.reduce` method, the reducer function signature is defined externally.
|
|
111
|
-
*
|
|
112
130
|
* If the foldable is a plain object:
|
|
113
131
|
* ```coffeescript [specscript]
|
|
114
132
|
* reducer(
|
|
115
133
|
* accumulator any,
|
|
116
|
-
*
|
|
134
|
+
* item any,
|
|
117
135
|
* key string,
|
|
118
136
|
* fold Object
|
|
119
137
|
* ) -> nextAccumulator Promise|any
|
|
120
138
|
* ```
|
|
121
139
|
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
* ```javascript [playground]
|
|
125
|
-
* const max = (a, b) => a > b ? a : b
|
|
126
|
-
*
|
|
127
|
-
* const result = reduce([1, 3, 5, 4, 2], max)
|
|
128
|
-
* console.log(result) // 5
|
|
129
|
-
* ```
|
|
130
|
-
*
|
|
131
|
-
* If an initial value is provided, the accumulator starts as the initial value rather than the first element of the foldable.
|
|
132
|
-
*
|
|
133
|
-
* ```javascript [playground]
|
|
134
|
-
* const add = (a, b) => a + b
|
|
135
|
-
*
|
|
136
|
-
* const result = reduce([1, 2, 3, 4, 5], add, 0)
|
|
137
|
-
* console.log(result) // 15
|
|
138
|
-
* ```
|
|
140
|
+
* If the foldable is an object with a `.reduce` method, the reducer function signature is defined externally.
|
|
139
141
|
*
|
|
140
142
|
* If the reducer is asynchronous, all promises created by the reducer are resolved before continuing with the reducing operation.
|
|
141
143
|
*
|
|
142
144
|
* ```javascript [playground]
|
|
143
145
|
* const asyncAdd = async (a, b) => a + b
|
|
144
146
|
*
|
|
145
|
-
* const
|
|
146
|
-
*
|
|
147
|
+
* const result = await reduce([1, 2, 3, 4, 5], asyncAdd, 0)
|
|
148
|
+
*
|
|
149
|
+
* console.log(result)
|
|
147
150
|
* ```
|
|
148
151
|
*
|
|
149
152
|
* If the initialization parameter is a function, it is treated as a resolver of the initial value and called with the foldable.
|
|
@@ -151,63 +154,53 @@ const _reduce = function (collection, reducer, initial) {
|
|
|
151
154
|
* ```javascript [playground]
|
|
152
155
|
* const concatSquares = (array, value) => array.concat(value ** 2)
|
|
153
156
|
*
|
|
154
|
-
* const contrivedInitializer = array => [`initial length ${array.length}`]
|
|
155
|
-
*
|
|
156
157
|
* const array = [1, 2, 3, 4, 5]
|
|
157
158
|
*
|
|
158
|
-
*
|
|
159
|
-
* // ['initial length 5', 1, 4, 9, 16, 25]
|
|
160
|
-
* ```
|
|
161
|
-
*
|
|
162
|
-
* For objects, `reduce` iterates over just the values.
|
|
163
|
-
*
|
|
164
|
-
* ```javascript [playground]
|
|
165
|
-
* const add = (a, b) => a + b
|
|
166
|
-
*
|
|
167
|
-
* const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }
|
|
159
|
+
* const result = reduce(array, concatSquares, () => [])
|
|
168
160
|
*
|
|
169
|
-
*
|
|
170
|
-
* console.log(result) // 15
|
|
161
|
+
* console.log(result)
|
|
171
162
|
* ```
|
|
172
163
|
*
|
|
173
|
-
*
|
|
164
|
+
* `reduce` iterates over just the values of objects and maps.
|
|
174
165
|
*
|
|
175
166
|
* ```javascript [playground]
|
|
176
167
|
* const add = (a, b) => a + b
|
|
177
168
|
*
|
|
178
|
-
* const
|
|
169
|
+
* const object = { a: 1, b: 2, c: 3, d: 4, e: 5 }
|
|
170
|
+
* const map = new Map([['a', 1], ['b', 2], ['c', 3], ['d', 4], ['e', 5]])
|
|
179
171
|
*
|
|
180
|
-
* const
|
|
181
|
-
*
|
|
172
|
+
* const objectResult = reduce(object, add)
|
|
173
|
+
* const mapResult = reduce(map, add)
|
|
174
|
+
*
|
|
175
|
+
* console.log(objectResult)
|
|
176
|
+
* console.log(mapResult)
|
|
182
177
|
* ```
|
|
183
178
|
*
|
|
184
|
-
* `reduce`
|
|
179
|
+
* `reduce` reduces async generators.
|
|
185
180
|
*
|
|
186
181
|
* ```javascript [playground]
|
|
187
|
-
* const
|
|
182
|
+
* const add = (a, b) => a + b
|
|
188
183
|
*
|
|
189
|
-
* const
|
|
184
|
+
* const generateAsyncNumbers = async function* () {
|
|
190
185
|
* yield 1; yield 2; yield 3; yield 4; yield 5
|
|
191
186
|
* }
|
|
192
187
|
*
|
|
193
|
-
* reduce(
|
|
188
|
+
* const result = await reduce(generateAsyncNumbers(), add)
|
|
189
|
+
*
|
|
190
|
+
* console.log(result)
|
|
194
191
|
* ```
|
|
195
192
|
*
|
|
196
|
-
*
|
|
193
|
+
* If the foldable or initial value is a promise, it is resolved for its value before further execution for the eager interface only.
|
|
197
194
|
*
|
|
198
195
|
* ```javascript [playground]
|
|
199
196
|
* const add = (a, b) => a + b
|
|
200
197
|
*
|
|
201
|
-
* reduce(Promise.resolve([1, 2, 3, 4, 5]), add, 0)
|
|
202
|
-
* ```
|
|
203
|
-
*
|
|
204
|
-
* Any promises passed for the initial value are also resolved before further execution.
|
|
198
|
+
* const resultWithPromiseFoldable = await reduce(Promise.resolve([1, 2, 3, 4, 5]), add, 0)
|
|
205
199
|
*
|
|
206
|
-
*
|
|
207
|
-
* const add = (a, b) => a + b
|
|
200
|
+
* const resultWithPromiseInitialValue = await reduce([1, 2, 3, 4, 5], add, Promise.resolve(0))
|
|
208
201
|
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
202
|
+
* console.log(resultWithPromiseFoldable)
|
|
203
|
+
* console.log(resultWithPromiseInitialValue)
|
|
211
204
|
* ```
|
|
212
205
|
*
|
|
213
206
|
* See also:
|
package/some.js
CHANGED
|
@@ -41,23 +41,25 @@ const _some = function (collection, predicate) {
|
|
|
41
41
|
* @synopsis
|
|
42
42
|
* ```coffeescript [specscript]
|
|
43
43
|
* type Foldable = Array|Set|Map|Generator|AsyncGenerator|{ reduce: function }|Object
|
|
44
|
-
* type
|
|
44
|
+
* type Predicate = any=>Promise|boolean
|
|
45
45
|
*
|
|
46
|
-
* predicate
|
|
46
|
+
* predicate Predicate
|
|
47
47
|
*
|
|
48
|
-
* some(foldable Foldable, predicate) -> Promise|boolean
|
|
49
|
-
* some(predicate)(foldable Foldable) -> Promise|boolean
|
|
48
|
+
* some(foldable Promise|Foldable, predicate) -> testResult Promise|boolean
|
|
49
|
+
* some(predicate)(foldable Foldable) -> testResult Promise|boolean
|
|
50
50
|
* ```
|
|
51
51
|
*
|
|
52
52
|
* @description
|
|
53
|
-
*
|
|
53
|
+
* Tests a predicate concurrently across all items of a foldable. Returns true if any item tests true by the predicate.
|
|
54
54
|
*
|
|
55
55
|
* ```javascript [playground]
|
|
56
56
|
* const isOdd = number => number % 2 == 1
|
|
57
57
|
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
58
|
+
* const array = [1, 2, 3, 4, 5]
|
|
59
|
+
*
|
|
60
|
+
* const arrayHasOddNumbers = some(array, isOdd)
|
|
61
|
+
*
|
|
62
|
+
* console.log(arrayHasOddNumbers)
|
|
61
63
|
* ```
|
|
62
64
|
*
|
|
63
65
|
* The following data types are considered to be foldables:
|
|
@@ -69,44 +71,19 @@ const _some = function (collection, predicate) {
|
|
|
69
71
|
* * `object with .reduce method`
|
|
70
72
|
* * `object`
|
|
71
73
|
*
|
|
72
|
-
* `some` works for async generators.
|
|
73
|
-
*
|
|
74
|
-
* ```javascript [playground]
|
|
75
|
-
* const toTodosUrl = id => 'https://jsonplaceholder.typicode.com/todos/' + id
|
|
76
|
-
*
|
|
77
|
-
* const fetchedToJson = fetched => fetched.json()
|
|
78
|
-
*
|
|
79
|
-
* const fetchTodo = pipe([
|
|
80
|
-
* toTodosUrl,
|
|
81
|
-
* fetch,
|
|
82
|
-
* fetchedToJson,
|
|
83
|
-
* ])
|
|
84
|
-
*
|
|
85
|
-
* const todoIDsGenerator = async function* () {
|
|
86
|
-
* yield 1; yield 2; yield 3; yield 4; yield 5
|
|
87
|
-
* }
|
|
88
|
-
*
|
|
89
|
-
* const promise = some(todoIDsGenerator(), async id => {
|
|
90
|
-
* const todo = await fetchTodo(id)
|
|
91
|
-
* return todo.title.startsWith('fugiat')
|
|
92
|
-
* })
|
|
93
|
-
*
|
|
94
|
-
* promise.then(console.log) // true
|
|
95
|
-
* ```
|
|
96
|
-
*
|
|
97
74
|
* `some` supports a lazy interface for composability.
|
|
98
75
|
*
|
|
99
76
|
* ```javascript [playground]
|
|
100
77
|
* pipe([1, 2, 3], [
|
|
101
78
|
* some(number => number < 5),
|
|
102
|
-
* console.log,
|
|
79
|
+
* console.log,
|
|
103
80
|
* ])
|
|
104
81
|
* ```
|
|
105
82
|
*
|
|
106
|
-
*
|
|
83
|
+
* If the foldable is a promise, it is resolved for its value before further execution for the eager interface only.
|
|
107
84
|
*
|
|
108
85
|
* ```javascript [playground]
|
|
109
|
-
* some(Promise.resolve([1, 2, 3, 4, 5]), n => n > 6).then(console.log)
|
|
86
|
+
* some(Promise.resolve([1, 2, 3, 4, 5]), n => n > 6).then(console.log)
|
|
110
87
|
* ```
|
|
111
88
|
*
|
|
112
89
|
* See also:
|
package/switchCase.js
CHANGED
|
@@ -12,15 +12,15 @@ const curryArgs3 = require('./_internal/curryArgs3')
|
|
|
12
12
|
*
|
|
13
13
|
* @synopsis
|
|
14
14
|
* ```coffeescript [specscript]
|
|
15
|
-
* type
|
|
16
|
-
* type
|
|
15
|
+
* type Predicate = (...arguments)=>Promise|boolean|any
|
|
16
|
+
* type Function = (...arguments)=>Promise|any
|
|
17
17
|
*
|
|
18
18
|
* conditionalValues Array<Promise|boolean|any>
|
|
19
|
-
* conditionalFunctionsOrValues Array<
|
|
19
|
+
* conditionalFunctionsOrValues Array<Predicate|Function|Promise|boolean|any>
|
|
20
20
|
*
|
|
21
|
-
* switchCase(conditionalValues) -> Promise|any
|
|
22
|
-
* switchCase(...arguments, conditionalFunctionsOrValues) -> Promise|any
|
|
23
|
-
* switchCase(conditionalFunctionsOrValues)(...arguments) -> Promise|any
|
|
21
|
+
* switchCase(conditionalValues) -> result Promise|any
|
|
22
|
+
* switchCase(...arguments, conditionalFunctionsOrValues) -> result Promise|any
|
|
23
|
+
* switchCase(conditionalFunctionsOrValues)(...arguments) -> result Promise|any
|
|
24
24
|
* ```
|
|
25
25
|
*
|
|
26
26
|
* @description
|
|
@@ -44,7 +44,7 @@ const curryArgs3 = require('./_internal/curryArgs3')
|
|
|
44
44
|
* const fruitsGuesser = switchCase([
|
|
45
45
|
* fruit => fruit.color == 'yellow',
|
|
46
46
|
* fruit => fruit.name + ' is possibly a banana',
|
|
47
|
-
* fruit => fruit.name + ' is
|
|
47
|
+
* fruit => fruit.name + ' is possibly not a banana',
|
|
48
48
|
* ])
|
|
49
49
|
*
|
|
50
50
|
* const guess1 = fruitsGuesser({ name: 'plantain', color: 'yellow' })
|
|
@@ -91,13 +91,13 @@ const curryArgs3 = require('./_internal/curryArgs3')
|
|
|
91
91
|
* (a, b, c) => console.log(`${a} + ${b} + ${c} !== 6`),
|
|
92
92
|
* ])
|
|
93
93
|
*
|
|
94
|
-
* const
|
|
94
|
+
* const result = await switchCase(true, [
|
|
95
95
|
* bool => bool,
|
|
96
96
|
* Promise.resolve(1),
|
|
97
97
|
* Promise.resolve(2),
|
|
98
98
|
* ])
|
|
99
99
|
*
|
|
100
|
-
*
|
|
100
|
+
* console.log(result)
|
|
101
101
|
* ```
|
|
102
102
|
*
|
|
103
103
|
* See also:
|
package/tap.js
CHANGED
|
@@ -22,12 +22,10 @@ const _tap = function (args, f) {
|
|
|
22
22
|
*
|
|
23
23
|
* @synopsis
|
|
24
24
|
* ```coffeescript [specscript]
|
|
25
|
-
* type
|
|
25
|
+
* type Function = (...arguments)=>Promise|any
|
|
26
26
|
*
|
|
27
|
-
* f
|
|
28
|
-
*
|
|
29
|
-
* tap(...arguments, f) -> Promise|arguments[0]
|
|
30
|
-
* tap(f)(...arguments) -> Promise|arguments[0]
|
|
27
|
+
* tap(...arguments, f Function) -> result Promise|any
|
|
28
|
+
* tap(f Function)(...arguments) -> result Promise|any
|
|
31
29
|
* ```
|
|
32
30
|
*
|
|
33
31
|
* @description
|
|
@@ -103,14 +101,11 @@ const _tapIf = function (predicate, f, args) {
|
|
|
103
101
|
*
|
|
104
102
|
* @synopsis
|
|
105
103
|
* ```coffeescript [specscript]
|
|
106
|
-
* type
|
|
107
|
-
* type
|
|
108
|
-
*
|
|
109
|
-
* predicate SyncOrAsyncPredicate
|
|
110
|
-
* f SyncOrAsyncFunction
|
|
104
|
+
* type Predicate = (...arguments)=>Promise|boolean|any
|
|
105
|
+
* type Function = (...arguments)=>Promise|any
|
|
111
106
|
*
|
|
112
|
-
* tap.if(...arguments, predicate, f) -> Promise|
|
|
113
|
-
* tap.if(predicate, f)(...arguments) -> Promise|
|
|
107
|
+
* tap.if(...arguments, predicate Predicate, f Function) -> result Promise|any
|
|
108
|
+
* tap.if(predicate Predicate, f Function)(...arguments) -> result Promise|any
|
|
114
109
|
* ```
|
|
115
110
|
*
|
|
116
111
|
* @description
|
package/thunkify.js
CHANGED
|
@@ -23,20 +23,28 @@ function _thunkifyArgs(func, context, args) {
|
|
|
23
23
|
*
|
|
24
24
|
* @synopsis
|
|
25
25
|
* ```coffeescript [specscript]
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* thunkify(func function, ...args) -> thunk ()=>func(...args)
|
|
26
|
+
* thunkify(func function, ...arguments) -> thunk function
|
|
29
27
|
* ```
|
|
30
28
|
*
|
|
31
29
|
* @description
|
|
32
|
-
* Creates a thunk from a function and arguments. A thunk takes no arguments, and when called, executes the
|
|
30
|
+
* Creates a thunk function from a function and arguments. A thunk function takes no arguments, and when called, executes the function and arguments. The function is said to be "thunkified".
|
|
33
31
|
*
|
|
34
32
|
* ```javascript [playground]
|
|
35
33
|
* const add = (a, b) => a + b
|
|
36
34
|
*
|
|
37
35
|
* const thunkAdd12 = thunkify(add, 1, 2)
|
|
38
36
|
*
|
|
39
|
-
* console.log(thunkAdd12())
|
|
37
|
+
* console.log(thunkAdd12())
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* Any promises in `arguments` are resolved for their values during thunk creation.
|
|
41
|
+
*
|
|
42
|
+
* ```javascript [playground]
|
|
43
|
+
* const add = (a, b) => a + b
|
|
44
|
+
*
|
|
45
|
+
* const thunkAdd12 = await thunkify(add, Promise.resolve(1), 2)
|
|
46
|
+
*
|
|
47
|
+
* console.log(thunkAdd12())
|
|
40
48
|
* ```
|
|
41
49
|
*
|
|
42
50
|
* See also:
|
|
@@ -60,13 +68,35 @@ const thunkify = function thunkify(func, ...args) {
|
|
|
60
68
|
*
|
|
61
69
|
* @synopsis
|
|
62
70
|
* ```coffeescript [specscript]
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
* thunkify.call(func function, context object, ...args) -> thunk ()=>func(...args)
|
|
71
|
+
* thunkify.call(func function, context object, ...arguments) -> thunk function
|
|
66
72
|
* ```
|
|
67
73
|
*
|
|
68
74
|
* @description
|
|
69
|
-
* Creates a thunk that calls a function with the specified context and arguments.
|
|
75
|
+
* Creates a thunk function that calls a function with the specified context and arguments.
|
|
76
|
+
*
|
|
77
|
+
* ```javascript [playground]
|
|
78
|
+
* class Point {
|
|
79
|
+
* constructor(x, y) {
|
|
80
|
+
* this.x = x
|
|
81
|
+
* this.y = y
|
|
82
|
+
* }
|
|
83
|
+
*
|
|
84
|
+
* distanceTo(point) {
|
|
85
|
+
* const x2 = (point.x - this.x) ** 2
|
|
86
|
+
* const y2 = (point.y - this.y) ** 2
|
|
87
|
+
* return (x2 + y2) ** 0.5
|
|
88
|
+
* }
|
|
89
|
+
* }
|
|
90
|
+
*
|
|
91
|
+
* const point0 = new Point(0, 0)
|
|
92
|
+
* const point1 = new Point(3, 4)
|
|
93
|
+
*
|
|
94
|
+
* const thunk = thunkify.call(point0.distanceTo, point0, point1)
|
|
95
|
+
*
|
|
96
|
+
* console.log(thunk())
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* Any promises in `arguments` are resolved for their values during thunk creation.
|
|
70
100
|
*
|
|
71
101
|
* ```javascript [playground]
|
|
72
102
|
* class Point {
|
|
@@ -75,7 +105,7 @@ const thunkify = function thunkify(func, ...args) {
|
|
|
75
105
|
* this.y = y
|
|
76
106
|
* }
|
|
77
107
|
*
|
|
78
|
-
* distanceTo() {
|
|
108
|
+
* distanceTo(point) {
|
|
79
109
|
* const x2 = (point.x - this.x) ** 2
|
|
80
110
|
* const y2 = (point.y - this.y) ** 2
|
|
81
111
|
* return (x2 + y2) ** 0.5
|
|
@@ -83,9 +113,9 @@ const thunkify = function thunkify(func, ...args) {
|
|
|
83
113
|
* }
|
|
84
114
|
*
|
|
85
115
|
* const point0 = new Point(0, 0)
|
|
86
|
-
* const
|
|
116
|
+
* const point1 = new Point(3, 4)
|
|
87
117
|
*
|
|
88
|
-
* const thunk = thunkify.call(point0.distanceTo, point0,
|
|
118
|
+
* const thunk = await thunkify.call(point0.distanceTo, point0, Promise.resolve(point1))
|
|
89
119
|
*
|
|
90
120
|
* console.log(thunk())
|
|
91
121
|
* ```
|
package/transform.js
CHANGED
|
@@ -22,23 +22,55 @@ const _transform = function (collection, transducer, initialValue) {
|
|
|
22
22
|
* @synopsis
|
|
23
23
|
* ```coffeescript [specscript]
|
|
24
24
|
* type Foldable = Array|Set|Map|Generator|AsyncGenerator|{ reduce: function }|Object
|
|
25
|
-
* type
|
|
26
|
-
* type Transducer =
|
|
25
|
+
* type Reducer = (accumulator any, value any)=>(nextAccumulator Promise|any)
|
|
26
|
+
* type Transducer = Reducer=>Reducer
|
|
27
27
|
*
|
|
28
|
-
* type Semigroup =
|
|
29
|
-
* Array|String|Set|TypedArray|{ concat: function }|{ write: function }|Object
|
|
28
|
+
* type Semigroup = Array|String|Set|TypedArray|{ concat: function }|{ write: function }|Object
|
|
30
29
|
*
|
|
31
|
-
* type
|
|
30
|
+
* type SemigroupResolver = any=>Promise|Semigroup
|
|
32
31
|
*
|
|
33
|
-
* transducer
|
|
34
|
-
*
|
|
32
|
+
* transform(foldable Promise|Foldable, transducer, initialValue Promise|any) -> result Promise|Semigroup
|
|
33
|
+
* transform(foldable Promise|Foldable, transducer, initialResolver SemigroupResolver) -> result Promise|Semigroup
|
|
35
34
|
*
|
|
36
|
-
* transform(
|
|
37
|
-
* transform(transducer,
|
|
35
|
+
* transform(transducer, initialValue Promise|any)(foldable Foldable) -> result Promise|Semigroup
|
|
36
|
+
* transform(transducer, initialResolver SemigroupResolver)(foldable Foldable) -> result Promise|Semigroup
|
|
38
37
|
* ```
|
|
39
38
|
*
|
|
40
39
|
* @description
|
|
41
|
-
* Transforms a foldable with [
|
|
40
|
+
* Transforms a foldable into a semigroup with a [transducer](https://rubico.land/blog/transducers-crash-course-rubico-v2).
|
|
41
|
+
*
|
|
42
|
+
* ```javascript [playground]
|
|
43
|
+
* const square = number => number ** 2
|
|
44
|
+
*
|
|
45
|
+
* const isOdd = number => number % 2 == 1
|
|
46
|
+
*
|
|
47
|
+
* const squaredOdds = compose([
|
|
48
|
+
* Transducer.filter(isOdd),
|
|
49
|
+
* Transducer.map(square),
|
|
50
|
+
* ])
|
|
51
|
+
*
|
|
52
|
+
* const array = [1, 2, 3, 4, 5]
|
|
53
|
+
*
|
|
54
|
+
* // transform arrays into arrays
|
|
55
|
+
* const squaredOddsArray = transform(array, squaredOdds, [])
|
|
56
|
+
* console.log('array into array')
|
|
57
|
+
* console.log(squaredOddsArray)
|
|
58
|
+
*
|
|
59
|
+
* // transform arrays into strings
|
|
60
|
+
* const squaredOddsString = transform(array, squaredOdds, '')
|
|
61
|
+
* console.log('array into string')
|
|
62
|
+
* console.log(squaredOddsString)
|
|
63
|
+
*
|
|
64
|
+
* // transform arrays into sets
|
|
65
|
+
* const squaredOddsSet = transform(array, squaredOdds, new Set())
|
|
66
|
+
* console.log('array into set')
|
|
67
|
+
* console.log(squaredOddsSet)
|
|
68
|
+
*
|
|
69
|
+
* // transform arrays into typed arrays
|
|
70
|
+
* const squaredOddsUint8Array = transform(array, squaredOdds, new Uint8Array())
|
|
71
|
+
* console.log('array into binary')
|
|
72
|
+
* console.log(squaredOddsUint8Array)
|
|
73
|
+
* ```
|
|
42
74
|
*
|
|
43
75
|
* The following data types are considered to be foldables:
|
|
44
76
|
* * `array`
|
|
@@ -49,11 +81,11 @@ const _transform = function (collection, transducer, initialValue) {
|
|
|
49
81
|
* * `object with .reduce method`
|
|
50
82
|
* * `object`
|
|
51
83
|
*
|
|
52
|
-
*
|
|
84
|
+
* The transducer defines the transformation done by `transform`. In a transformation, each item of the foldable is processed by the transducer in series.
|
|
53
85
|
*
|
|
54
86
|
* ```coffeescript [specscript]
|
|
55
|
-
* type
|
|
56
|
-
* type Transducer =
|
|
87
|
+
* type Reducer = (accumulator any, value any)=>(nextAccumulator Promise|any)
|
|
88
|
+
* type Transducer = Reducer=>Reducer
|
|
57
89
|
* ```
|
|
58
90
|
*
|
|
59
91
|
* The following data types are considered to be semigroups:
|
|
@@ -106,40 +138,7 @@ const _transform = function (collection, transducer, initialValue) {
|
|
|
106
138
|
* nextAccumulator = ({ ...accumulator, ...values })
|
|
107
139
|
* ```
|
|
108
140
|
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
* ```javascript [playground]
|
|
112
|
-
* const square = number => number ** 2
|
|
113
|
-
*
|
|
114
|
-
* const isOdd = number => number % 2 == 1
|
|
115
|
-
*
|
|
116
|
-
* const squaredOdds = compose([
|
|
117
|
-
* Transducer.filter(isOdd),
|
|
118
|
-
* Transducer.map(square),
|
|
119
|
-
* ])
|
|
120
|
-
*
|
|
121
|
-
* // transform arrays into arrays
|
|
122
|
-
* console.log(
|
|
123
|
-
* transform([1, 2, 3, 4, 5], squaredOdds, [])
|
|
124
|
-
* ) // [1, 9, 25]
|
|
125
|
-
*
|
|
126
|
-
* // transform arrays into strings
|
|
127
|
-
* console.log(
|
|
128
|
-
* transform([1, 2, 3, 4, 5], squaredOdds, '')
|
|
129
|
-
* ) // '1925'
|
|
130
|
-
*
|
|
131
|
-
* // transform arrays into sets
|
|
132
|
-
* console.log(
|
|
133
|
-
* transform([1, 2, 3, 4, 5], squaredOdds, new Set())
|
|
134
|
-
* ) // Set (3) { 1, 9, 25 }
|
|
135
|
-
*
|
|
136
|
-
* // transform arrays into typed arrays
|
|
137
|
-
* console.log(
|
|
138
|
-
* transform([1, 2, 3, 4, 5], squaredOdds, new Uint8Array()),
|
|
139
|
-
* ) // Uint8Array(3) [ 1, 9, 25 ]
|
|
140
|
-
* ```
|
|
141
|
-
*
|
|
142
|
-
* `transform` transforms arrays into objects that implement `.concat`.
|
|
141
|
+
* Any object that implements concat may be used as the semigroup for `transform`.
|
|
143
142
|
*
|
|
144
143
|
* ```javascript [playground]
|
|
145
144
|
* const square = number => number ** 2
|
|
@@ -152,14 +151,9 @@ const _transform = function (collection, transducer, initialValue) {
|
|
|
152
151
|
* }
|
|
153
152
|
*
|
|
154
153
|
* transform([1, 2, 3, 4, 5], Transducer.map(square), Stdout)
|
|
155
|
-
* // 1
|
|
156
|
-
* // 4
|
|
157
|
-
* // 9
|
|
158
|
-
* // 16
|
|
159
|
-
* // 25
|
|
160
154
|
* ```
|
|
161
155
|
*
|
|
162
|
-
*
|
|
156
|
+
* Node.js `process.stdout`, a writable stream (implements the `write` method), may be used as the semigroup for `transform`
|
|
163
157
|
*
|
|
164
158
|
* ```javascript
|
|
165
159
|
* const { pipe, compose, transform } = rubico
|
|
@@ -189,28 +183,35 @@ const _transform = function (collection, transducer, initialValue) {
|
|
|
189
183
|
* )
|
|
190
184
|
* ```
|
|
191
185
|
*
|
|
192
|
-
* If the initial value is a function it is treated as a resolver of the semigroup. The resolver may be asynchronous.
|
|
186
|
+
* If the initial value is a function, it is treated as a resolver of the semigroup. The resolver may be asynchronous.
|
|
193
187
|
*
|
|
194
188
|
* ```javascript [playground]
|
|
195
|
-
* const
|
|
189
|
+
* const result = await transform(
|
|
196
190
|
* [1, 2, 3, 4, 5],
|
|
197
191
|
* Transducer.map(number => number ** 2),
|
|
198
|
-
* async () => [
|
|
192
|
+
* async () => []
|
|
199
193
|
* )
|
|
200
194
|
*
|
|
201
|
-
*
|
|
195
|
+
* console.log(result)
|
|
202
196
|
* ```
|
|
203
197
|
*
|
|
204
|
-
*
|
|
198
|
+
* If the foldable or initial value is a promise, it is resolved for its value before further execution for the eager interface only.
|
|
205
199
|
*
|
|
206
200
|
* ```javascript [playground]
|
|
207
|
-
* const
|
|
201
|
+
* const resultFromPromiseFoldable = await transform(
|
|
208
202
|
* Promise.resolve([1, 2, 3, 4, 5]),
|
|
209
203
|
* Transducer.map(n => n ** 2),
|
|
210
|
-
* []
|
|
204
|
+
* []
|
|
211
205
|
* )
|
|
212
206
|
*
|
|
213
|
-
*
|
|
207
|
+
* const resultFromPromiseSemigroup = await transform(
|
|
208
|
+
* [1, 2, 3, 4, 5],
|
|
209
|
+
* Transducer.map(n => n ** 2),
|
|
210
|
+
* Promise.resolve([])
|
|
211
|
+
* )
|
|
212
|
+
*
|
|
213
|
+
* console.log(resultFromPromiseFoldable)
|
|
214
|
+
* console.log(resultFromPromiseSemigroup)
|
|
214
215
|
* ```
|
|
215
216
|
*
|
|
216
217
|
* See also:
|