functionalscript 0.0.215 → 0.0.216
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/package.json +1 -1
- package/sequence/index.js +2 -2
- package/sequence/operator/README.md +41 -0
package/package.json
CHANGED
package/sequence/index.js
CHANGED
|
@@ -185,8 +185,8 @@ const last = def => input => {
|
|
|
185
185
|
/** @type {<T, R>(s: seqOp.ExclusiveScan<T, R>) => (input: Sequence<T>) => R} */
|
|
186
186
|
const reduce = ([first, s]) => input => last(first)(scan(s)(input))
|
|
187
187
|
|
|
188
|
-
/** @type {<T, R>(
|
|
189
|
-
const fold =
|
|
188
|
+
/** @type {<T, R>(operator: op.ReduceOperator<R, T>) => (first: R) => (input: Sequence<T>) => R} */
|
|
189
|
+
const fold = operator => first => reduce(seqOp.exclusiveScan(operator)(first))
|
|
190
190
|
|
|
191
191
|
const entries = scan(seqOp.entries)
|
|
192
192
|
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Sequence Operators
|
|
2
|
+
|
|
3
|
+
## A `FlatMap` Operator
|
|
4
|
+
|
|
5
|
+
`flatMap = combine(flat)(map)`
|
|
6
|
+
|
|
7
|
+
## A `Scan` Operator
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
type Scan<A, T> = (accumulator: A) => (value: T) => A
|
|
11
|
+
type ExclusiveScan<A, T> = [first, Scan<A, T>]
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
`scan`
|
|
15
|
+
|
|
16
|
+
`reduce = last(first)(scan)`
|
|
17
|
+
|
|
18
|
+
An alternative definition of a scan:
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
type Scan2<A, T> = (value: T) => [A, Scan2<A, T>]
|
|
22
|
+
type ExclusiveScan2<A, T> = [first, Scan2<A, T>]
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`takeWhile`, `find` can use `scan`. Optimization: if a `Scan2` part is `emptyScan` then we can stop searching.
|
|
26
|
+
|
|
27
|
+
## A Universal Operator `FlatScan`
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
type FlatScan<A, T> = (value: T) => FlatScanSequence<A, T>
|
|
31
|
+
type FlatScanSequence<A, T> = () => FlatScanResult<A, T>
|
|
32
|
+
type FlatScanResult<A, T> =
|
|
33
|
+
['value', A, FlatScanSequence<A, T>] |
|
|
34
|
+
['novalue', FlatScan<A, T>]
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Optimization: if result is `empty`, then we can stop.
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
const empty = ['novalue', () => empty]
|
|
41
|
+
```
|