<N extends number>(n: N): <A>(self: Iterable<A>) => Array<TupleOf<N, A>>
<A, N extends number>(self: Iterable<A>, n: N): Array<TupleOf<N, A>>Creates overlapping sliding windows of size n.
When to use
Use to process sequences with a moving window, such as for computing running averages or detecting patterns.
Details
Returns an empty array if n <= 0 or the array has fewer than n elements.
Each window is a tuple of exactly n elements.
Example (Creating sliding windows)
import { Array } from "effect"
console.log(Array.window([1, 2, 3, 4, 5], 3)) // [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
console.log(Array.window([1, 2, 3, 4, 5], 6)) // []export const const window: {
<N extends number>(n: N): <A>(
self: Iterable<A>
) => Array<TupleOf<N, A>>
<A, N extends number>(
self: Iterable<A>,
n: N
): Array<TupleOf<N, A>>
}
Creates overlapping sliding windows of size n.
When to use
Use to process sequences with a moving window, such as for computing running averages or detecting patterns.
Details
Returns an empty array if n <= 0 or the array has fewer than n elements.
Each window is a tuple of exactly n elements.
Example (Creating sliding windows)
import { Array } from "effect"
console.log(Array.window([1, 2, 3, 4, 5], 3)) // [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
console.log(Array.window([1, 2, 3, 4, 5], 6)) // []
window: {
<function (type parameter) N in <N extends number>(n: N): <A>(self: Iterable<A>) => Array<TupleOf<N, A>>N extends number>(n: N extends numbern: function (type parameter) N in <N extends number>(n: N): <A>(self: Iterable<A>) => Array<TupleOf<N, A>>N): <function (type parameter) A in <A>(self: Iterable<A>): Array<TupleOf<N, A>>A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>): Array<TupleOf<N, A>>A>) => interface Array<T>Array<type TupleOf<N extends number, T> = N extends N ? number extends N ? T[] : TupleOf_<T, N, []> : neverConstructs a tuple type with exactly N elements of type T.
When to use
Use when you need a fixed-length array type, especially instead of manually
writing [T, T, T, ...] for longer tuples.
Details
- If
N is a literal number, produces a tuple of that exact length.
- If
N is the general number type (non-literal), degrades to Array<T>.
- Negative numbers produce
never.
Example (Checking fixed-length tuples)
import type { Types } from "effect"
// Exactly 3 numbers
const triple: Types.TupleOf<3, number> = [1, 2, 3]
//
TupleOf<function (type parameter) N in <N extends number>(n: N): <A>(self: Iterable<A>) => Array<TupleOf<N, A>>N, function (type parameter) A in <A>(self: Iterable<A>): Array<TupleOf<N, A>>A>>
<function (type parameter) A in <A, N extends number>(self: Iterable<A>, n: N): Array<TupleOf<N, A>>A, function (type parameter) N in <A, N extends number>(self: Iterable<A>, n: N): Array<TupleOf<N, A>>N extends number>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, N extends number>(self: Iterable<A>, n: N): Array<TupleOf<N, A>>A>, n: N extends numbern: function (type parameter) N in <A, N extends number>(self: Iterable<A>, n: N): Array<TupleOf<N, A>>N): interface Array<T>Array<type TupleOf<N extends number, T> = N extends N ? number extends N ? T[] : TupleOf_<T, N, []> : neverConstructs a tuple type with exactly N elements of type T.
When to use
Use when you need a fixed-length array type, especially instead of manually
writing [T, T, T, ...] for longer tuples.
Details
- If
N is a literal number, produces a tuple of that exact length.
- If
N is the general number type (non-literal), degrades to Array<T>.
- Negative numbers produce
never.
Example (Checking fixed-length tuples)
import type { Types } from "effect"
// Exactly 3 numbers
const triple: Types.TupleOf<3, number> = [1, 2, 3]
//
TupleOf<function (type parameter) N in <A, N extends number>(self: Iterable<A>, n: N): Array<TupleOf<N, A>>N, function (type parameter) A in <A, N extends number>(self: Iterable<A>, n: N): Array<TupleOf<N, A>>A>>
} = dual<(...args: Array<any>) => any, <A>(self: Iterable<A>, n: number) => Array<Array<A>>>(arity: 2, body: <A>(self: Iterable<A>, n: number) => Array<Array<A>>): ((...args: Array<any>) => any) & (<A>(self: Iterable<A>, n: number) => Array<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>, n: number): Array<Array<A>>A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>, n: number): Array<Array<A>>A>, n: numbern: number): interface Array<T>Array<interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, n: number): Array<Array<A>>A>> => {
const const input: A[]input = const fromIterable: <A>(
collection: Iterable<A>
) => A[]
Converts an Iterable to an Array.
When to use
Use to convert any Iterable (Set, Generator, etc.) into an array.
Details
If the input is already an array, this returns it by reference without
copying. Otherwise, it creates a new array from the iterable. Use copy if
you need a fresh array even when the input is already an array.
Example (Converting a Set to an array)
import { Array } from "effect"
const result = Array.fromIterable(new Set([1, 2, 3]))
console.log(result) // [1, 2, 3]
fromIterable(self: Iterable<A>self)
if (n: numbern > 0 && const isReadonlyArrayNonEmpty: <A>(
self: ReadonlyArray<A>
) => self is NonEmptyReadonlyArray<A>
Checks whether a ReadonlyArray is non-empty, narrowing the type to
NonEmptyReadonlyArray.
When to use
Use when you need to prove a readonly array has at least one element without
requiring mutable array methods afterward.
Example (Checking for a non-empty readonly array)
import { Array } from "effect"
console.log(Array.isReadonlyArrayNonEmpty([])) // false
console.log(Array.isReadonlyArrayNonEmpty([1, 2, 3])) // true
isReadonlyArrayNonEmpty(const input: A[]input)) {
return const Array: ArrayConstructorExposes the global array constructor.
When to use
Use to access native JavaScript array constructor methods such as isArray
or from from the Effect module namespace.
Example (Accessing the Array constructor)
import { Array } from "effect"
const arr = new Array.Array(3)
console.log(arr) // [undefined, undefined, undefined]
Array.ArrayConstructor.from<unknown, A[]>(iterable: Iterable<unknown> | ArrayLike<unknown>, mapfn: (v: unknown, k: number) => A[], thisArg?: any): A[][] (+3 overloads)Creates an array from an iterable object.
from(
{ ArrayLike<T>.length: numberlength: const input: A[] & readonly [A, ...A[]]input.Array<T>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length - (n: numbern - 1) },
(_: unknown_, index: numberindex) => const input: A[] & readonly [A, ...A[]]input.function slice(start?: number, end?: number): A[]Returns a copy of a section of an array.
For both start and end, a negative index can be used to indicate an offset from the end of the array.
For example, -2 refers to the second to last element of the array.
slice(index: numberindex, index: numberindex + n: numbern)
)
}
return []
})