(n: number): <A>(self: Iterable<A>) => Array<A>
<A>(self: Iterable<A>, n: number): Array<A>Keeps the first n elements, creating a new array.
When to use
Use to keep up to the first n elements from an iterable as a new array.
Details
n is clamped to [0, length]. Returns an empty array when n <= 0.
Example (Taking from the start)
import { Array } from "effect"
console.log(Array.take([1, 2, 3, 4, 5], 3)) // [1, 2, 3]export const const take: {
(n: number): <A>(self: Iterable<A>) => Array<A>
<A>(self: Iterable<A>, n: number): Array<A>
}
Keeps the first n elements, creating a new array.
When to use
Use to keep up to the first n elements from an iterable as a new array.
Details
n is clamped to [0, length]. Returns an empty array when n <= 0.
Example (Taking from the start)
import { Array } from "effect"
console.log(Array.take([1, 2, 3, 4, 5], 3)) // [1, 2, 3]
take: {
(n: numbern: number): <function (type parameter) A in <A>(self: Iterable<A>): Array<A>A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>): Array<A>A>) => interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>): Array<A>A>
<function (type parameter) A in <A>(self: Iterable<A>, n: number): 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<A>A>, n: numbern: number): interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, n: number): Array<A>A>
} = dual<(...args: Array<any>) => any, <A>(self: Iterable<A>, n: number) => Array<A>>(arity: 2, body: <A>(self: Iterable<A>, n: number) => Array<A>): ((...args: Array<any>) => any) & (<A>(self: Iterable<A>, n: number) => 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<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<A>A>, n: numbern: number): interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, n: number): 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)
return const input: A[]input.Array<A>.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(0, const clamp: <A>(
i: number,
as: readonly A[]
) => number
clamp(n: numbern, const input: A[]input))
})