<B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<A>) => Iterable<B>
<A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>Reduces an Iterable from the left, keeping all intermediate results instead of only the final result.
Example (Tracking running results)
import { Iterable } from "effect"
// Running sum of numbers
const numbers = [1, 2, 3, 4, 5]
const runningSum = Iterable.scan(numbers, 0, (acc, n) => acc + n)
console.log(Array.from(runningSum)) // [0, 1, 3, 6, 10, 15]
// Build strings progressively
const letters = ["a", "b", "c"]
const progressive = Iterable.scan(letters, "", (acc, letter) => acc + letter)
console.log(Array.from(progressive)) // ["", "a", "ab", "abc"]
// Track maximum values seen so far
const values = [3, 1, 4, 1, 5, 9, 2]
const runningMax = Iterable.scan(values, -Infinity, Math.max)
console.log(Array.from(runningMax)) // [-Infinity, 3, 3, 4, 4, 5, 9, 9]folding
Source effect/Iterable.ts:39922 lines
export const const scan: {
<B, A>(b: B, f: (b: B, a: A) => B): (
self: Iterable<A>
) => Iterable<B>
<A, B>(
self: Iterable<A>,
b: B,
f: (b: B, a: A) => B
): Iterable<B>
}
Reduces an Iterable from the left, keeping all intermediate results instead of only the final result.
Example (Tracking running results)
import { Iterable } from "effect"
// Running sum of numbers
const numbers = [1, 2, 3, 4, 5]
const runningSum = Iterable.scan(numbers, 0, (acc, n) => acc + n)
console.log(Array.from(runningSum)) // [0, 1, 3, 6, 10, 15]
// Build strings progressively
const letters = ["a", "b", "c"]
const progressive = Iterable.scan(letters, "", (acc, letter) => acc + letter)
console.log(Array.from(progressive)) // ["", "a", "ab", "abc"]
// Track maximum values seen so far
const values = [3, 1, 4, 1, 5, 9, 2]
const runningMax = Iterable.scan(values, -Infinity, Math.max)
console.log(Array.from(runningMax)) // [-Infinity, 3, 3, 4, 4, 5, 9, 9]
scan: {
<function (type parameter) B in <B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<A>) => Iterable<B>B, function (type parameter) A in <B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<A>) => Iterable<B>A>(b: Bb: function (type parameter) B in <B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<A>) => Iterable<B>B, f: (b: B, a: A) => Bf: (b: Bb: function (type parameter) B in <B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<A>) => Iterable<B>B, a: Aa: function (type parameter) A in <B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<A>) => Iterable<B>A) => function (type parameter) B in <B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<A>) => Iterable<B>B): (self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<A>) => Iterable<B>A>) => interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) B in <B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<A>) => Iterable<B>B>
<function (type parameter) A in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>A, function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>B>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>A>, b: Bb: function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>B, f: (b: B, a: A) => Bf: (b: Bb: function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>B, a: Aa: function (type parameter) A in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>A) => function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>B): interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>B>
} = import dualdual(3, <function (type parameter) A in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>A, function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>B>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>A>, b: Bb: function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>B, f: (b: B, a: A) => Bf: (b: Bb: function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>B, a: Aa: function (type parameter) A in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>A) => function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>B): interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) B in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>B> => ({
[var Symbol: SymbolConstructorSymbol.SymbolConstructor.iterator: typeof Symbol.iteratorA method that returns the default iterator for an object. Called by the semantics of the
for-of statement.
iterator]() {
let let acc: Bacc = b: Bb
let let iterator:
| Iterator<A, any, any>
| undefined
iterator: interface Iterator<T, TReturn = any, TNext = any>Iterator<function (type parameter) A in <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>A> | undefined
function function (local function) next(): IteratorReturnResult<any> | {
done: boolean;
value: B;
}
next() {
if (let iterator:
| Iterator<A, any, any>
| undefined
iterator === var undefinedundefined) {
let iterator:
| Iterator<A, any, any>
| undefined
iterator = self: Iterable<A>self[var Symbol: SymbolConstructorSymbol.SymbolConstructor.iterator: typeof Symbol.iteratorA method that returns the default iterator for an object. Called by the semantics of the
for-of statement.
iterator]()
return { done: booleandone: false, value: Bvalue: let acc: Bacc }
}
const const result: IteratorResult<A, any>result = let iterator: Iterator<A, any, any>iterator.Iterator<A, any, any>.next(...[value]: [] | [any]): IteratorResult<A, any>next()
if (const result: IteratorResult<A, any>result.done?: boolean | undefineddone) {
return const result: IteratorReturnResult<any>result
}
let acc: Bacc = f: (b: B, a: A) => Bf(let acc: Bacc, const result: IteratorYieldResult<A>result.IteratorYieldResult<A>.value: Avalue)
return { done: booleandone: false, value: Bvalue: let acc: Bacc }
}
return { Iterator<B, any, any>.next(...[value]: [] | [any]): IteratorResult<B, any>next }
}
}))