<B, C>(bfc: (b: B) => Option<C>): <A>(
afb: (a: A) => Option<B>
) => (a: A) => Option<C>
<A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (
a: A
) => Option<C>Composes two Option-returning functions into a single function that chains
them together.
When to use
Use when you need to compose two functions that each return an Option, so
None short-circuits without calling the next function.
Details
- Calls
afb(a), then ifSome, callsbfcwith its value - Short-circuits to
Noneif either function returnsNone
Example (Composing parsers)
import { Option } from "effect"
const parse = (s: string): Option.Option<number> =>
isNaN(Number(s)) ? Option.none() : Option.some(Number(s))
const double = (n: number): Option.Option<number> =>
n > 0 ? Option.some(n * 2) : Option.none()
const parseAndDouble = Option.composeK(parse, double)
console.log(parseAndDouble("42"))
// Output: { _id: 'Option', _tag: 'Some', value: 84 }
console.log(parseAndDouble("not a number"))
// Output: { _id: 'Option', _tag: 'None' }export const const composeK: {
<B, C>(bfc: (b: B) => Option<C>): <A>(
afb: (a: A) => Option<B>
) => (a: A) => Option<C>
<A, B, C>(
afb: (a: A) => Option<B>,
bfc: (b: B) => Option<C>
): (a: A) => Option<C>
}
Composes two Option-returning functions into a single function that chains
them together.
When to use
Use when you need to compose two functions that each return an Option, so
None short-circuits without calling the next function.
Details
- Calls
afb(a), then if Some, calls bfc with its value
- Short-circuits to
None if either function returns None
Example (Composing parsers)
import { Option } from "effect"
const parse = (s: string): Option.Option<number> =>
isNaN(Number(s)) ? Option.none() : Option.some(Number(s))
const double = (n: number): Option.Option<number> =>
n > 0 ? Option.some(n * 2) : Option.none()
const parseAndDouble = Option.composeK(parse, double)
console.log(parseAndDouble("42"))
// Output: { _id: 'Option', _tag: 'Some', value: 84 }
console.log(parseAndDouble("not a number"))
// Output: { _id: 'Option', _tag: 'None' }
composeK: {
<function (type parameter) B in <B, C>(bfc: (b: B) => Option<C>): <A>(afb: (a: A) => Option<B>) => (a: A) => Option<C>B, function (type parameter) C in <B, C>(bfc: (b: B) => Option<C>): <A>(afb: (a: A) => Option<B>) => (a: A) => Option<C>C>(bfc: (b: B) => Option<C>bfc: (b: Bb: function (type parameter) B in <B, C>(bfc: (b: B) => Option<C>): <A>(afb: (a: A) => Option<B>) => (a: A) => Option<C>B) => 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) C in <B, C>(bfc: (b: B) => Option<C>): <A>(afb: (a: A) => Option<B>) => (a: A) => Option<C>C>): <function (type parameter) A in <A>(afb: (a: A) => Option<B>): (a: A) => Option<C>A>(afb: (a: A) => Option<B>afb: (a: Aa: function (type parameter) A in <A>(afb: (a: A) => Option<B>): (a: A) => Option<C>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) B in <B, C>(bfc: (b: B) => Option<C>): <A>(afb: (a: A) => Option<B>) => (a: A) => Option<C>B>) => (a: Aa: function (type parameter) A in <A>(afb: (a: A) => Option<B>): (a: A) => Option<C>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) C in <B, C>(bfc: (b: B) => Option<C>): <A>(afb: (a: A) => Option<B>) => (a: A) => Option<C>C>
<function (type parameter) A in <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>A, function (type parameter) B in <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>B, function (type parameter) C in <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>C>(afb: (a: A) => Option<B>afb: (a: Aa: function (type parameter) A in <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>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) B in <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>B>, bfc: (b: B) => Option<C>bfc: (b: Bb: function (type parameter) B in <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>B) => 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) C in <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>C>): (a: Aa: function (type parameter) A in <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>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) C in <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>C>
} = import dualdual(2, <function (type parameter) A in <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>A, function (type parameter) B in <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>B, function (type parameter) C in <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>C>(afb: (a: A) => Option<B>afb: (a: Aa: function (type parameter) A in <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>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) B in <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>B>, bfc: (b: B) => Option<C>bfc: (b: Bb: function (type parameter) B in <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>B) => 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) C in <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>C>) => (a: Aa: function (type parameter) A in <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>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) C in <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>C> => 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(afb: (a: A) => Option<B>afb(a: Aa), bfc: (b: B) => Option<C>bfc))