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 [x :int])
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)
return the none Option[A] (NULL).
0 (NULL) representing the absent option.
(none) ; => none
Since: Phase TC1
some?
(some? [o :int])
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 [o :int dflt :int])
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
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 [o :int f])
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)
Since: Phase TC1
option-eq?
(option-eq? [o1 :int o2 :int cmp-fn])
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.