<A, X>(f: (a: A) => Option<X>): (self: Option<A>) => Option<A>
<A, X>(self: Option<A>, f: (a: A) => Option<X>): Option<A>Runs a side-effecting Option-returning function on the value of a Some,
returning the original Option if the function returns Some, or None
if it returns None.
When to use
Use to validate an Option's present value without transforming it, such as
adding a side-condition check in a pipeline.
Details
None→NoneSome→ callsf(value); if result isSome, returns originalself; ifNone, returnsNone
Example (Validating without transforming)
import { Option } from "effect"
const getInteger = (n: number) =>
Number.isInteger(n) ? Option.some(n) : Option.none()
console.log(Option.tap(Option.some(1), getInteger))
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
console.log(Option.tap(Option.some(1.14), getInteger))
// Output: { _id: 'Option', _tag: 'None' }export const const tap: {
<A, X>(f: (a: A) => Option<X>): (
self: Option<A>
) => Option<A>
<A, X>(
self: Option<A>,
f: (a: A) => Option<X>
): Option<A>
}
Runs a side-effecting Option-returning function on the value of a Some,
returning the original Option if the function returns Some, or None
if it returns None.
When to use
Use to validate an Option's present value without transforming it, such as
adding a side-condition check in a pipeline.
Details
None → None
Some → calls f(value); if result is Some, returns original self; if None, returns None
Example (Validating without transforming)
import { Option } from "effect"
const getInteger = (n: number) =>
Number.isInteger(n) ? Option.some(n) : Option.none()
console.log(Option.tap(Option.some(1), getInteger))
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
console.log(Option.tap(Option.some(1.14), getInteger))
// Output: { _id: 'Option', _tag: 'None' }
tap: {
<function (type parameter) A in <A, X>(f: (a: A) => Option<X>): (self: Option<A>) => Option<A>A, function (type parameter) X in <A, X>(f: (a: A) => Option<X>): (self: Option<A>) => Option<A>X>(f: (a: A) => Option<X>f: (a: Aa: function (type parameter) A in <A, X>(f: (a: A) => Option<X>): (self: Option<A>) => Option<A>A) => type Option<A> = None<A> | 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) X in <A, X>(f: (a: A) => Option<X>): (self: Option<A>) => Option<A>X>): (self: Option<A>self: type Option<A> = None<A> | 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, X>(f: (a: A) => Option<X>): (self: Option<A>) => Option<A>A>) => type Option<A> = None<A> | 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, X>(f: (a: A) => Option<X>): (self: Option<A>) => Option<A>A>
<function (type parameter) A in <A, X>(self: Option<A>, f: (a: A) => Option<X>): Option<A>A, function (type parameter) X in <A, X>(self: Option<A>, f: (a: A) => Option<X>): Option<A>X>(self: Option<A>self: type Option<A> = None<A> | 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, X>(self: Option<A>, f: (a: A) => Option<X>): Option<A>A>, f: (a: A) => Option<X>f: (a: Aa: function (type parameter) A in <A, X>(self: Option<A>, f: (a: A) => Option<X>): Option<A>A) => type Option<A> = None<A> | 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) X in <A, X>(self: Option<A>, f: (a: A) => Option<X>): Option<A>X>): type Option<A> = None<A> | 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, X>(self: Option<A>, f: (a: A) => Option<X>): Option<A>A>
} = dual<(...args: Array<any>) => any, <A, X>(self: Option<A>, f: (a: A) => Option<X>) => Option<A>>(arity: 2, body: <A, X>(self: Option<A>, f: (a: A) => Option<X>) => Option<A>): ((...args: Array<any>) => any) & (<A, X>(self: Option<A>, f: (a: A) => Option<X>) => Option<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, X>(self: Option<A>, f: (a: A) => Option<X>): Option<A>A, function (type parameter) X in <A, X>(self: Option<A>, f: (a: A) => Option<X>): Option<A>X>(self: Option<A>self: type Option<A> = None<A> | 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, X>(self: Option<A>, f: (a: A) => Option<X>): Option<A>A>, f: (a: A) => Option<X>f: (a: Aa: function (type parameter) A in <A, X>(self: Option<A>, f: (a: A) => Option<X>): Option<A>A) => type Option<A> = None<A> | 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) X in <A, X>(self: Option<A>, f: (a: A) => Option<X>): Option<A>X>): type Option<A> = None<A> | 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, X>(self: Option<A>, f: (a: A) => Option<X>): Option<A>A> => const flatMap: {
<A, B>(f: (a: A) => Option<B>): (
self: Option<A>
) => Option<B>
<A, B>(
self: Option<A>,
f: (a: A) => Option<B>
): Option<B>
}
flatMap(self: Option<A>self, (a: Aa) => const map: {
<A, B>(f: (a: A) => B): (
self: Option<A>
) => Option<B>
<A, B>(
self: Option<A>,
f: (a: A) => B
): Option<B>
}
map(f: (a: A) => Option<X>f(a: Aa), () => a: Aa)))