<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"combining
Source effect/Iterable.ts:99924 lines
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>
} = import dualdual(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
}
}
}
}))