qol_gleam/qol_list

Values

pub fn at(in list: List(a), get index: Int) -> Result(a, Nil)

Returns the element in the Nth position in the list, with 0 being the first position.

Error(Nil) is returned if the list is not long enough for the given index or if the index is less than 0.

⚠️ WARNING ⚠️: It’s best to avoid using this function as it would be commonly misused, resulting in poor code. Try to redesign your code and data structure first, and only use this as a last resource (or if you know what you are doing).

Examples

assert at([1, 2, 3], 1) == Ok(2)
assert at([1, 2, 3], 5) == Error(Nil)

Origin

This function was from the stdlib and was deprecated since v0.37.0 as it was misleading and would be commonly misused, resulting in poor code.

pub fn pop(
  in haystack: List(a),
  one_that is_desired: fn(a) -> Bool,
) -> Result(#(a, List(a)), Nil)

Removes the first element in a given list for which the predicate function returns True.

Returns Error(Nil) if no such element is found.

Examples

assert pop([1, 2, 3], fn(x) { x > 2 }) == Ok(#(3, [1, 2]))
assert pop([1, 2, 3], fn(x) { x > 4 }) == Error(Nil)
assert pop([], fn(_) { True }) == Error(Nil)

Origin

This function was from the stdlib and was deprecated since v0.54.0 as due to confusion with the name.

pub fn pop_map(
  in haystack: List(a),
  one_that is_desired: fn(a) -> Result(b, c),
) -> Result(#(b, List(a)), Nil)

Removes the first element in a given list for which the given function returns Ok(new_value), then returns the wrapped new_value as well as list with the value removed.

Returns Error(Nil) if no such element is found.

Examples

assert pop_map([[], [2], [3]], first) == Ok(#(2, [[], [3]]))
assert pop_map([[], []], first) == Error(Nil)
assert pop_map([], first) == Error(Nil)

Origin

This function was from the stdlib and was deprecated since v0.54.0 as due to confusion with the name.

pub fn rotate(list: List(a), by n: Int) -> List(a)

Returns a new list with elements rotated from one end to the other.

Examples

assert rotate([1, 2, 3, 4, 5], by: 2) == [3, 4, 5, 1, 2]
assert rotate([1, 2, 3, 4, 5], by: -2) == [4, 5, 1, 2, 3]

Origin

This function is commonly expected from other programming language.

pub fn tally(list: List(a)) -> dict.Dict(a, Int)

Tally the elements from the given list.

Examples

assert ["a", "b", "c", "b", "c", "a", "c", "b"] |> tally() |> dict.to_list()
  == [#("a", 2), #("b", 3), #("c", 3)]

Origin

This function is commonly expected from other programming language.

Search Document