functionalscript 0.0.502 → 0.0.503

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.502",
3
+ "version": "0.0.503",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "module.f.cjs",
6
6
  "scripts": {
@@ -2,7 +2,7 @@ const function_ = require('../function/module.f.cjs')
2
2
  const { identity, fn, compose } = function_
3
3
  const operator = require('../function/operator/module.f.cjs')
4
4
  const {
5
- counter,
5
+ addition,
6
6
  logicalNot,
7
7
  strictEqual,
8
8
  stateScanToScan,
@@ -255,8 +255,22 @@ const fold = op => init => compose(foldScan(op)(init))(last(init))
255
255
  /** @type {<T>(op: operator.Reduce<T>) => <D>(def: D) => (input: List<T>) => D|T} */
256
256
  const reduce = op => def => compose(scan(reduceToScan(op)))(last(def))
257
257
 
258
+ /** @type {<T>(list: List<T>) => Thunk<number>} */
259
+ const lengthList = list => () => {
260
+ const notLazy = trampoline(list)
261
+ if (notLazy === null) { return null }
262
+ if (notLazy instanceof Array) { return [notLazy.length] }
263
+ const tail = lengthList(notLazy.tail)
264
+ if ("first" in notLazy) {
265
+ return { first: 1, tail }
266
+ }
267
+ return { head: lengthList(notLazy.head), tail }
268
+ }
269
+
270
+ const sum = reduce(addition)(0)
271
+
258
272
  /** @type {<T>(input: List<T>) => number} */
259
- const length = fold(counter)(0)
273
+ const length = compose(lengthList)(sum)
260
274
 
261
275
  /**
262
276
  * @template T
@@ -280,7 +294,7 @@ const reverseOperator = first => tail => ({ first, tail })
280
294
  /** @type {<T>(input: List<T>) => List<T>} */
281
295
  const reverse = fold(reverseOperator)(null)
282
296
 
283
- /** @type {<A>(a: List<A>) => <B>(b: List<B>) => List<readonly[A, B]>} */
297
+ /** @type {<A>(a: List<A>) => <B>(b: List<B>) => Thunk<readonly[A, B]>} */
284
298
  const zip = a => b => () => {
285
299
  const aResult = next(a)
286
300
  if (aResult === null) { return null }
@@ -309,5 +309,10 @@ module.exports = {
309
309
  if (result !== false) { throw result }
310
310
  }
311
311
  ],
312
+ length: () => {
313
+ if (_.length([1, 2, 3]) !== 3) { throw 3 }
314
+ if (_.length(null) !== 0) { throw 0 }
315
+ if (_.length(_.flat([[1, 3], null, () => [3], _.concat([12])([4, 89])])) !== 6) { throw 6 }
316
+ },
312
317
  //stress
313
318
  }