Package: effect
Module: Array
Splits an iterable into two arrays: the longest prefix where the predicate holds, and the remaining elements.
When to use
Use when you need both the longest predicate-matching prefix and the remaining elements.
Details
Equivalent to [takeWhile(pred), dropWhile(pred)], but more efficient
because it runs in a single pass. Supports refinements for type narrowing of
the prefix.
Example (Splitting at predicate boundary)
import { Array } from "effect"
console.log(Array.span([1, 3, 2, 4, 5], (x) => x % 2 === 1)) // [[1, 3], [2, 4, 5]]
See
takeWhile for keeping only the matching prefixdropWhile for keeping only the elements after the matching prefixsplitWhere for splitting at the first element that satisfies a predicateSignature
declare const span: { <A, B extends A>(refinement: (a: NoInfer<A>, i: number) => a is B): (self: Iterable<A>) => [init: Array<B>, rest: Array<Exclude<A, B>>]; <A>(predicate: (a: NoInfer<A>, i: number) => boolean): (self: Iterable<A>) => [init: Array<A>, rest: Array<A>]; <A, B extends A>(self: Iterable<A>, refinement: (a: A, i: number) => a is B): [init: Array<B>, rest: Array<Exclude<A, B>>]; <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): [init: Array<A>, rest: Array<A>]; }
Since v2.0.0