gleither

Types

monad representing a Left or Right

pub type Either(left, right) {
  Left(val: left)
  Right(val: right)
}

Constructors

  • Left(val: left)

    left

  • Right(val: right)

    right

Values

pub fn flat_map(
  either: Either(left, right),
  func: fn(right) -> Either(left, right),
) -> Either(left, right)

alias for flat_map_right

: let assert gleither.Right(2) =
: gleither.Right(1)
: |> gleither.flat_map(fn(x) { gleither.Right(x + 1) })
pub fn flat_map_left(
  either: Either(left, right),
  func: fn(left) -> Either(left, right),
) -> Either(left, right)

map and flatten a Left

: let assert gleither.Left(2) =
: gleither.Left(1)
: |> gleither.flat_map_left(fn(x) { gleither.Left(x + 1) })
pub fn flat_map_right(
  either: Either(left, right),
  func: fn(right) -> Either(left, right),
) -> Either(left, right)

map and flatten a Right

: let assert gleither.Right(2) =
: gleither.Right(1)
: |> gleither.flat_map_right(fn(x) { gleither.Right(x + 1) })
pub fn flatten(
  either: Either(left, Either(left, right)),
) -> Either(left, right)

alias for flatten_right

: let assert gleither.Right(1) = gleither.flatten_right(gleither.Right(gleither.Right(1))) 
pub fn flatten_left(
  either: Either(Either(left, right), right),
) -> Either(left, right)

flatten a nested Left

: let assert gleither.Left(1) = gleither.flatten_left(gleither.Left(gleither.Left(1))) 
pub fn flatten_right(
  either: Either(left, Either(left, right)),
) -> Either(left, right)

flatten a nested Right

: let assert gleither.Right(1) = gleither.flatten_right(gleither.Right(gleither.Right(1))) 
pub fn from_bool(val: a, bool: Bool) -> Either(a, a)

constructs an Either from a value and a Bool by wrapping the value with Left if the Bool is False, Right if the Bool is True

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

constructs an Either from a value val : a and a condition fn(a) -> Bool, wrapping the value with Left or Right according to whether the condition evaluates to False or True, respectively, at the value

pub fn from_result(
  result: Result(left, right),
) -> Either(left, right)

convert a Result to an Either, mapping Ok to Left and Error to Right

: let assert gleither.Left(1) = gleither.from_result(Ok(1))  
: let assert gleither.Right(1) = gleither.from_result(Error(1))  
pub fn full_flat_map(
  either: Either(left, right),
  left_func: fn(left) -> Either(left, right),
  right_func: fn(right) -> Either(left, right),
) -> Either(left, right)

flat_map either potential value

: let assert gleither.Left(2) =
: gleither.Left(1)
: |> gleither.full_flat_map(fn(x) { gleither.Left(x + 1) }, fn(x) { gleither.Right(x * 3) })
: let assert gleither.Right(3) =
: gleither.Right(1)
: |> gleither.full_flat_map(fn(x) { gleither.Left(x + 1) }, fn(x) { gleither.Right(x * 3) })
pub fn full_map(
  either: Either(left, right),
  left_func: fn(left) -> new_left,
  right_func: fn(right) -> new_right,
) -> Either(new_left, new_right)

map either potential value

: let assert gleither.Left(2) = gleither.full_map(gleither.Left(1), int.add(_, 1), int.add(_, 1)) 
: let assert gleither.Right(2) = gleither.full_map(gleither.Right(1), int.add(_, 1), int.add(_, 1)) 
pub fn get(either: Either(left, right)) -> option.Option(right)

alias for get_right get the value of a Right or None

: let assert option.Some(1) = gleither.get(gleither.Right(1))
: let assert option.None = gleither.get(gleither.Left(1))
pub fn get_left(
  either: Either(left, right),
) -> option.Option(left)

get the value of a Left or None

: let assert option.Some(1) = gleither.get_left(gleither.Left(1))
pub fn get_left_with_default(
  either: Either(left, right),
  default: left,
) -> left

get the value of a Left or default

: assert gleither.get_left_with_default(gleither.Left(1), -1) == 1
: assert gleither.get_left_with_default(gleither.Right(1), -1) == -1
pub fn get_right(
  either: Either(left, right),
) -> option.Option(right)

get the value of a Right or None

: let assert option.Some(1) = gleither.get_right(gleither.Right(1))
pub fn get_right_with_default(
  either: Either(left, right),
  default: right,
) -> right

get the value of a Right or default

: assert gleither.get_right_with_default(gleither.Right(1), -1) == 1
: assert gleither.get_right_with_default(gleither.Left(1), -1) == -1
pub fn get_with_default(
  either: Either(left, right),
  default: right,
) -> right

