import { Options } from "https://deno.land/x/eitherway@0.10.0/lib/core/option.ts";
const { any } = Options;
Use this to extract the first element of type Some<T>
from an
Option<T>[]
If no item is Some<T>
or the input array is empty, None
is returned
Examples
Example 1
Example 1
import { assert } from "./assert.ts";
import { Option, Options, None, Some } from "./option.ts";
type Prime = number;
const toPrime = function (n: number): Option<Prime> {
if (!Number.isSafeInteger(n) || n < 2) return None;
if (n % 2 === 0) return (n !== 2) ? None : Some(n);
if (n % 3 === 0) return (n !== 3) ? None : Some(n);
const m = Math.sqrt(n);
for (let i = 5; i <= m; i += 6) {
if (n % i === 0) return None;
if (n % (i + 2) === 0) return None;
}
return Some(n);
};
const makeRange = function* (start: number, end: number) {
let cursor = start;
while (cursor < end) {
yield cursor;
cursor += 1;
}
return cursor;
};
const maybePrimes: Option<Prime>[] = [...makeRange(9, 19)].map(toPrime);
const firstPrime = Options.any(maybePrimes);
assert(firstPrime.isSome() === true);
assert(firstPrime.unwrap() === 11);
Type Parameters
O extends ReadonlyArray<Option<unknown>>
Parameters
opts: O