Opt
Opt
9.0
top
← prev up next →

OptπŸ”— i

Raymond Racine <ray.racine@gmail.com>

1OptionπŸ”— i

Utility functions useful for Typed Rackets Option type.

procedure

( opt-carlst)(Optiona)

lst:(Listofa)
(:opt-car(All(A)((ListofA)->(OptionA))))

Total head function for a hetergeneous list. If the list is non-empty returns the first element else #f.

procedure

( opt-undefined?x)Boolean

x:(Optiona)
(:opt-defined?(All(a)(Optiona)->Boolean))

Is the option value defined? Of little to no utility, but provided for completeness.

procedure

( opt-exists?xpref)Boolean

x:(Optiona)
pref:(a->Boolean)
(:opt-exists(All(a)(Optiona)(a->Boolean)->Boolean))

Does the option value exist and does the value match the provided predicate?

procedure

( opt-get-orelsexdef-expr)a

x:(Optiona)
def-expr:(Thunka)
{ (:opt-get-orelse(All(a)(Optiona)(->a)->a))

Returns the option’s value if defined else the value returned by the provided thunk. }

procedure

( opt-get-orelse-valuexdef-value)a

x:(Optiona)
def-value:a
(:opt-get-orelse-value(All(a)(Optiona)a->a))

Returns the options’ value if defined else the provided default value.

procedure

( opt-orelsexdef-expr)(Optiona)

x:(Optiona)
def-expr:(->(Optiona))
{ (:opt-orelse(All(a)(Optiona)(->(Optiona))->(Optiona)))

Returns the provided option itself if its value is defined otherwise the given alternative option. }

procedure

( opt-mapxfn)(Optionb)

x:(Optiona)
fn:(->ab)
(: opt-map (All (a b) (Option a) (a -> b) -> (Option b)))

Apply the given procedure to the option value if defined.

procedure

( opt-map-orelse-valuexfndefault)b

x:(Optiona)
fn:(->ab)
default:b
(:opt-map-orelse-value(All(ab)(Optiona)(a->b)b->b))

Apply the given procedure to the option’s value if defined and return it, otherwise return the provided default value.

procedure

( opt-map-orelsexfndefault-expr)b

x:(Optiona)
fn:(->ab)
default-expr:(Thunkb)
(:opt-map-orelse(All(ab)(Optiona)(a->b)(->b)->b))

Apply the given procedure to the option’s value if defined and return it, otherwise return the value returned from the provided thunk when evaluated.

procedure

( opt-flatmapxfmap)(Optionb)

x:(Optiona)
fmap:(->a(Optionb))
(:opt-flatmap(All(ab)(Optiona)(a->(Optionb))->(Optionb)))

Use the provided function to translate from an option value of one type to an option value of another type.

procedure

( opt-filterxfn)(Optiona)

x:(Optiona)
fn:(->aBoolean)
(:opt-filter(All(a)(Optiona)(a->Boolean)->(Optiona)))

If the option is defined and the predicate is satisfied return the option’s value else #f.

procedure

( opt-rejectxfn)(Optiona)

x:(Optiona)
fn:(->aBoolean)
(:opt-reject(All(a)(Optiona)(a->Boolean)->(Optiona)))

Drop’s the options value (i.e. returns #f) if the value does not match the provided predicate.

procedure

( opt-foreachxproc)Void

x:(Optiona)
proc:(->aVoid)
(:opt-foreach(All(T)(OptionT)(T->Void)->Void))

Apply a side-effecting procedure to the option’s value if defined, otherwise does nothing. Returns a void value.

2EitherπŸ”— i

struct

(struct Left(val)
#:extra-constructor-namemake-Left)
val:E
(struct:(E)Left([val:E])#:transparent)

The left possible value of an Either type. By convention is often the error or failure value or message.

struct

(struct Right(val)
#:extra-constructor-namemake-Right)
val:D
(struct:(D)Right([val:D])#:transparent)

The right possible value of an Either type. By convention is often the correct or success value.

Type

Either:(U(LeftE)(RightD))

(define-type(EitherED)(U(LeftE)(RightD)))

A type whose value is of one type or another. By convention often the Left type value is a failure value value and the Right type value is a success value.

procedure

( leftx)E

x:(EitherED)
(:left(All(ED)(EitherED)->E))

Project (return) the Either’s value IF it is a Left value, otherwise and error is thrown.

procedure

( rightx)D

x:(EitherED)
(:right(All(ED)(EitherED)->D))

Project (return the Either’s value IF it is a Right value, otherwise an error is thrown.

procedure

( eitherleft-fnright-fnan-either)T

left-fn:(->ET)
right-fn:(->DT)
an-either:(EitherED)
(:either(All(EDT)((E->T)(D->T)(EitherED)->T)))

Applies one of the provided procedures to the value of the provided Either argument. Which procedure is applied depends on the type of the provided Either’s value.

top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /