either_or

Types

A generic choice type.

pub type EitherOr(a, b) {
  Either(a)
  Or(b)
}

Constructors

  • Either(a)
  • Or(b)

Values

pub const discriminate: fn(List(a), fn(a) -> Bool) -> List(
  EitherOr(a, a),
)

Alias for map_from_condition.

pub fn flat_map_either(
  v: EitherOr(a, b),
  f: fn(a) -> EitherOr(c, b),
) -> EitherOr(c, b)
pub fn flat_map_or(
  v: EitherOr(a, b),
  with f: fn(b) -> EitherOr(a, c),
) -> EitherOr(a, c)

Compose map_or and flatten_or.

pub fn flatten(
  t: EitherOr(EitherOr(a, b), EitherOr(a, b)),
) -> EitherOr(a, b)

Flatten a EitherOr(EitherOr(a, b), EitherOr(a, b)) value.

Isomorphic to the special case of unwrap in which the type a has the form EitherOr(c, d) for some types c and d.

pub fn flatten_either(
  t: EitherOr(EitherOr(a, b), b),
) -> EitherOr(a, b)

Flatten an EitherOr(EitherOr(a, b), b)-value.

pub fn flatten_or(
  t: EitherOr(a, EitherOr(a, b)),
) -> EitherOr(a, b)

Flatten an EitherOr(a, EitherOr(a, b))-value.

pub fn from_bool(z: z, b: Bool) -> EitherOr(z, z)

Given a value z of arbitrary type and a bool b returns Either(z) if b == True else returns Or(z).

pub fn from_condition(
  a: a,
  condition condition: fn(a) -> Bool,
) -> EitherOr(a, a)

Lazy form of from_bool.

pub fn from_result(z: Result(a, b)) -> EitherOr(a, b)

Converts a Result(a, b) into an EitherOr(a, b).

pub fn get_either(v: EitherOr(a, b)) -> option.Option(a)

Given a value of type EitherOr(a, b) returns Some(a) if the value has the form Either(a) else returns None.

pub fn get_or(v: EitherOr(a, b)) -> option.Option(b)

Given a value of type EitherOr(a, b) returns Some(x) if the value has the form Or(x) else returns None.

pub fn group_eithers(
  ze_list: List(EitherOr(a, b)),
) -> List(EitherOr(List(a), b))

Aggregates the payloads of consecutive Either instances from a List(EitherOr(a, b)) into single Either(List(a)) elements, such as to turn a List(EitherOr(a, b)) into a List(EitherOr(List(a), b)). Discards elements of the form Either([]) from the final list.

pub fn group_eithers_including_empty_lists(
  ze_list: List(EitherOr(a, b)),
) -> List(EitherOr(List(a), b))

Aggregates the payloads of consecutive Either instances from a List(EitherOr(a, b)) into a single Either(List(a)) elements, such as to turn a List(EitherOr(a, b)) into a List(EitherOr(List(a), b)). Introduces an element of the form Either([]: List(a)) between each consecutive pair of Or(b) elements symoblizing an empty of Either payloads sitting between pair of consecutive Or(b) elements.

pub fn group_ors(
  ze_list: List(EitherOr(a, b)),
) -> List(EitherOr(a, List(b)))

Aggregates the payloads of consecutive Either instances from a List(EitherOr(a, b)) into single Either(List(a)) elements such as to turn a List(EitherOr(a, b)) into a List(EitherOr(List(a), b)). Discards elements of the form Or([]) from the final list.

pub fn group_ors_including_empty_lists(
  ze_list: List(EitherOr(a, b)),
) -> List(EitherOr(a, List(b)))

Aggregates the payloads of consecutive Or instances from a List(EitherOr(a, b)) into single Or(List(b)) elements, such as to turn a List(EitherOr(a, b)) into a List(EitherOr(a, List(b))). Introduces an element of the form Or([]: List(b)) between each consecutive pair of Either(a) elements, symoblizing an empty of Or payloads sitting between pair of consecutive Either(a) elements.

pub fn is_either(v: EitherOr(a, b)) -> Bool

Given a value of type EitherOr(a, b) returns True if and only if the value is an Either variant.

pub fn is_or(v: EitherOr(a, b)) -> Bool

Given a value of type EitherOr(a, b) value returns True if and only if the value is an Or variant.

pub fn map_either(
  v: EitherOr(a, b),
  f: fn(a) -> c,
) -> EitherOr(c, b)

