<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]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>
} = dual<(...args: Array<any>) => any, <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B) => Iterable<B>>(arity: 3, body: <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B) => Iterable<B>): ((...args: Array<any>) => any) & (<A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B) => Iterable<B>) (+1 overload)Creates a function that can be called in data-first style or data-last
(pipe-friendly) style.
When to use
Use to expose one implementation through both direct and pipe-friendly
call styles.
Details
Pass either the arity of the uncurried function or a predicate that decides
whether the current call is data-first. Arity is the common case. Use a
predicate when optional arguments make arity ambiguous.
Example (Selecting data-first or data-last style by arity)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(2, (self, that) => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Defining overloads with call signatures)
import { Function, pipe } from "effect"
const sum: {
(that: number): (self: number) => number
(self: number, that: number): number
} = Function.dual(2, (self: number, that: number): number => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Selecting data-first or data-last style with a predicate)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(
(args) => args.length === 2,
(self, that) => self + that
)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
dual(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 }
}
}))