7.7
3.3 Instances
Link to this section with
@secref["class:data" #:doc '(lib "algebraic/scribblings/algebraic.scrbl")]
Link to this section with
@secref["class:data" #:doc '(lib "algebraic/scribblings/algebraic.scrbl")]
3.3.1 Box
Link to this section with
@secref["class:data:box"
#:doc '(lib "algebraic/scribblings/algebraic.scrbl")]
Link to this section with
@secref["class:data:box"
#:doc '(lib "algebraic/scribblings/algebraic.scrbl")]
Examples:
Example:
Example:
3.3.2 Event
Link to this section with
@secref["class:data:event"
#:doc '(lib "algebraic/scribblings/algebraic.scrbl")]
Link to this section with
@secref["class:data:event"
#:doc '(lib "algebraic/scribblings/algebraic.scrbl")]
Examples:
Examples:
Examples:
3.3.3 List
Link to this section with
@secref["class:data:list"
#:doc '(lib "algebraic/scribblings/algebraic.scrbl")]
Link to this section with
@secref["class:data:list"
#:doc '(lib "algebraic/scribblings/algebraic.scrbl")]
Examples:
Example:
Examples:
Example:
Examples:
3.3.4 Maybe
Link to this section with
@secref["class:data:maybe"
#:doc '(lib "algebraic/scribblings/algebraic.scrbl")]
Link to this section with
@secref["class:data:maybe"
#:doc '(lib "algebraic/scribblings/algebraic.scrbl")]
The Maybe sum, and associated operations.
The
Maybe sum encapsulates an optional value. A member of
Maybe either contains a value
a (represented as
(Just a)), or it is empty (represented as
Nothing). Using
Maybe is a
good way to deal with errors or exceptional cases without resorting to drastic
measures such as
error.
The Maybe sum is also a monad. It is a simple kind of
error monad, where all errors are represented by Nothing. A richer
error monad can be built using the Either type.
Examples:
Examples:
extends MaybeApplicative
Examples:
3.3.5 Truthy
Link to this section with
@secref["class:data:truthy"
#:doc '(lib "algebraic/scribblings/algebraic.scrbl")]
Link to this section with
@secref["class:data:truthy"
#:doc '(lib "algebraic/scribblings/algebraic.scrbl")]
The Truthy sum, and associated operations.
Functions that return #f to indicate failure can be chained together
with and and or.
But #f does not always indicate failure.
> (hash-set! Δ 'B #f) |
|
28 |
> Δ |
'#hasheq((A . 40) (B . 28)) |
The Truthy monad uses the Fail product as a
failure indicator distinct from #f.
Truthy is like the Maybe sum if Just were
implicitly applied to all non-#f values.
Truthy is a lazy monad. With do~-notation, several
Truthy expressions can be evaluated in sequence. If an expression
evaluates to Fail, the sequence short circuits.
> (hash-set! Δ 'C #f) |
|
Fail |
> Δ |
'#hasheq((A . 40) (B . 28) (C . #f)) |
|
27 |
> Δ |
'#hasheq((A . 40) (B . 28) (C . #f) (D . 27)) |
As a convenience, truthy-and, truthy-not, and
truthy-or reduce the boilerplate.
|
#f |
> Δ |
'#hasheq((A . 40) (B . 28) (C . #f) (D . 27)) |
|
27 |
> Δ |
'#hasheq((A . 40) (B . 28) (C . #f) (D . 27)) |
3.3.6 Values
Link to this section with
@secref["class:data:Values"
#:doc '(lib "algebraic/scribblings/algebraic.scrbl")]
Link to this section with
@secref["class:data:Values"
#:doc '(lib "algebraic/scribblings/algebraic.scrbl")]
extends ValuesFunctor
Examples:
Example:
extends ValuesApplicative
Examples: