Next: Port positioning, Previous: Linear adjustable-length strings, Up: Library modules - SRFIs [Contents][Index]

`srfi-189`

- Maybe and Either: optional container types- Module:
**srfi-189** -
Maybe and Either types are immutable container types that can “wrap” zero or more values. Maybe can be used in the context where the value(s) may be missing (as opposed to “zero values”). Either can be used in the context where the value(s) can be correct ones or erroneous ones.

If you’re familiar with functional languages like Haskell, you may already know them. They are useful when used in the monadic pattern; that is, you can code as if the chain of calculations never fails, yet whenever the calculation fail at one place, the rest of chain is canceled and just the failure is returned.

Maybe is a union of two types, Just and Nothing. Just wraps valid value(s), while Nothing indicates there’s no meaningful values. Either is a union of two types, Right and Left. Right wraps valid value(s), while Left indicates an error, carrying some information as its payload.

Next: Maybe and Either constructors, Previous: Maybe and Either optional container types, Up: Maybe and Either optional container types [Contents][Index]

- Class:
**<maybe>** - Class:
**<just>** - Class:
**<nothing>** {

`srfi-189`} Maybe classes.`<just>`

and`<nothing>`

are subclasses of`<maybe>`

. An instance of`<just>`

carries zero or more values (payload), while an instance of`<nothing>`

doesn’t carry information.The

`<maybe>`

class is an abstract class and can’t create an instance of its own. Instances of`<just>`

and`<nothing>`

should be created with constructor procedures`just`

and`nothing`

.

- Class:
**<either>** - Class:
**<right>** - Class:
**<left>** {

`srfi-189`} Either classes.`<right>`

and`<left>`

are subclasses of`<either>`

. An instance of either class carry zero or more values (payload); it is customary to use`<right>`

to propagate the legitimate results of compuation, while use`<left>`

to propagate erroneous conditions whose payload describes what’s wrong.The

`<either>`

class is an abstract class and can’t create an instance of its own. Instances of`<right>`

and`<left>`

should be created with constructor procedures`right`

and`left`

.

- Function:
**maybe?***obj* - Function:
**just?***obj* - Function:
**nothing?***obj* {

`srfi-189`} [SRFI-189] Type predicates. Returns`#t`

iff`obj`is a Maybe, a Just, or a Nothing, respectively.

- Function:
**either?***obj* - Function:
**right?***obj* - Function:
**left?***obj* {

`srfi-189`} [SRFI-189] Type predicates. Returns`#t`

iff`obj`is a Either, a Right, or a Left, respectively.

- Function:
**maybe=***elt= maybe1 maybe …* {

`srfi-189`} [SRFI-189] Equality predicate of Maybes. It returns`#t`

iff all of`maybe1``maybe`… are Nothings, or Justs with their respective payload objects are the same using`elt=`.

- Function:
**either=***elt= either1 either …* {

`srfi-189`} [SRFI-189] Equality predicate of Eithers. It returns`#t`

iff all of`either11``either`… are the same types (all Rights, or all Lefts), with respective payload objects are the same using`elt=`.

Next: Maybe and Either accessors, Previous: Maybe and Either types and predicates, Up: Maybe and Either optional container types [Contents][Index]

- Function:
**just***obj …* {

`srfi-189`} [SRFI-189] Returns a Just with`obj`… as its payload.

- Function:
**nothing** {

`srfi-189`} [SRFI-189] Returns a Nothing.

- Function:
**right***obj …* - Function:
**left***obj …* {

`srfi-189`} [SRFI-189] Returns a Right or a Left respectively, with`obj`… as its payload.

- Function:
**list->just***objs* - Function:
**list->right***objs* - Function:
**list->left***objs* {

`srfi-189`} [SRFI-189] Returns a Just, Right or Left respectively, with`objs`as its payload.

- Function:
**maybe->either***maybe obj …* {

`srfi-189`} [SRFI-189] The`maybe`argument must be a Maybe. If it is a Just, then a Right with the same payload is returned. If it is a Nothing, a Left with`obj`… as its payload.

