Opt
1 Option
opt-car
opt-undefined?
opt-exists?
opt-get-orelse
opt-get-orelse-value
opt-orelse
opt-map
opt-map-orelse-value
opt-map-orelse
opt-flatmap
opt-filter
opt-reject
opt-foreach
2 Either
Left
Right
Either
left
right
either
6.12

Opt

Raymond Racine <[email protected]>

1 Option

 (require option)

Utility functions useful for Typed Rackets Option type.

procedure

(opt-car lst)  (Option a)

  lst : (Listof a)
(: opt-car (All (A) ((Listof A) -> (Option A))))

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 : (Option a)
(: opt-defined? (All (a) (Option a) -> Boolean))

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

procedure

(opt-exists? x pref)  Boolean

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

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

procedure

(opt-get-orelse x def-expr)  a

  x : (Option a)
  def-expr : (Thunk a)
{ (: opt-get-orelse (All (a) (Option a) (-> a) -> a))

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

procedure

(opt-get-orelse-value x def-value)  a

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

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

procedure

(opt-orelse x def-expr)  (Option a)

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

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

procedure

(opt-map x fn)  (Option b)

  x : (Option a)
  fn : (-> a b)
(: 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-value x fn default)  b

  x : (Option a)
  fn : (-> a b)
  default : b
(: opt-map-orelse-value (All (a b) (Option a) (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-orelse x fn default-expr)  b

  x : (Option a)
  fn : (-> a b)
  default-expr : (Thunk b)
(: opt-map-orelse (All (a b) (Option a) (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-flatmap x fmap)  (Option b)

  x : (Option a)
  fmap : (-> a (Option b))
(: opt-flatmap (All (a b) (Option a) (a -> (Option b)) -> (Option b)))

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

procedure

(opt-filter x fn)  (Option a)

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

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

procedure

(opt-reject x fn)  (Option a)

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

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

procedure

(opt-foreach x proc)  Void

  x : (Option a)
  proc : (-> a Void)
(: opt-foreach (All (T) (Option T) (T -> Void) -> Void))

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

2 Either

 (require either)

struct

(struct Left (val)
    #:extra-constructor-name make-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-name make-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 (Left E) (Right D))

(define-type (Either E D) (U (Left E) (Right D)))

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

(left x)  E

  x : (Either E D)
(: left (All (E D) (Either E D) -> E))

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

procedure

(right x)  D

  x : (Either E D)
(: right (All (E D) (Either E D) -> D))

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

procedure

(either left-fn right-fn an-either)  T

  left-fn : (-> E T)
  right-fn : (-> D T)
  an-either : (Either E D)
(: either (All (E D T) ((E -> T) (D -> T) (Either E D) -> 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.