Belt.OptionUtilities for option data type.
Utilities for option data type
keep optionValue p
If optionValue is Some value and p value = true, it returns Some value; otherwise returns None
keep (Some 10)(fun x -> x > 5);; (* returns [Some 10] *)
keep (Some 4)(fun x -> x > 5);; (* returns [None] *)
keep None (fun x -> x > 5);; (* returns [None] *)forEach optionValue f
If optionValue is Some value, it calls f value; otherwise returns ()
forEach (Some "thing")(fun x -> Js.log x);; (* logs "thing" *)
forEach None (fun x -> Js.log x);; (* returns () *)getExn optionalValue Returns value if optionalValue is Some value, otherwise raises getExn
getExn (Some 3) = 3;;
getExn None (* Raises getExn error *)getUnsafe x returns x This is an unsafe operation, it assumes x is neither not None or (Some (None .. ))
Uncurried version of mapWithDefault
mapWithDefault optionValue default f
If optionValue is Some value, returns f value; otherwise returns default
mapWithDefault (Some 3) 0 (fun x -> x + 5) = 8;;
mapWithDefault None 0 (fun x -> x + 5) = 0;;map optionValue f
If optionValue is Some value, returns Some (f value); otherwise returns None
map (Some 3) (fun x -> x * x) = (Some 9);;
map None (fun x -> x * x) = None;;Uncurried version of flatMap
flatMap optionValue f
If optionValue is Some value, returns f value; otherwise returns None The function f must have a return type of 'a option
let f (x : float) =
if x >= 0.0 then
Some (sqrt x)
else
None;;
flatMap (Some 4.0) f = Some 2.0;;
flatMap (Some (-4.0)) f = None;;
flatMap None f = None;;getWithDefault optionalValue default
If optionalValue is Some value, returns value, otherwise default
getWithDefault (Some 1812) 1066 = 1812;;
getWithDefault None 1066 = 1066;;orElse optionalValue otherOptional
If optionalValue is Some value, returns Some value, otherwise otherOptional
orElse (Some 1812) (Some 1066) = Some 1812;;
orElse None (Some 1066) = Some 1066;;
orElse None None = None;;Uncurried version of eq
eq optValue1 optvalue2 predicate
Evaluates two optional values for equality with respect to a predicate function.
If both optValue1 and optValue2 are None, returns true.
If one of the arguments is Some value and the other is None, returns false
If arguments are Some value1 and Some value2, returns the result of predicate value1 value2; the predicate function must return a bool
let clockEqual = (fun a b -> a mod 12 = b mod 12);;
eq (Some 3) (Some 15) clockEqual = true;;
eq (Some 3) None clockEqual = false;;
eq None (Some 3) clockEqual = false;;
eq None None clockEqual = true;;Uncurried version of cmp
cmp optValue1 optvalue2 comparisonFcn
Compares two optional values with respect to a comparison function
If both optValue1 and optValue2 are None, returns 0.
If the first argument is Some value1 and the second is None, returns 1 (something is greater than nothing)
If the first argument is None and the second is Some value2, returns -1 (nothing is less than something)
If the arguments are Some value1 and Some value2, returns the result of comparisonFcn value1 value2; comparisonFcn takes two arguments and returns -1 if the first argument is less than the second, 0 if the arguments are equal, and 1 if the first argument is greater than the second.
let clockCompare = fun a b -> compare (a mod 12) (b mod 12);;
cmp (Some 3) (Some 15) clockCompare = 0;;
cmp (Some 3) (Some 14) clockCompare = 1;;
cmp (Some 2) (Some 15) clockCompare = -1;;
cmp None (Some 15) clockCompare = -1;;
cmp (Some 14) None clockCompare = 1;;
cmp None None clockCompare = 0;;