- Function:
**either->maybe***either* {

`srfi-189`} [SRFI-189] The`either`argument must be an Either. If it is a Right, then a Just with the same payload is returned. If it is a Left, a Nothing is returned.

- Function:
**either-swap***either* {

`srfi-189`} [SRFI-189] The`either`argument must be an Either. If it is a Right, a Left with the same payload argument is returned. If it is a Left, a Right with the same payload argument is returned.

Next: Maybe and Either sequence operations, Previous: Maybe and Either constructors, Up: Maybe and Either optional container types [Contents][Index]

- Function:
**maybe-ref***maybe failure :optional success* {

`srfi-189`} [SRFI-189] The`maybe`arugment must be a Maybe. If it’s a Nothing, a procedure`failure`is tail-called with no arguments. If it’s Just, a procedure`success`is tail-called with its payload object(s). If`success`is omitted,`values`

is used.(maybe-ref (just 1 2) (^[] (error 'huh?)) +) ⇒ 3

- Function:
**either-ref***either failure :optional success* {

`srfi-189`} [SRFI-189] The`either`argument must be an Either. If it’s a Left, a procedure`failure`is called with its payload object(s). If it’s a Right, a procedure`success`is called with its payload object(s). If`success`is omitted,`values`

is used.

- Function:
**maybe-ref/default***maybe default …* {

`srfi-189`} [SRFI-189] The`maybe`argument must be a Maybe. If it’s a Nothing,`default`… are returned as multiple values. If it’s a Just, its payload object(s) is/are returned as multiple values.

- Function:
**either-ref/default***maybe default …* {

`srfi-189`} [SRFI-189] The`either`argument must be an Either. If it’s a Left,`default`… are returned as multiple values (the Left’s payload is discarded). If it’s a Right, its payload object(s) is/are returned as multiple values.

- Function:
**maybe-join***maybe* {

`srfi-189`} [SRFI-189] If`maybe`is a Nothing, it is returned. If it’s a Maybe and its only payload is a Maybe, the inner Maybe is returned. Other cases raise an error.

- Function:
**either-join***either* {

`srfi-189`} [SRFI-189] If`either`is a Left, it is returned. If it is a Right and its only payload is an Either, the inner Either is returned. Other cases raise an error.

- Function:
**maybe-bind***maybe mproc mproc2 …* {

`srfi-189`} [SRFI-189] Monadic`bind`operation. The`maybe`argument must be a Maybe. If it is a Nothing, it is returned. If it is a Just, its payload object(s) is/are applied to a procedure`mproc`, which must return a Maybe. If`mproc2`… are given, the same operation is repeated on them.(maybe-bind m p p2) ≡ (maybe-bind (maybe-bind m p) p2)

- Function:
**either-bind***either mproc mproc2 …* {

`srfi-189`} [SRFI-189] Monadic`bind`operation. The`either`argument must be an Either. If it is a Left, it is returned as is. If it is a Right, its payload object(s) is/are applied to a procedure`mproc`, which must return an Either. If`mproc2`… are given, the same operation is repeated on them.(either-bind e p p2) ≡ (either-bind (either-bind e p) p2)

- Function:
**maybe-compose***mproc mproc2 …* - Function:
**either-compose***mproc mproc2 …* {

`srfi-189`} [SRFI-189] Each argument must be a procedure taking zero or more arguments and return a Maybe/an Either. Returns a procedure that accepts zero or more arguments and returns a Maybe/an Either.When the returned procedure is called, it first calls

`mproc`; if it returns a Nothing/Left, or there’s no more mprocs, the result is returned. If the result is a Just, its payload is applied to the next mproc, and so on.(maybe-bind m p p2 ...) ⇒ (maybe-ref m (^[] (nothing)) (maybe-compose p p2 ...))

Next: Maybe and Either protocol converters, Previous: Maybe and Either accessors, Up: Maybe and Either optional container types [Contents][Index]

A Maybe and an Either can be a container of zero or one element, and we have several procedures that employ this view. (Note: For this purpose, we treat multiple payload values as a whole, since they are processed in one step—as if we don’t regard multiple procedure arguments and multiple return values as a sequence of individual values.)

- Function:
**maybe-length***maybe* - Function:
**either-length***either* {

`srfi-189`} [SRFI-189] Returns 0 if`maybe`/`either`is a Nothing/Left, and 1 if it is a Just/Right. An error is thrown if the argument isn’t a Maybe/an Either.

- Function:
**maybe-filter***pred maybe* - Function:
**either-filter***pred either obj …* {

`srfi-189`} [SRFI-189] If`maybe`/`either`is a Nothing/Left, returns a Nothing/a Left of`obj`…. If`maybe`/`either`is a Just/a Right, apply`pred`on its payload value(s). If`pred`returns a true value,`maybe`/`either`is returned; otherwise, a Nothing/a Left of`obj`… is returned.An error is thrown if

`maybe`/`either`isn’t a Maybe/an Either.

- Function:
**maybe-remove***pred maybe* - Function:
**either-remove***pred either obj …* {

`srfi-189`} [SRFI-189] Like`maybe-filter`

/`either-filter`

, but the meaning of`pred`is reversed.

- Function:
**maybe-sequence***mappable cmap :optional aggregator* - Function:
**either-sequence***mappable cmap :optional aggregator* {

`srfi-189`} [SRFI-189] This converts a collection of Maybes/Eithers to a Mayber/an Either of a collection. The input collection and the collection in the output can be of different type.It’s easier to explain using Haskell-ish type signatures, although it’s not precisely specified. Suppose

`Container x`

is some kind of a collection of type`x`, and`a*`

is multiple values of arbitrary types.Mappable = Container Maybe a* CMap = ((Maybe a* -> b) -> Container Maybe a* -> Container' b Aggregator = a* -> b maybe-sequence :: Mappable -> CMap -> Aggregator -> Container' b

The

`cmap`maps`proc`over the input container (`mappable`), and gathers the result into another container. It can be any containers, as long as it matches the`mappable`argument. For example,`mappable`may be a vector of Maybes, and`cmap`can be`vector-map`

–in that case, both`Container`

and`Container'`

are`Vector`

. Or,`mappable`may be a list of Maybes, and`cmap`can be`(cut map-to <string> <> <>)`

, then`Container`

is a list and`Container'`

is a string.The types

`a*`

and`b`

is determined by the`aggregator`procedure, whose default value is`list`

.

- Function:
**maybe-map***proc maybe* - Function:
**either-map***proc either* {

`srfi-189`} [SRFI-189] If`maybe`/`either`is a Nothing/Left, it is returned as is. If it is a Just/Right, its payload value(s) is/are passed to`proc`, and the result is returned.

- Function:
**maybe-for-each***proc maybe* - Function:
**either-for-each***proc either* {

`srfi-189`} [SRFI-189] If`maybe`/`either`is a Nothing/Left, these procedures do nothing. Otherwise,`proc`is applied to the argument’s payload values. The result of`proc`is discarded. Returns an unspecified vlaue.

- Function:
**maybe-fold***kons knil maybe* - Function:
**either-fold***kons knil either* {

`srfi-189`} [SRFI-189] If`maybe`/`either`is a Nothing/Left,`knil`is returned. Otherwise,`kons`is called with the argument’s payload values, plus`knil`. What`kons`returns becomes the result.

- Function:
**maybe-unfold***p f g seed …* - Function:
**either-unfold***p f g seed …* {

`srfi-189`} [SRFI-189] First, the stop predicate`p`is applied to`seed`…. If it returns a true value, a Nothing / a Left of`seed`… is returned. Otherwise,`g`is applied to`seed`…, which should return the same number of values as seeds, and passed to`p`. If`p`returns false, it is an error. If`p`returns true,`mapper`is applied to`seed`…, then the results are wrapped in a Just/Right to be returned.

Next: Maybe and Either syntactic utilities, Previous: Maybe and Either sequence operations, Up: Maybe and Either optional container types [Contents][Index]

- Function:
**maybe->list***maybe* {

`srfi-189`} [SRFI-189] If`maybe`is a Just, returns a list of its payload values. If it is a Nothing, an empty list is retured.

- Function:
**list->maybe***lis* {

`srfi-189`} [SRFI-189] If`lis`is an empty list, a Nothing is returned. Otherwise, a Just that has elements in`lis`as payload values is returned.Note that

`(list->maybe (maybe->list x))`

isn’t an identity mapping—if`x`is a Just with zero payload values, you’ll get a Nothing.

- Function:
**either->list***either* {

`srfi-189`} [SRFI-189] If`either`is a Right, returns a list of its payload values. If it is a Left, returns an empty string.

- Function:
**list->either***lis obj …* {

`srfi-189`} [SRFI-189] If`lis`is an empty list, a Left of`obj`… is returned. Otherwise, a Right that has elements in`lis`as payload values is returned.

- Function:
**maybe->truth***maybe* {

`srfi-189`} [SRFI-189] If`maybe`is a Nothing,`#f`

is returned. Otherwise, it must be a Just with one value, and its value is returned.If

`maybe`is a Just and it doesn’t have exactly one value, an error is thrown. If you want to deal with arbitrary number of payload values, use`maybe->list-truth`

.

- Function:
**truth->maybe***obj* {

`srfi-189`} [SRFI-189] If`obj`is`#f`

, returns a Nothing. Otherwise, returns a Just with`obj`as its payload.Note that

`(truth->maybe (maybe->truth x))`

isn’t an identity mapping— if`x`is a Just wrapping`#f`

, you’ll get a Nothing.

- Function:
**either->truth***either* {

`srfi-189`} [SRFI-189] If`either`is a Left,`#f`

is returned. Otherwise, it must be a Right with one value, and its value is returned.If

`either`is a Right and it doesn’t have exactly one value, an error is thrown. If you want to deal with arbitrary number of payload values, use`either->list-truth`

.

- Function:
**truth->either***obj fail-obj …* {

`srfi-189`} [SRFI-189] If`obj`is`#f`

, returns a Left with`fail-obj`… is returned. Otherwise, returns a Right with`obj`as its payload.

- Function:
**maybe->list-truth***maybe* {

`srfi-189`} [SRFI-189] Like`maybe->list`

, it returns`#f`

if`maybe`is a Nothing. If`maybe`is a Just, however, it returns a list of its payload values.

- Function:
**list-truth->maybe***lis-or-false* {

`srfi-189`} [SRFI-189] The argument must be`#f`

of a list. If it is`#f`

, a Nothing is returned. If it is a list, a Just with elements of the list is returned.`(list-truth->maybe (maybe->list-truth x))`

is an identity mapping.

- Function:
**either->list-truth***either* {

`srfi-189`} [SRFI-189] Like`either->list`

, it returns`#f`

if`either`is a Left. If`either`is a Right, however, it returns a list of its payload values.

- Function:
**list-truth->either***lis-or-false fail-objs …* {

`srfi-189`} [SRFI-189] The`list-or-false`argument must be`#f`

of a list. If it is`#f`

, a Left with`fail-objs`… is returned. If it is a list, a Right with elements of the list is returned.

- Function:
**maybe->generation***maybe* {

`srfi-189`} [SRFI-189] If`maybe`is a Nothing, an EOF object is returned. Otherwise, it must be a Just with one value, and its value is returned. If`maybe`is a Just and it doesn’t have exactly one value, an error is thrown.

- Function:
**generation->maybe***obj* {

`srfi-189`} [SRFI-189] If`obj`is an EOF value, a Nothing is returned. Otherwise, a Just wrapping`obj`is returned.

- Function:
**either->generation***either* {

`srfi-189`} [SRFI-189] If`either`is a Left, an EOF object is returned. Otherwise, it must be a Right with one value, and its value is returned. If`either`is a Right and it doesn’t have exactly one value, an error is thrown.

- Function:
**generation->either***obj fail-objs …* {

`srfi-189`} [SRFI-189] If`obj`is an EOF value, a Left with`fail-objs`… is returned. Otherwise, a Right wrapping`obj`is returned.

- Function:
**maybe->values***maybe* {

`srfi-189`} [SRFI-189] If`maybe`is a Just, returns its payload as multiple values. If it is a Nothing, returns no values. (Note that a Just with zero values also returns no values.)

- Function:
**values->maybe***producer* {

`srfi-189`} [SRFI-189] It first invokes a procedure`producer`with no values. If it returns zero values, a Nothing is returned; otherwise, a Just with those values are returned.

- Function:
**either->values***either* {

`srfi-189`} [SRFI-189] If`either`is a Right, returns its payload as multiple values. If it is a Left, returns no values. (Note that a Right with zero values also returns no values.)

- Function:
**values->either***producer fail-obj …* {

`srfi-189`} [SRFI-189] It first invokes a procedure`producer`with no values. If it returns zero values, a Left with`fail-obj`… as its payload is returned. If it returns one or more values, a Right with those values are returned.

- Function:
**maybe->two-values***maybe* {

`srfi-189`} [SRFI-189] If`maybe`is a Just with exactly one value, the value and`#t`

is returned. If`maybe`is a Nothing, two`#f`

is returned. An error is thrown if`maybe`has a Just with zero or more than two values.

- Function:
**two-values->maybe***producer* {

`srfi-189`} [SRFI-189] The inverse of`maybe->two-values`

. A procedure`producer`is called with no arguments. It must return two values, a possible payload value, and a boolean. If the second value is true, a Just with the first value is returned. If the second value is`#f`

, a Nothing is returned (the first return value is ignored).

- Function:
**exception->either***pred thunk* {

`srfi-189`} [SRFI-189] A procedure`thunk`is called without argument, wrapped by an exception handler. If`thunk`raises a condition, it is examined by`pred`. If`pred`returns true on the condition, the exception is wrapped by a Left and returned. If`pred`returns`#f`

, the exception is reraised. If no exception is raised, the result(s) of`thunk`is wrapped by a Right and returned.

Next: Trivalent logic, Previous: Maybe and Either protocol converters, Up: Maybe and Either optional container types [Contents][Index]

- Macro:
**maybe-if***mtest then else* {

`srfi-189`} [SRFI-189] If the`mtest`expression yields a Just, evaluates`then`. If the`mtest`expression yeilds a Nothing, evaluates`else`. If the`mtest`expression doesn’t produce a Maybe, an error is thrown.

- Macro:
**maybe-and***maybe …* - Macro:
**either-and***either …* {

`srfi-189`} [SRFI-189] Evaluates`maybe`/`either`from left to right, as far as each yields a Just/Right. If every expression yields a Just/Right, the last one is returned. If it encounters an expression that yields a Nothing/Left, it stops evaluating the rest of expressions and returns the Nothing/Left.If expressions yield something other than Maybe/Either, an error is thrown.

- Macro:
**maybe-or***maybe …* - Macro:
**either-or***either …* {

`srfi-189`} [SRFI-189] Evaluates`maybe`/`either`from left to right, as far as each yields a Nothing/Left. If it encounters an expression that yields a Just/Right, it stops evaluating the rest of expressions and returns it.If expressions yield something other than Maybe/Either, an error is thrown.

- Macro:
**maybe-let****( claw … ) body …* - Macro:
**either-let****( claw … ) body …* {

`srfi-189`} [SRFI-189] This is a Maybe/Either version of`and-let*`

.

- Macro:
**maybe-let*-values***( mv-claw … ) body …* - Macro:
**either-let*-values***( mv-claw … ) body …* {

`srfi-189`} [SRFI-189]

- Macro:
**either-guard***(pred) body …* {

`srfi-189`} [SRFI-189]

Previous: Maybe and Either syntactic utilities, Up: Maybe and Either optional container types [Contents][Index]

- Function:
**tri-not***maybe* {

`srfi-189`} [SRFI-189]

- Function:
**try=?***maybe …* {

`srfi-189`} [SRFI-189]

- Function:
**try-and***maybe …* {

`srfi-189`} [SRFI-189]

- Function:
**try-or***maybe …* {

`srfi-189`} [SRFI-189]

- Function:
**try-merge***maybe …* {

`srfi-189`} [SRFI-189]