functionalscript 0.0.216 → 0.0.217

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.216",
3
+ "version": "0.0.217",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,41 +1,41 @@
1
- # Sequence Operators
1
+ # Operator
2
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:
3
+ ## Sequence
19
4
 
20
5
  ```ts
21
- type Scan2<A, T> = (value: T) => [A, Scan2<A, T>]
22
- type ExclusiveScan2<A, T> = [first, Scan2<A, T>]
6
+ type Sequence<T> = SubSequence<T, undefined>
23
7
  ```
24
8
 
25
- `takeWhile`, `find` can use `scan`. Optimization: if a `Scan2` part is `emptyScan` then we can stop searching.
26
-
27
- ## A Universal Operator `FlatScan`
9
+ ## SubSequence
28
10
 
29
11
  ```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>]
12
+ type SubSequence<T, C> = () => SubSequenceResult<T, C>
13
+ type SubSequenceResult<T, C> = [T, SubSequence<T, C>] | [C]
35
14
  ```
36
15
 
37
- Optimization: if result is `empty`, then we can stop.
16
+ ## The Main FlatScan Operator
38
17
 
39
18
  ```ts
40
- const empty = ['novalue', () => empty]
41
- ```
19
+ type FlatScanOperator<T, A> = (value: T) => SubSequence<A, FlatScanOp<T, A>>
20
+
21
+ const flatScanConcat
22
+ : SubSequence<A, FlatScanOp<T, A>> => Sequence<T> => Sequence<A>
23
+ => a => b => () => {
24
+ switch (next(a)) {
25
+ case [first, tail]: { return [first, flatScanConcat(tail)(b)] }
26
+ case [operator]: { return flatScan(operator)(b)() }
27
+ }
28
+ }
29
+
30
+ const flatScan
31
+ : FlatScanOperator<T, A> => Sequence<T> => Sequence<A>
32
+ => operator => sequence => () => {
33
+ // optimization for `takeWhile`, `find`
34
+ if (operator === flatScanEmpty) { return [undefined] }
35
+ //
36
+ switch (next(s)) {
37
+ case [first, tail]: { return flatScanConcat(operator(first))(tail)() }
38
+ case [undefined]: { return [undefined] }
39
+ }
40
+ }
41
+ ```