<B>(middle: B): <A>(self: Iterable<A>) => Iterable<A | B>
<A, B>(self: Iterable<A>, middle: B): Iterable<A | B>Places a separator between members of an Iterable.
When to use
Use to lazily insert a separator between adjacent values.
Details
If the input is a non-empty array, the result is also a non-empty array.
Example (Interspersing separators)
import { Iterable } from "effect"
// Join numbers with separator
const numbers = [1, 2, 3, 4]
const withCommas = Iterable.intersperse(numbers, ",")
console.log(Array.from(withCommas)) // [1, ",", 2, ",", 3, ",", 4]
// Join words with spaces
const words = ["hello", "world", "from", "effect"]
const sentence = Iterable.intersperse(words, " ")
console.log(Array.from(sentence).join("")) // "hello world from effect"
// Empty iterable remains empty
const empty = Iterable.empty<string>()
const stillEmpty = Iterable.intersperse(empty, "-")
console.log(Array.from(stillEmpty)) // []
// Single element has no separators added
const single = [42]
const noSeparator = Iterable.intersperse(single, "|")
console.log(Array.from(noSeparator)) // [42]
// Build CSS-like strings
const styles = ["color: red", "font-size: 14px", "margin: 10px"]
const css = Iterable.intersperse(styles, "; ")
console.log(Array.from(css).join("")) // "color: red; font-size: 14px; margin: 10px"export const const intersperse: {
<B>(middle: B): <A>(
self: Iterable<A>
) => Iterable<A | B>
<A, B>(self: Iterable<A>, middle: B): Iterable<
A | B
>
}
Places a separator between members of an Iterable.
When to use
Use to lazily insert a separator between adjacent values.
Details
If the input is a non-empty array, the result is also a non-empty array.
Example (Interspersing separators)
import { Iterable } from "effect"
// Join numbers with separator
const numbers = [1, 2, 3, 4]
const withCommas = Iterable.intersperse(numbers, ",")
console.log(Array.from(withCommas)) // [1, ",", 2, ",", 3, ",", 4]
// Join words with spaces
const words = ["hello", "world", "from", "effect"]
const sentence = Iterable.intersperse(words, " ")
console.log(Array.from(sentence).join("")) // "hello world from effect"
// Empty iterable remains empty
const empty = Iterable.empty<string>()
const stillEmpty = Iterable.intersperse(empty, "-")
console.log(Array.from(stillEmpty)) // []
// Single element has no separators added
const single = [42]
const noSeparator = Iterable.intersperse(single, "|")
console.log(Array.from(noSeparator)) // [42]
// Build CSS-like strings
const styles = ["color: red", "font-size: 14px", "margin: 10px"]
const css = Iterable.intersperse(styles, "; ")
console.log(Array.from(css).join("")) // "color: red; font-size: 14px; margin: 10px"
intersperse: {
<function (type parameter) B in <B>(middle: B): <A>(self: Iterable<A>) => Iterable<A | B>B>(middle: Bmiddle: function (type parameter) B in <B>(middle: B): <A>(self: Iterable<A>) => Iterable<A | B>B): <function (type parameter) A in <A>(self: Iterable<A>): Iterable<A | B>A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>): Iterable<A | B>A>) => interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>): Iterable<A | B>A | function (type parameter) B in <B>(middle: B): <A>(self: Iterable<A>) => Iterable<A | B>B>
<function (type parameter) A in <A, B>(self: Iterable<A>, middle: B): Iterable<A | B>A, function (type parameter) B in <A, B>(self: Iterable<A>, middle: B): Iterable<A | B>B>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B>(self: Iterable<A>, middle: B): Iterable<A | B>A>, middle: Bmiddle: function (type parameter) B in <A, B>(self: Iterable<A>, middle: B): Iterable<A | B>B): interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B>(self: Iterable<A>, middle: B): Iterable<A | B>A | function (type parameter) B in <A, B>(self: Iterable<A>, middle: B): Iterable<A | B>B>
} = dual<(...args: Array<any>) => any, <A, B>(self: Iterable<A>, middle: B) => Iterable<A | B>>(arity: 2, body: <A, B>(self: Iterable<A>, middle: B) => Iterable<A | B>): ((...args: Array<any>) => any) & (<A, B>(self: Iterable<A>, middle: B) => Iterable<A | 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(2, <function (type parameter) A in <A, B>(self: Iterable<A>, middle: B): Iterable<A | B>A, function (type parameter) B in <A, B>(self: Iterable<A>, middle: B): Iterable<A | B>B>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B>(self: Iterable<A>, middle: B): Iterable<A | B>A>, middle: Bmiddle: function (type parameter) B in <A, B>(self: Iterable<A>, middle: B): Iterable<A | B>B): interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B>(self: Iterable<A>, middle: B): Iterable<A | B>A | function (type parameter) B in <A, B>(self: Iterable<A>, middle: B): Iterable<A | 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]() {
const const iterator: Iterator<A, any, any>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]()
let let next: IteratorResult<A, any>next = const iterator: Iterator<A, any, any>iterator.Iterator<A, any, any>.next(...[value]: [] | [any]): IteratorResult<A, any>next()
let let emitted: booleanemitted = false
return {
Iterator<A | B, any, any>.next(...[value]: [] | [any]): IteratorResult<A | B, any>next() {
if (let next: IteratorResult<A, any>next.done?: boolean | undefineddone) {
return let next: IteratorReturnResult<any>next
} else if (let emitted: booleanemitted) {
let emitted: booleanemitted = false
return { IteratorYieldResult<TYield>.done?: false | undefineddone: false, IteratorYieldResult<A | B>.value: A | Bvalue: middle: Bmiddle }
}
let emitted: booleanemitted = true
const const result: IteratorYieldResult<A>result = let next: IteratorYieldResult<A>next
let next: IteratorResult<A, any>next = const iterator: Iterator<A, any, any>iterator.Iterator<A, any, any>.next(...[value]: [] | [any]): IteratorResult<A, any>next()
return const result: IteratorYieldResult<A>result
}
}
}
}))