Given a value of type EitherOr(a, b) and a function f: a -> c returns Either(f(a)) if the value has the form Either(a) and returns Or(b) if the value has the form Or(b).

pub fn map_eithers(
  v: List(EitherOr(a, b)),
  f: fn(a) -> c,
) -> List(EitherOr(c, b))

Applies map_either to each element of a list of EitherOr(a, b) elements.

pub fn map_eo(
  v: EitherOr(a, b),
  on_either: fn(a) -> a_prime,
  on_or: fn(b) -> b_prime,
) -> EitherOr(a_prime, b_prime)

Apply separate maps to each payload of an EitherOr value.

Symmetric to map_oe.

Both functions are offered to in order to allow the happy path to be pursued with either Either or Or variants via the use <- syntax.

pub fn map_from_condition(
  list: List(z),
  condition condition: fn(z) -> Bool,
) -> List(EitherOr(z, z))

Given a List(z) and a function f: z -> Bool returns a List(EitherOr(z, z)) by mapping over the list with from_condition(_, f).

pub fn map_oe(
  v: EitherOr(a, b),
  on_or: fn(b) -> b_prime,
  on_either: fn(a) -> a_prime,
) -> EitherOr(a_prime, b_prime)

Apply separate maps to each payload of an EitherOr value.

Symmetric to map_eo.

Both functions are offered to in order to allow the happy path to be pursued with either Either or Or variants via the use <- syntax.

pub fn map_or(
  v: EitherOr(a, b),
  with f: fn(b) -> c,
) -> EitherOr(a, c)

Given a value of type EitherOr(a, b) and a function f: b -> c returns Or(f(b)) if the value has the form Or(b) and returns Either(a) if the value has the form Either(a).

pub fn map_ors(
  v: List(EitherOr(a, b)),
  f: fn(b) -> c,
) -> List(EitherOr(a, c))

Applies map_or to each element of a list of EitherOr(a, b) elements.

pub fn map_resolve(
  v: List(EitherOr(a, b)),
  on_either f: fn(a) -> c,
  on_or g: fn(b) -> c,
) -> List(c)

Applies resolve_eo to each element of a list of EitherOr(a, b) elements.

pub fn remove_eithers_unwrap_ors(
  ze_list: List(EitherOr(a, b)),
) -> List(b)

Given a List(EitherOr(a, b)) removes all elements of the form Either(a) and unwraps the remaining elements.

pub fn remove_ors_unwrap_eithers(
  ze_list: List(EitherOr(a, b)),
) -> List(a)

Given a List(EitherOr(a, b)) removes all elements of the form Or(b) and unwraps the remaining elements.

pub fn resolve_eo(
  t: EitherOr(a, b),
  on_either f1: fn(a) -> c,
  on_or f2: fn(b) -> c,
) -> c

Given a value of type EitherOr(a, b) and functions f1: a -> c, f2: b -> c, returns f1(a) if the value has the form Either(a) and returns f2(b) if the value has the form Or(b).

Symmetric to resolve_oe.

Both functions are offered to in order to allow the happy path to be pursued with either Either or Or variants via the use <- syntax.

pub fn resolve_oe(
  t: EitherOr(a, b),
  on_or f1: fn(b) -> c,
  on_either f2: fn(a) -> c,
) -> c

Given a value of typeEitherOr(a, b) and functions f1: b -> c, f2: a -> c, returns f1(a) if the value has the form Either(a) and returns f2(b) if the value has the form Or(b).

Symmetric to resolve_eo.

Both functions are offered to in order to allow the happy path to be pursued with either Either or Or variants via the use <- syntax.

pub fn swap(v: EitherOr(a, b)) -> EitherOr(b, a)

Given a value of type EitherOr(a, b) returns a value of type EitherOr(b, a) by swapping the payloads.

pub fn to_result(z: EitherOr(a, b)) -> Result(a, b)

Converts an EitherOr(a, b) into a Result(a, b).

pub fn unwrap(v: EitherOr(a, a)) -> a

Given a value of type EitherOr(a, a) returns the payload of type a regardless of the variant.

pub fn unwrap_either(v: EitherOr(a, b), default: a) -> a

Given a value of type EitherOr(a, b) and default of type a returns x if the value has the form Either(x) else returns the default.

pub fn unwrap_or(v: EitherOr(a, b), default: b) -> b

Given a value of type EitherOr(a, b) and default of type b returns x if the value has the form Or(x) else returns the default.

Search Document