<B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<Option<A>>) => B
<A, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): BReduces an iterable of Options to a single value, skipping None entries.
When to use
Use when you need to aggregate values from a collection where some may be absent.
Details
- Iterates through the collection, applying
fonly toSomevalues Nonevalues are skipped entirely- Returns the accumulated result
Example (Summing present values)
import { Option, pipe } from "effect"
const items = [Option.some(1), Option.none(), Option.some(2), Option.none()]
console.log(pipe(items, Option.reduceCompact(0, (b, a) => b + a)))
// Output: 3export const const reduceCompact: {
<B, A>(b: B, f: (b: B, a: A) => B): (
self: Iterable<Option<A>>
) => B
<A, B>(
self: Iterable<Option<A>>,
b: B,
f: (b: B, a: A) => B
): B
}
Reduces an iterable of Options to a single value, skipping None entries.
When to use
Use when you need to aggregate values from a collection where some may be
absent.
Details
- Iterates through the collection, applying
f only to Some values
None values are skipped entirely
- Returns the accumulated result
Example (Summing present values)
import { Option, pipe } from "effect"
const items = [Option.some(1), Option.none(), Option.some(2), Option.none()]
console.log(pipe(items, Option.reduceCompact(0, (b, a) => b + a)))
// Output: 3
reduceCompact: {
<function (type parameter) B in <B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<Option<A>>) => BB, function (type parameter) A in <B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<Option<A>>) => BA>(b: Bb: function (type parameter) B in <B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<Option<A>>) => BB, f: (b: B, a: A) => Bf: (b: Bb: function (type parameter) B in <B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<Option<A>>) => BB, a: Aa: function (type parameter) A in <B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<Option<A>>) => BA) => function (type parameter) B in <B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<Option<A>>) => BB): (self: Iterable<Option<A>>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<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 <B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<Option<A>>) => BA>>) => function (type parameter) B in <B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<Option<A>>) => BB
<function (type parameter) A in <A, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): BA, function (type parameter) B in <A, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): BB>(self: Iterable<Option<A>>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<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, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): BA>>, b: Bb: function (type parameter) B in <A, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): BB, f: (b: B, a: A) => Bf: (b: Bb: function (type parameter) B in <A, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): BB, a: Aa: function (type parameter) A in <A, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): BA) => function (type parameter) B in <A, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): BB): function (type parameter) B in <A, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): BB
} = import dualdual(
3,
<function (type parameter) A in <A, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): BA, function (type parameter) B in <A, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): BB>(self: Iterable<Option<A>>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<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, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): BA>>, b: Bb: function (type parameter) B in <A, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): BB, f: (b: B, a: A) => Bf: (b: Bb: function (type parameter) B in <A, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): BB, a: Aa: function (type parameter) A in <A, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): BA) => function (type parameter) B in <A, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): BB): function (type parameter) B in <A, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): BB => {
let let out: Bout: function (type parameter) B in <A, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): BB = b: Bb
for (const const oa: Option<A>oa of self: Iterable<Option<A>>self) {
if (const isSome: <A>(
self: Option<A>
) => self is Some<A>
Checks whether an Option contains a value (Some).
When to use
Use when you need to branch on a present Option before accessing .value.
Details
- Acts as a type guard, narrowing to
Some<A>
Example (Checking for Some)
import { Option } from "effect"
console.log(Option.isSome(Option.some(1)))
// Output: true
console.log(Option.isSome(Option.none()))
// Output: false
isSome(const oa: Option<A>oa)) {
let out: Bout = f: (b: B, a: A) => Bf(let out: Bout, const oa: Some<A>const oa: {
_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;
}
oa.Some<A>.value: Avalue)
}
}
return let out: Bout
}
)