functionalscript
Advanced tools
Comparing version 0.0.167 to 0.0.168
@@ -86,3 +86,3 @@ const { pipe } = require('../../function') | ||
/** @type {<T, R>(es: seq.ExlusiveScan<T, R>) => (c: AnyIterable<T>) => AsyncIterable<R>} */ | ||
/** @type {<T, R>(es: seq.Scan<T, R>) => (c: AnyIterable<T>) => AsyncIterable<R>} */ | ||
const applyExclusiveScan = es => c => ({ | ||
@@ -89,0 +89,0 @@ async *[Symbol.asyncIterator]() { |
@@ -41,4 +41,4 @@ const { pipe } = require('../function') | ||
/** @type {<T, R>(es: seq.ExlusiveScan<T, R>) => (c: Iterable<T>) => Iterable<R>} */ | ||
const applyExclusiveScan = es => c => ({ | ||
/** @type {<T, R>(es: seq.Scan<T, R>) => (c: Iterable<T>) => Iterable<R>} */ | ||
const applyScan = es => c => ({ | ||
*[Symbol.iterator]() { | ||
@@ -54,4 +54,7 @@ let ies = es | ||
const entries = applyExclusiveScan(seq.entries) | ||
/** @type {<T, R>(is: seq.InclusiveScan<T, R>) => (c: Iterable<T>) => Iterable<R>} */ | ||
const applyInclusiveScan = ({scan, first}) => c => concat([first])(applyScan(scan)(c)) | ||
const entries = applyScan(seq.entries) | ||
/** @type {<I, S, R>(op: seq.Operation<I, S, R>) => (_: Iterable<I>) => R} */ | ||
@@ -58,0 +61,0 @@ const apply = ({ merge, init, result }) => pipe(reduce(merge)(init))(result) |
{ | ||
"name": "functionalscript", | ||
"version": "0.0.167", | ||
"version": "0.0.168", | ||
"description": "FunctionalScript is a functional subset of JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,10 +0,84 @@ | ||
const { pipe, id } = require('../function') | ||
const { todo } = require('../dev') | ||
const { id } = require('../function') | ||
/** | ||
* @template S | ||
* @template I | ||
* @typedef {(state: S) => (value: I) => S} Merge | ||
* @template T0 | ||
* @template T1 | ||
* @typedef {import('../array').Tuple2<T0, T1>} Tuple2 | ||
*/ | ||
/** | ||
* @template T | ||
* @template R | ||
* @typedef {Tuple2<R, Scan<T, R>>} ScanResult | ||
*/ | ||
/** | ||
* @template T | ||
* @template R | ||
* @typedef {(value: T) => ScanResult<T, R>} Scan | ||
*/ | ||
/** | ||
* @template T | ||
* @template R | ||
* @typedef {{ | ||
* readonly scan: Scan<T, R> | ||
* readonly first: R | ||
* }} InclusiveScan | ||
*/ | ||
/** | ||
* @template R | ||
* @template T | ||
* @typedef {(prior: R) => (value: T) => R} BinaryOperator | ||
*/ | ||
/** @type {<R, T>(operator: BinaryOperator<R, T>) => (prior: R) => Scan<T, R>} */ | ||
const operatorScan = operator => { | ||
/** @typedef {typeof operator extends BinaryOperator<infer R, infer T> ? [R, T] : never} RT */ | ||
/** @typedef {RT[0]} R */ | ||
/** @typedef {RT[1]} T */ | ||
/** @type {(prior: R) => Scan<T, R>} */ | ||
const f = prior => value => { | ||
const result = operator(prior)(value) | ||
return [result, f(result)] | ||
} | ||
return f | ||
} | ||
/** @type {<R, T>(operator: BinaryOperator<R, T>) => (first: R) => InclusiveScan<T, R>} */ | ||
const inclusiveOperatorScan = operator => first => ({ | ||
scan: operatorScan(operator)(first), | ||
first, | ||
}) | ||
/** | ||
* @template T | ||
* @typedef {Tuple2<number, T>} Entry | ||
*/ | ||
/** @type {(index: number) => <T>(value: T) => ScanResult<T, Entry<T>>} */ | ||
const createEntries = index => value => [[index, value], createEntries(index + 1)] | ||
const entries = createEntries(0) | ||
/** @type {(separator: string) => BinaryOperator<string, string>} */ | ||
const joinOperation = separator => prior => value => `${prior}${separator}${value}` | ||
/** @type {(separator: string) => InclusiveScan<string, string>} */ | ||
const join2 = separator => ({ | ||
scan: value => [value, operatorScan(joinOperation(separator))(value)], | ||
first: '' | ||
}) | ||
/** @type {(sum: number) => (value: number) => number} */ | ||
const addition = a => b => a + b | ||
/** @type {InclusiveScan<number, number>} */ | ||
const sum2 = inclusiveOperatorScan(addition)(0) | ||
//// | ||
/** | ||
* @template I | ||
@@ -14,3 +88,3 @@ * @template S | ||
* @typedef {{ | ||
* readonly merge: Merge<S, I> | ||
* readonly merge: BinaryOperator<S, I> | ||
* readonly result: (state: S) => O | ||
@@ -21,9 +95,2 @@ * readonly init: S | ||
/** @type {<I, T>(mapFn: (value: I) => T) => <S, O>(op: Operation<T, S, O>) => Operation<I, S, O>} */ | ||
const map = mapFn => ({ merge, result, init}) => ({ | ||
merge: pipe(merge)(pipe(mapFn)), | ||
result, | ||
init, | ||
}) | ||
/** @type {(separator: string) => Operation<string, string|undefined, string>} */ | ||
@@ -56,33 +123,6 @@ const join = separator => ({ | ||
/** | ||
* @template T | ||
* @template R | ||
* @typedef {(value: T) => readonly [R, ExlusiveScan<T, R>]} ExlusiveScan | ||
*/ | ||
/** | ||
* @template T | ||
* @template R | ||
* @template I | ||
* @typedef {{ | ||
* readonly exlusive: ExlusiveScan<T, R> | ||
* readonly default: I | ||
* }} InclusiveScan | ||
*/ | ||
/** | ||
* @template T | ||
* @typedef {readonly[number, T]} Entry | ||
*/ | ||
/** @type {(index: number) => <T>(value: T) => readonly[Entry<T>, ExlusiveScan<T, Entry<T>>]} */ | ||
const entriesFrom = index => value => [[index, value], entriesFrom(index + 1)] | ||
const entries = entriesFrom(0) | ||
module.exports = { | ||
/** @readonly */ | ||
map, | ||
/** @readonly */ | ||
join, | ||
join2, | ||
/** @readonly */ | ||
@@ -89,0 +129,0 @@ sum, |
80489
1472