<A>(predicate: (a: NoInfer<A>, i: number) => boolean): (
self: Iterable<A>
) => [beforeMatch: Array<A>, fromMatch: Array<A>]
<A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): [
beforeMatch: Array<A>,
fromMatch: Array<A>
]Splits an iterable at the first element matching the predicate. The matching element is included in the second array.
When to use
Use when you need to split an array at the first element that marks a condition boundary.
Example (Splitting at a condition)
import { Array } from "effect"
console.log(Array.splitWhere([1, 2, 3, 4, 5], (n) => n > 3)) // [[1, 2, 3], [4, 5]]export const const splitWhere: {
<A>(
predicate: (
a: NoInfer<A>,
i: number
) => boolean
): (
self: Iterable<A>
) => [
beforeMatch: Array<A>,
fromMatch: Array<A>
]
<A>(
self: Iterable<A>,
predicate: (a: A, i: number) => boolean
): [beforeMatch: Array<A>, fromMatch: Array<A>]
}
Splits an iterable at the first element matching the predicate. The matching
element is included in the second array.
When to use
Use when you need to split an array at the first element that marks a
condition boundary.
Example (Splitting at a condition)
import { Array } from "effect"
console.log(Array.splitWhere([1, 2, 3, 4, 5], (n) => n > 3)) // [[1, 2, 3], [4, 5]]
splitWhere: {
<function (type parameter) A in <A>(predicate: (a: NoInfer<A>, i: number) => boolean): (self: Iterable<A>) => [beforeMatch: Array<A>, fromMatch: Array<A>]A>(
predicate: (a: NoInfer<A>, i: number) => booleanpredicate: (a: NoInfer<A>a: type NoInfer<A> = [A][A extends any ? 0 : never]Prevents TypeScript from inferring a type parameter from a specific
position.
When to use
Use when a function parameter must match an inferred type without becoming
an inference source.
Details
The parameter using NoInfer must still match the inferred type.
Example (Controlling inference)
import type { Types } from "effect"
declare function withDefault<T>(value: T, fallback: Types.NoInfer<T>): T
// T is inferred as "a" | "b" from the first argument only
const result = withDefault<"a" | "b">("a", "b")
NoInfer<function (type parameter) A in <A>(predicate: (a: NoInfer<A>, i: number) => boolean): (self: Iterable<A>) => [beforeMatch: Array<A>, fromMatch: Array<A>]A>, i: numberi: number) => boolean
): (self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(predicate: (a: NoInfer<A>, i: number) => boolean): (self: Iterable<A>) => [beforeMatch: Array<A>, fromMatch: Array<A>]A>) => [A[]beforeMatch: interface Array<T>Array<function (type parameter) A in <A>(predicate: (a: NoInfer<A>, i: number) => boolean): (self: Iterable<A>) => [beforeMatch: Array<A>, fromMatch: Array<A>]A>, A[]fromMatch: interface Array<T>Array<function (type parameter) A in <A>(predicate: (a: NoInfer<A>, i: number) => boolean): (self: Iterable<A>) => [beforeMatch: Array<A>, fromMatch: Array<A>]A>]
<function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): [beforeMatch: Array<A>, fromMatch: Array<A>]A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): [beforeMatch: Array<A>, fromMatch: Array<A>]A>, predicate: (a: A, i: number) => booleanpredicate: (a: Aa: function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): [beforeMatch: Array<A>, fromMatch: Array<A>]A, i: numberi: number) => boolean): [A[]beforeMatch: interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): [beforeMatch: Array<A>, fromMatch: Array<A>]A>, A[]fromMatch: interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): [beforeMatch: Array<A>, fromMatch: Array<A>]A>]
} = dual<(...args: Array<any>) => any, <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean) => [beforeMatch: Array<A>, fromMatch: Array<A>]>(arity: 2, body: <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean) => [beforeMatch: Array<A>, fromMatch: Array<A>]): ((...args: Array<any>) => any) & (<A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean) => [beforeMatch: Array<A>, fromMatch: Array<A>]) (+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>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): [beforeMatch: Array<A>, fromMatch: Array<A>]A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): [beforeMatch: Array<A>, fromMatch: Array<A>]A>, predicate: (a: A, i: number) => booleanpredicate: (a: Aa: function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): [beforeMatch: Array<A>, fromMatch: Array<A>]A, i: numberi: number) => boolean): [A[]beforeMatch: interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): [beforeMatch: Array<A>, fromMatch: Array<A>]A>, A[]fromMatch: interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): [beforeMatch: Array<A>, fromMatch: Array<A>]A>] =>
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>]
}
span(self: Iterable<A>self, (a: Aa: function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): [beforeMatch: Array<A>, fromMatch: Array<A>]A, i: numberi: number) => !predicate: (a: A, i: number) => booleanpredicate(a: Aa, i: numberi))
)