|(require rebellion/base/option)||package: rebellion|
Constructs a present value containing v. Additionally, can be used as a match expander to match present values and unwrap them.
Returns the value wrapped by pres.
The absent constant.
Converts falsey-value to an option. If falsey-value is #false, absent is returned, otherwise falsey-value is wrapped as a present value.
Note that there is no specialized option->falsey function. In order to convert from an option to a falsey value, use option-get with #false as the default value.
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.
> (option-case (present 42) #:present add1 #:absent (λ () (error "missing!")))
> (option-case absent #:present add1 #:absent (λ () (error "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.
Applies f to the value contained in opt if it is present and returns the result of f. If opt is absent, returns absent.
(define (inverse x) (if (zero? x) absent (present (/ 1 x))))
> (option-flat-map (present 42) inverse)
> (option-flat-map absent inverse)
> (option-flat-map (present 0) inverse)
If opt is present, tests its value with pred and returns it if pred results in #t. Otherwise, returns absent.
> (option-filter (present 42) number?)
> (option-filter (present "hello") number?)
> (option-filter absent number?)
Returns first-opt if it’s present, otherwise returns the first present option from calling each opt-thunk. Returns absent if first-opt is absent and every opt-thunk returns absent.
Returns the value in opt if it is present, otherwise returns default.
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.
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)).
Constructs a contract for present values that satisfy contract.