tur/option
typed Option[A]: parameterized optional value.
Since: Phase TC1-D
Option
(defstruct Option [A])
parameterized optional value containing either some A or none.
Since: Phase TC1
some
(some [A])
wrap a value of type A in a some Option[A].
| x | the value to wrap (type A, stored as int64_t) |
A heap-allocated Option[A] with is-some=true.
(some 42) ; => some(42)
Since: Phase TC1
none
(none [A])
return the none Option[A] (NULL).
0 (NULL) representing the absent option.
(none) ; => none
Since: Phase TC1
some?
(some? [A])
check whether an Option[A] contains a value.
| o | Option[A] |
true if o is some, false if o is none.
(some? (some 1)) ; => true (some? (none)) ; => false
Since: Phase TC1
unwrap
(unwrap [A])
extract the value from a typed Option[A].
| o | Option[A] |
The contained value of type A.
(let [o (:: (make-struct Option true 7) (Option int))]
(unwrap o)) ; => 7
Since: Phase TC1
unwrap-or
(unwrap-or [A])
extract the value or return a default.
| o | Option[A] | |
| dflt | value to return if o is none (type A) |
The contained value, or default if none.
(unwrap-or (none) 99) ; => 99 (unwrap-or (some 7) 99) ; => 7
Since: Phase TC1
unwrap-or-carrier
(unwrap-or-carrier [o : int dflt : int])
carrier-ABI shim for the Track A cascade: extract the
option-free
(option-free [o : int])
free a heap-allocated Option[A].
| o | Option[A] to free (may be none / 0) |
(option-free my-opt)
Since: Phase TC1
option-map
(option-map [A B])
apply a function to the contained value if some.
| o | Option[A] | |
| f | function A -> B |
Option[B] with f applied if some, or none.
(option-map (some 3) (fn [x] (* x 2))) ; => some(6) Note: Mapping over a *literal* `(none)` with an *unannotated* lambda leaves the element type under-determined (`(none)` carries no element type) and fails to compile -- annotate the lambda or map over a typed Option value. See docs/reported/option-map-literal-none-unannotated-fn-no-A-inference.md.
Since: Phase TC1
option-eq?
(option-eq? [A])
compare two Option[A] values using a comparator.
| o1 | first Option[A] | |
| o2 | second Option[A] | |
| cmp-fn | comparator fn [a :int b :int] :bool |
true if both none, or both some with equal values.
(option-eq? (some 1) (some 1) (fn [a b] (= a b))) ; => true
Since: Phase TC1
Eq[Option]
(definstance Eq [Option])
equality using the inner type's Eq instance.
Functor[Option]
(definstance Functor [Option])
map a function over the some payload; none unchanged.
(fmap (some 21) (fn [x] (* x 2))) ; => some(42)
Since: stdlib-hkt-consolidation T1
Applicative[Option]
(definstance Applicative [Option])
pure wraps in some; ap applies a some function to a
(ap (some f) (some 3)) ; => some(f 3)
Since: stdlib-hkt-consolidation T1
Monad[Option]
(definstance Monad [Option])
bind sequences through some; short-circuits on none.
(bind (some 20) (fn [x] (some (+ x 1)))) ; => some(21)
Since: stdlib-hkt-consolidation T1
Alternative[Option]
(definstance Alternative [Option])
empty is none; alt-or picks the first some.
(alt-or (none) (some 7)) ; => some(7)
Since: stdlib-hkt-consolidation T1