7.6

#### 1.4Option 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>

 procedure(option? v) → boolean? v : any/c
A predicate for option values, which are either present or absent.

 procedure(present? v) → boolean? v : any/c
A predicate for present values. Implies option?.

 procedure(present v) → present? v : any/c
Constructs a present value containing v. Additionally, can be used as a match expander to match present values and unwrap them.

Example:
 > (match (present 42) [(present x) (add1 x)] [(== absent) 0])

43

 procedure(present-value pres) → any/c pres : present?
Returns the value wrapped by pres.

 procedure(absent? v) → boolean? v : any/c
A predicate for the absent value. Implies option?.

 value
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!

 procedure(option-map opt f) → option? opt : option? f : (-> any/c any/c)
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) #

 procedure(option-flat-map opt f) → option? opt : option? f : (-> any/c option?)
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?) # > (option-filter absent number?) #

 procedure(option-get opt default) → any/c opt : option? default : any/c
Returns the value in opt if it is present, otherwise returns default.

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.1Contracts 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.