alias for get_right_with_default

: assert gleither.get_with_default(gleither.Right(1), -1) == gleither.get_right_with_default(gleither.Right(1), -1)
: assert gleither.get_with_default(gleither.Left(1), -1) == gleither.get_right_with_default(gleither.Left(1), -1)
pub fn group_left(
  vals: List(Either(left, right)),
) -> List(Either(List(left), right))

group consecutive Left-elements of a List(Either) into sublists, converting a List(Either(left, right)) to a List(Either(List(left), right))

Example

group_left([Left(1), Left(5), Left(4), Right(“a”), Right(“b”), Left(6), Right(“c”))

// -> [Left([1, 5, 4]), Right(“a”), Left([]), Right(“b”), Left([6]), Right(“c”), Left([])]

pub fn group_right(
  vals: List(Either(left, right)),
) -> List(Either(left, List(right)))

group consecutive Right-elements of a List(Either) into sublists, converting a List(Either(left, right)) to a List(Either(left, List(right)))

Example

group_right([Left(1), Left(5), Left(4), Right(“a”), Right(“b”), Left(6), Right(“c”))

// -> [Right([]), Left(1), Left(5), Left(4), Right([“a”, “b”]), Left(6), Right([“c”])]

pub fn is_left(either: Either(left, right)) -> Bool

returns True if the supplied value is a Left

: assert gleither.is_left(gleither.Left(1))
pub fn is_right(either: Either(left, right)) -> Bool

returns True if the supplied value is a Right

: assert gleither.is_right(gleither.Right(1))
pub fn map(
  either: Either(left, right),
  func: fn(right) -> new,
) -> Either(left, new)

alias for map_right

: let assert gleither.Left(1) = gleither.map(gleither.Left(1), int.add(_, 1)) 
: let assert gleither.Right(2) = gleither.map(gleither.Right(1), int.add(_, 1)) 
pub fn map_from_condition(
  vals: List(a),
  condition: fn(a) -> Bool,
) -> List(Either(a, a))

shorthand to list.map gleither.from_condition over a list of values, creating a List(Either(a, a)) from a List(a) via a condition: fn(a) -> Bool

pub fn map_left(
  either: Either(left, right),
  func: fn(left) -> new,
) -> Either(new, right)

apply a function to the Left or preserve the Right

: let assert gleither.Left(2) = gleither.map_left(gleither.Left(1), int.add(_, 1)) 
: let assert gleither.Right(1) = gleither.map_left(gleither.Right(1), int.add(_, 1)) 
pub fn map_resolve(
  vals: List(Either(left, right)),
  left_map: fn(left) -> new,
  right_map: fn(right) -> new,
) -> List(new)

a shorthand to list.map gleither.resolve

pub fn map_right(
  either: Either(left, right),
  func: fn(right) -> new,
) -> Either(left, new)

apply a function to the Right or preserve the Left

: let assert gleither.Left(1) = gleither.map_right(gleither.Left(1), int.add(_, 1)) 
: let assert gleither.Right(2) = gleither.map_right(gleither.Right(1), int.add(_, 1)) 
pub fn nonempty_group_left(
  vals: List(Either(left, right)),
) -> List(Either(List(left), right))

group consecutive Left-elements of a List(Either) into sublists, converting a List(Either(left, right)) to a List(Either(List(left), right)), while discarding empty lists

Example

group_left([Left(1), Left(5), Left(4), Right(“a”), Right(“b”), Left(6), Right(“c”))

// -> [Left([1, 5, 4]), Right(“a”), Right(“b”), Left([6]), Right(“c”)]

pub fn nonempty_group_right(
  vals: List(Either(left, right)),
) -> List(Either(left, List(right)))

group consecutive Right-elements of a List(Either) into sublists, converting a List(Either(left, right)) to a List(Either(left, List(right))), while discarding empty lists

Example

group_right([Left(1), Left(5), Left(4), Right(“a”), Right(“b”), Left(6), Right(“c”))

// -> [Left(1), Left(5), Left(4), Right([“a”, “b”]), Left(6), Right([“c”])]

pub fn resolve(
  val: Either(left, right),
  left_map: fn(left) -> new,
  right_map: fn(right) -> new,
) -> new

maps an Either(left, right) value to a value of a third type by applying separate Left and Right mappers that both map to the third type

pub fn swap(either: Either(left, right)) -> Either(right, left)

convert a Left to a Right and vice versa

: let assert gleither.Right(1) = gleither.swap(gleither.Left(1))  
Search Document