Returns the contained Some value or a provided default.
Arguments passed to unwrapOr
are eagerly evaluated; if you are passing
the result of a function call, it is recommended to use unwrapOrElse,
which is lazily evaluated.
assert.equal(Some('car').unwrapOr('bike'), 'car');
assert.equal(None.unwrapOr('bike'), 'bike');
Returns the contained Some value or computes it from a closure.
const k = 10;
assert.equal(Some(4).unwrapOrElse(() => 2 * k), 4);
assert.equal(None.unwrapOrElse(() => 2 * k), 20);
Maps an Option<T>
to Option<U>
by applying a function to a contained value.
Converts an Option<string>
into an Option<number>
, consuming
the original:
const maybeSomeString = Some('Hello, World!');
const maybeSomeLen = maybeSomeString.map((s) => s.length);
assert.deepEqual(maybeSomeLen, Some(13));
Returns the provided default result (if none), or applies a function to the contained value (if any).
Arguments passed to map_or
are eagerly evaluated; if you are passing
the result of a function call, it is recommended to use mapOrElse,
which is lazily evaluated.
const x = Some('foo');
assert.equal(x.mapOr(42, (v) => v.length), 3);
const y = None as Option<string>;
assert.equal(y.mapOr(42, (v) => v.length), 42);
Computes a default function result (if none), or applies a different function to the contained value (if any).
const k = 21;
const x = Some('foo');
assert.equal(x.mapOrElse(() => 2 * k, (v) => v.length), 3);
const y = None as Option<string>;
assert.equal(y.mapOrElse(() => 2 * k, (v) => v.length), 42);
Returns None if the option is None, otherwise returns optb
.
const a = Some(2);
const b: Option<string> = None;
assert.deepEqual(a.and(b), None);
const c: Option<number> = None;
const d = Some('foo');
assert.deepEqual(c.and(d), None);
const e = Some(2);
const f = Some('foo');
assert.deepEqual(e.and(f), Some('foo'));
const g: Option<number> = None;
const h: Option<string> = None;
assert.deepEqual(g.and(h), None);
Returns None if the option is None, otherwise calls f
with the
wrapped value and returns the result.
Some languages call this operation flatmap.
const evenThenToString = (x: number): Option<string> => {
return x % 2 === 0 ? Some(x.toString()) : None;
};
assert.deepEqual(Some(2).andThen(evenThenToString), Some('2'));
assert.deepEqual(Some(5).andThen(evenThenToString), None); // odd!
assert.deepEqual(None.andThen(evenThenToString), None);
Often used to chain fallible operations that may return None.
const arr2d = [['A0', 'A1'], ['B0', 'B1']];
const item01 = Option.fromUndefinable(arr2d.at(0)).andThen((row) => {
return Option.fromUndefinable(row.at(1));
});
assert.deepEqual(item01, Some('A1'));
const item20 = Option.fromUndefinable(arr2d.at(2)).andThen((row) => {
return Option.fromUndefinable(row.at(0));
});
assert.deepEqual(item20, None);
Returns None if the option is None, otherwise calls predicate
with the wrapped value and returns:
predicate
returns true
(where t
is the wrapped
value), andpredicate
returns false
.This function works similar to Array.prototype.filter. You can imagine
the Option<T>
being an array over one or zero elements. filter()
lets you decide which elements to keep.
const isEven = (n: number): boolean => n % 2 == 0;
assert.deepEqual(None.filter(isEven), None);
assert.deepEqual(Some(3).filter(isEven), None);
assert.deepEqual(Some(4).filter(isEven), Some(4));
Returns the option if it contains a value, otherwise returns optb
.
Arguments passed to or
are eagerly evaluated; if you are passing the
result of a function call, it is recommended to use orElse, which is
lazily evaluated.
const a = Some(2);
const b = None;
assert.deepEqual(a.or(b), Some(2));
const c = None;
const d = Some(100);
assert.deepEqual(c.or(d), Some(100));
const e = Some(2);
const f = Some(100);
assert.deepEqual(e.or(f), Some(2));
const g = None as Option<number>;
const h = None;
assert.deepEqual(g.or(h), None);
Returns the option if it contains a value, otherwise calls f
and
returns the result.
const nobody = (): Option<string> => None;
const vikings = (): Option<string> => Some('vikings');
assert.deepEqual(Some('barbarians').orElse(vikings), Some('barbarians'));
assert.deepEqual(None.orElse(vikings), Some('vikings'));
assert.deepEqual(None.orElse(nobody), None);
Returns Some if exactly one of this
, optb
is Some, otherwise returns None.
const a = Some(2);
const b = None as Option<number>;
assert.deepEqual(a.xor(b), Some(2));
const c = None as Option<number>;
const d = Some(2);
assert.deepEqual(c.xor(d), Some(2));
const e = Some(2);
const f = Some(2);
assert.deepEqual(e.xor(f), None);
const g = None as Option<number>;
const h = None as Option<number>;
assert.deepEqual(g.xor(h), None);
Zips this
with another Option
.
If this
is Some(s)
and other
is Some(o)
, this method returns Some((s, o))
.
Otherwise, None
is returned.
const x = Some(1);
const y = Some('hi');
const z = None as Option<number>;
assert.deepEqual(x.zip(y), Some([1, 'hi']));
assert.deepEqual(x.zip(z), None);
Converts from Option<Option<T>>
to Option<T>
.
Basic usage:
const x: Option<Option<number>> = Some(Some(6));
assert.deepEqual(x.flatten(), Some(6));
const y: Option<Option<number>> = Some(None);
assert.deepEqual(y.flatten(), None);
const z = None as Option<Option<number>>;
assert.deepEqual(z.flatten(), None);
Flattening only removes one level of nesting at a time:
const x: Option<Option<Option<number>>> = Some(Some(Some(6)));
assert.deepEqual(x.flatten(), Some(Some(6)));
assert.deepEqual(x.flatten().flatten(), Some(6));
Generated using TypeDoc
The Option type. See the module level documentation for more.
In Rust
See
Option