Skip to content

Latest commit

 

History

History
294 lines (187 loc) · 4.43 KB

Option.md

File metadata and controls

294 lines (187 loc) · 4.43 KB

MODULE Option

Option

data

type Option<A> = None<A> | Some<A>

Represents optional values. Instances of Option are either an instance of Some or None

The most idiomatic way to use an Option instance is to treat it as a collection or monad and use map, flatMap or filter.

Methods

alt

(fa: Option<A>): Option<A>

alt short for alternative, takes another Option. If this Option is a Some type then it will be returned, if it is a None then it will return the next Some if it exist. If both are None then it will return none.

For example const someFn = (o: Option<number>) => o.alt(some(4))

someFn(some(2)) will return some(2).

someFn(none) will return some(4).

ap

<B>(fab: Option<(a: A) => B>): Option<B>

ap_

<B, C>(this: Option<(b: B) => C>, fb: Option<B>): Option<C>

chain

<B>(f: (a: A) => Option<B>): Option<B>

Returns the result of applying f to this Option's value if this Option is nonempty. Returns None if this Option is empty. Slightly different from map in that f is expected to return an Option (which could be None)

contains

(S: Setoid<A>, a: A): boolean

Returns true if the option has an element that is equal (as determined by S) to a, false otherwise

exists

(p: (a: A) => boolean): boolean

Returns true if this option is non empty and the predicate p returns true when applied to this Option's value

extend

<B>(f: (ea: Option<A>) => B): Option<B>

filter

(p: Predicate<A>): Option<A>

Returns this option if it is non empty and the predicate p return true when applied to this Option's value. Otherwise returns None

fold

<B>(b: B, some: (a: A) => B): B

Applies a function to each case in the data structure

foldL

<B>(none: () => B, some: (a: A) => B): B

Lazy verion of fold

getOrElse

(a: A): A

Returns the value from this Some or the given argument if this is a None

getOrElseL

(f: () => A): A

Lazy version of getOrElse

inspect

(): string

isNone

(): this is None<A>

Returns true if the option is None, false otherwise

isSome

(): this is Some<A>

Returns true if the option is an instance of Some, false otherwise

map

<B>(f: (a: A) => B): Option<B>

Takes a function f and an Option of A. Maps f either on None or Some, Option's data constructors. If it maps on Some then it will apply the f on Some's value, if it maps on None it will return None.

mapNullable

<B>(f: (a: A) => B | null | undefined): Option<B>

Maps f over this Option's value. If the value returned from f is null or undefined, returns None

reduce

<B>(b: B, f: (b: B, a: A) => B): B

toNullable

(): A | null

Returns the value from this Some or null if this is a None

toString

(): string

toUndefined

(): A | undefined

Returns the value from this Some or undefined if this is a None

option

instance

Monad1<URI> &
  Foldable1<URI> &
  Plus1<URI> &
  Traversable1<URI> &
  Alternative1<URI> &
  Extend1<URI>

fromEither

function

<L, A>(fa: Either<L, A>): Option<A>

fromNullable

function

<A>(a: A | null | undefined): Option<A>

Constructs a new Option from a nullable type. If the value is null or undefined, returns None, otherwise returns the value wrapped in a Some

fromPredicate

function

<A>(predicate: Predicate<A>) => (a: A): Option<A>

getFirstMonoid

function

<A = never>(): Monoid<Option<A>>

Option monoid returning the left-most non-None value

getLastMonoid

function

<A = never>(): Monoid<Option<A>>

Option monoid returning the right-most non-None value

getMonoid

function

<A>(S: Semigroup<A>): Monoid<Option<A>>

getSetoid

function

<A>(S: Setoid<A>): Setoid<Option<A>>

isNone

function

<A>(fa: Option<A>): fa is None<A>

Returns true if the option is None, false otherwise

isSome

function

<A>(fa: Option<A>): fa is Some<A>

Returns true if the option is an instance of Some, false otherwise

some

function Alias of

of

tryCatch

function

<A>(f: Lazy<A>): Option<A>