<A>(isEquivalent: (self: A, that: A) => boolean): (
self: Iterable<A>
) => Array<A>
<A>(
self: Iterable<A>,
isEquivalent: (self: A, that: A) => boolean
): Array<A>Removes consecutive duplicate elements using a custom equivalence.
When to use
Use when consecutive duplicates should be collapsed using a custom equivalence, while equivalent values that appear later should remain in the result.
Details
Non-adjacent duplicates are preserved.
Example (Deduplicating adjacent elements)
import { Array } from "effect"
console.log(Array.dedupeAdjacentWith([1, 1, 2, 2, 3, 3], (a, b) => a === b))
// [1, 2, 3]export const const dedupeAdjacentWith: {
<A>(
isEquivalent: (self: A, that: A) => boolean
): (self: Iterable<A>) => Array<A>
<A>(
self: Iterable<A>,
isEquivalent: (self: A, that: A) => boolean
): Array<A>
}
Removes consecutive duplicate elements using a custom equivalence.
When to use
Use when consecutive duplicates should be collapsed using a custom
equivalence, while equivalent values that appear later should remain in the
result.
Details
Non-adjacent duplicates are preserved.
Example (Deduplicating adjacent elements)
import { Array } from "effect"
console.log(Array.dedupeAdjacentWith([1, 1, 2, 2, 3, 3], (a, b) => a === b))
// [1, 2, 3]
dedupeAdjacentWith: {
<function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): (self: Iterable<A>) => Array<A>A>(isEquivalent: (self: A, that: A) => booleanisEquivalent: (self: Aself: function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): (self: Iterable<A>) => Array<A>A, that: Athat: function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): (self: Iterable<A>) => Array<A>A) => boolean): (self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): (self: Iterable<A>) => Array<A>A>) => interface Array<T>Array<function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): (self: Iterable<A>) => Array<A>A>
<function (type parameter) A in <A>(self: Iterable<A>, isEquivalent: (self: A, that: A) => boolean): Array<A>A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>, isEquivalent: (self: A, that: A) => boolean): Array<A>A>, isEquivalent: (self: A, that: A) => booleanisEquivalent: (self: Aself: function (type parameter) A in <A>(self: Iterable<A>, isEquivalent: (self: A, that: A) => boolean): Array<A>A, that: Athat: function (type parameter) A in <A>(self: Iterable<A>, isEquivalent: (self: A, that: A) => boolean): Array<A>A) => boolean): interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, isEquivalent: (self: A, that: A) => boolean): Array<A>A>
} = dual<(...args: Array<any>) => any, <A>(self: Iterable<A>, isEquivalent: (self: A, that: A) => boolean) => Array<A>>(arity: 2, body: <A>(self: Iterable<A>, isEquivalent: (self: A, that: A) => boolean) => Array<A>): ((...args: Array<any>) => any) & (<A>(self: Iterable<A>, isEquivalent: (self: A, that: A) => boolean) => 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>, isEquivalent: (self: A, that: A) => boolean): Array<A>A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>, isEquivalent: (self: A, that: A) => boolean): Array<A>A>, isEquivalent: (self: A, that: A) => booleanisEquivalent: (self: Aself: function (type parameter) A in <A>(self: Iterable<A>, isEquivalent: (self: A, that: A) => boolean): Array<A>A, that: Athat: function (type parameter) A in <A>(self: Iterable<A>, isEquivalent: (self: A, that: A) => boolean): Array<A>A) => boolean): interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, isEquivalent: (self: A, that: A) => boolean): Array<A>A> => {
const const out: A[]out: interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, isEquivalent: (self: A, that: A) => boolean): Array<A>A> = []
let let lastA: Option.Option<A>lastA: import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) A in <A>(self: Iterable<A>, isEquivalent: (self: A, that: A) => boolean): Array<A>A> = import OptionOption.const none: <A = never>() => Option<A>Creates an Option representing the absence of a value.
When to use
Use to represent a missing or uninitialized value, such as returning "no
result" from a function.
Details
- Returns
Option<never>, which is a subtype of Option<A> for any A
- Always returns the same singleton instance
Example (Creating an empty Option)
import { Option } from "effect"
// ┌─── Option<never>
// ▼
const noValue = Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
none()
for (const const a: Aa of self: Iterable<A>self) {
if (import OptionOption.const isNone: <A>(
self: Option<A>
) => self is None<A>
Checks whether an Option is None (absent).
When to use
Use when you need to branch on an absent Option before accessing .value.
Details
- Acts as a type guard, narrowing to
None<A>
Example (Checking for None)
import { Option } from "effect"
console.log(Option.isNone(Option.some(1)))
// Output: false
console.log(Option.isNone(Option.none()))
// Output: true
isNone(let lastA: Option.Option<A>lastA) || !isEquivalent: (self: A, that: A) => booleanisEquivalent(const a: Aa, let lastA: Option.Option<A>let lastA: {
_tag: "Some";
_op: "Some";
value: A;
valueOrUndefined: A;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
lastA.Some<A>.value: Avalue)) {
const out: A[]out.Array<A>.push(...items: A[]): numberAppends new elements to the end of an array, and returns the new length of the array.
push(const a: Aa)
let lastA: Option.Option<A>lastA = import OptionOption.const some: <A>(value: A) => Option<A>Wraps the given value into an Option to represent its presence.
When to use
Use to wrap a known present value as Option
- Returning a successful result from a partial function
Details
- Always returns
Some<A>
- Does not filter
null or undefined; use
fromNullishOr
for that
Example (Wrapping a value)
import { Option } from "effect"
// ┌─── Option<number>
// ▼
const value = Option.some(1)
console.log(value)
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
some(const a: Aa)
}
}
return const out: A[]out
})