Package: effect
Module: Iterable
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"
Signature
declare const intersperse: { <B>(middle: B): <A>(self: Iterable<A>) => Iterable<A | B>; <A, B>(self: Iterable<A>, middle: B): Iterable<A | B>; }
Since v2.0.0