functionalscript 0.0.174 → 0.0.176

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.174",
3
+ "version": "0.0.176",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -2,6 +2,7 @@ const array = require('../array')
2
2
  const option = require('../../option')
3
3
  const base = require('..')
4
4
  const { pipe } = require('../../function')
5
+ const { todo } = require('../../dev')
5
6
 
6
7
  /**
7
8
  * @template T
@@ -53,15 +54,19 @@ const concat = a => b => () => {
53
54
  }
54
55
 
55
56
  /** @type {<T, R>(f: (value: T) => List<R>) => ListMap<T, R>} */
56
- const flatMap = f => {
57
- /** @typedef {typeof f extends (value: infer T) => List<infer R> ? [T, R] : never} TR */
58
- /** @typedef {TR[0]} T */
59
- /** @typedef {TR[1]} R */
60
- /** @type {(firstAntTail: FirstAndTail<T>) => Result<R>} */
61
- const defined = ([first, tail]) => concat(f(first))(listMap(tail))()
62
- /** @type {(list: List<T>) => List<R>} */
63
- const listMap = list => () => option.map(defined)(list())
64
- return listMap
57
+ const flatMap = f => input => () => {
58
+ let i = input
59
+ while (true) {
60
+ const result = i()
61
+ if (result === undefined) { return undefined }
62
+ const [first, tail] = result
63
+ const firstResult = f(first)()
64
+ if (firstResult !== undefined) {
65
+ const [firstFirst, firstTail] = firstResult
66
+ return [firstFirst, concat(firstTail)(flatMap(f)(tail))]
67
+ }
68
+ i = tail
69
+ }
65
70
  }
66
71
 
67
72
  /** @type {<T>(list: List<List<T>>) => List<T>} */
@@ -12,13 +12,14 @@ const print = a => {
12
12
 
13
13
  {
14
14
  const big = list.fromArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 42, 60])
15
- print(big)
15
+ // print(big)
16
16
  /*
17
17
  const list0 = list.fromArray([0, 1, 2, 3])
18
18
  const list1 = list.flatMap(x => list.fromArray([x, x * 2, x * 3]))(list0)
19
19
  const list2 = list.concat(list0)(list0)
20
20
  const list3 = list.inclusiveScan(sum)(list0)
21
21
  print(list3)
22
- const r = list.find(x => x === 42)(list.fromArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 42, 60]))
23
22
  */
23
+ const r = list.find(x => x === 42)(big)
24
+ console.log(r)
24
25
  }