Skip to content

Latest commit

 

History

History
140 lines (95 loc) · 2.49 KB

option.md

File metadata and controls

140 lines (95 loc) · 2.49 KB
id stdlib-option
title Option Module
slug /stdlib/option

option is a native type with two cases: Some value and None.

Option.defaultValue : 'a -> 'a option -> 'a

Returns option value or a fallback.

let port = Some 8080 |> Option.defaultValue 80

Option.defaultWith : (unit -> 'a) -> 'a option -> 'a

Computes fallback lazily.

let value = None |> Option.defaultWith (fun () -> 42)

Option.isNone : 'a option -> bool

Returns true when option is None.

let missing = None |> Option.isNone

Option.isSome : 'a option -> bool

Returns true when option is Some.

let present = Some "x" |> Option.isSome

Option.map : ('a -> 'b) -> 'a option -> 'b option

Transforms value inside Some.

let upper = Some "fscript" |> Option.map String.toUpper

Pure support additions

Option.bind

Option.bind : ('a -> 'b option) -> 'a option -> 'b option

Calls the binder only for Some; its option result is returned directly.

Option.bind Int.tryParse (Some "42")
// Some 42

Option.flatten

Option.flatten : 'a option option -> 'a option

Removes one nested option layer. Both None and Some None produce None.

Option.flatten (Some (Some 2))
// Some 2

Option.filter

Option.filter : ('a -> bool) -> 'a option -> 'a option

Retains a present value only when the predicate is true. A missing value makes no callback call.

Option.filter (fun x -> x>1) (Some 1)
// None

Option.orElse

Option.orElse : 'a option -> 'a option -> 'a option

Uses the fallback when the final argument is None. Arguments are evaluated eagerly; use orElseWith for lazy fallback computation.

Option.orElse (Some 2) None
// Some 2

Option.orElseWith

Option.orElseWith : (unit -> 'a option) -> 'a option -> 'a option

Calls the fallback with unit only when the final argument is None.

Option.orElseWith (fun () -> Some 2) None
// Some 2

Option.map2

Option.map2 : ('a -> 'b -> 'd) -> 'a option -> 'b option -> 'd option

Calls the mapper only when both options are present; otherwise returns None.

Option.map2 (fun a b -> a+b) (Some 1) (Some 2)
// Some 3

Option.toList

Option.toList : 'a option -> 'a list

Returns a singleton list for Some or an empty list for None.

Option.toList (Some 1)
// [1]