7.7
1.4 Option Values
(require rebellion/base/option) | package: rebellion |
An option is an optional value that is either present or absent. Present values are constructed with the present function, and absent values are represented by the absent constant.
Examples:
(define (hash-ref-option h k) (if (hash-has-key? h k) (present (hash-ref h k)) absent))
> (hash-ref-option (hash 'a 1 'b 2) 'a) (present 1)
> (hash-ref-option (hash 'a 1 'b 2) 'c) #<absent>
Constructs a present value containing v. Additionally, can be
used as a match expander to match present values and
unwrap them.
Example:
procedure
(present-value pres) → any/c
pres : present?
Returns the value wrapped by pres.
The absent constant.
procedure
(option-case opt #:present present-handler #:absent absent-handler) → any/c opt : option? present-handler : (-> any/c any/c) absent-handler : (-> any/c)
Inspects opt and, if it is present, calls present-handler on the present value. If it is absent, calls absent-handler. Returns the result of the called handler.
Examples:
> (option-case (present 42) #:present add1 #:absent (λ () (error "missing!"))) 43
> (option-case absent #:present add1 #:absent (λ () (error "missing!"))) missing!
Applies f to the value contained in opt if it is present and returns the result wrapped in a present value. If opt is absent, returns absent.
Examples:
> (option-map (present 42) add1) (present 43)
> (option-map absent add1) #<absent>
Applies f to the value contained in opt if it is present and returns the result of f. If opt is absent, returns absent.
Examples:
(define (inverse x) (if (zero? x) absent (present (/ 1 x))))
> (option-flat-map (present 42) inverse) (present 1/42)
> (option-flat-map absent inverse) #<absent>
> (option-flat-map (present 0) inverse) #<absent>
procedure
(option-filter opt pred) → option?
opt : option? pred : predicate/c
If opt is present, tests its value with pred and
returns it if pred results in #t. Otherwise, returns absent.
Examples:
> (option-filter (present 42) number?) (present 42)
> (option-filter (present "hello") number?) #<absent>
> (option-filter absent number?) #<absent>
procedure
(option-get opt default) → any/c
opt : option? default : any/c
Examples:
> (option-get (present 42) 0) 42
> (option-get absent 0) 0
procedure
(in-option opt) → (sequence/c any/c)
opt : option?
Returns either a sequence that contains the present value of
opt, or an empty sequence if opt is absent. This
function is particularly useful in transduce pipelines, as it can be
used with append-mapping and similar transducers in order to
safely filter out absent values while simultaneously unwrapping present
values.
1.4.1 Contracts for Option Values
procedure
(option/c contract) → chaperone-contract?
contract : chaperone-contract?
Constructs a contract for option values that accepts either the absent value or a present value that satisfies contract.
Equivalent to (or/c absent? (present/c contract)).
procedure
(present/c contract) → chaperone-contract?
contract : chaperone-contract?
Constructs a contract for present values that satisfy contract.