zip_list

Types

pub opaque type ZipList(a)

Functions

pub fn append_next(
  zip_list: ZipList(a),
  next: List(a),
) -> ZipList(a)

Append a list to the next elements of the ZipList

Examples

let numbers = new([1, 2], 3, [4, 5])
append_next(numbers, [6, 7]) == new([1, 2], 3, [4, 5, 6, 7])
pub fn append_previous(
  zip_list: ZipList(a),
  previous: List(a),
) -> ZipList(a)

Append a list to the previous elements of the ZipList

Examples

let numbers = new([1, 2], 3, [4, 5])
append_previous(numbers, [-1, 0]) == new([1, 2, -1, 0], 3, [4, 5])
pub fn backwards(zip_list: ZipList(a), times: Int) -> ZipList(a)

Move the cursor N elements backwards

Examples

let numbers = new([1, 2], 3, [4, 5])
backwards(numbers, 2) == new([], 1, [2, 3, 4, 5])
let numbers = new([1, 2], 3, [4, 5])
backwards(numbers, 5) == new([], 1, [2, 3, 4, 5])
pub fn current(zip_list: ZipList(a)) -> a

Get the current element of the ZipList

Examples

new([1, 2], 3, [4, 5])
|> current
// => 3
pub fn current_map(
  zip_list: ZipList(a),
  f: fn(a, Bool) -> a,
) -> ZipList(a)

Map a function over the elements of the ZipList, passing a flag indicating if the element is the current element

Examples

let numbers = new([1, 2], 3, [4, 5])
current_map(numbers, fn(x, is_current) {
  case is_current {
    True -> x * 10
    False -> x * 2
  }
}) == new([2, 4], 30, [8, 10])
pub fn filter(
  zip_list: ZipList(a),
  f: fn(a) -> Bool,
) -> Result(ZipList(a), Nil)

Filter the elements of the ZipList

Examples

let numbers = new([1, 2], 3, [4, 5])
filter(numbers, int.is_even) == new([2], 4, [])
let numbers = new([1, 2], 3, [4, 5])
filter(numbers, int.is_odd) == new([1], 3, [5])
let numbers = new([], 1, [])
filter(numbers, int.is_even) == Error(Nil)
pub fn first(zip_list: ZipList(a)) -> ZipList(a)

Move the cursor to the first element

Examples

let numbers = new([1, 2], 3, [4, 5])
first(numbers) == new([], 1, [2, 3, 4, 5])
let numbers = new([], 1, [2, 3, 4, 5])
first(numbers) == new([], 1, [2, 3, 4, 5])
pub fn forward(zip_list: ZipList(a), times: Int) -> ZipList(a)

Move the cursor N elements forward

Examples

let numbers = new([1, 2], 3, [4, 5])
forward(numbers, 2) == new([1, 2, 3, 4], 5, [])
let numbers = new([1, 2], 3, [4, 5])
forward(numbers, 5) == new([1, 2, 3, 4, 5], 6, [])
pub fn from_list(list: List(a)) -> Result(ZipList(a), Nil)

Create a new ZipList from a list Returns Error(Nil) if the list is empty

Examples

from_list([1, 2, 3, 4, 5]) == Ok(new([], 1, [2, 3, 4, 5]))
from_list([]) == Error(Nil)
pub fn from_list_by(
  list: List(a),
  predicate: fn(a) -> Bool,
) -> Result(ZipList(a), Nil)

Create a new ZipList from a list, setting the current element to the first element that satisfies the predicate///

Examples

from_list_by([1, 2, 3, 4, 5], fn(x) { x == 3 }) == Ok(new([1, 2], 3, [4, 5]))
from_list_by([1, 2, 3, 4, 5], fn(x) { x == 6 }) == Error(Nil)
pub fn get_all_next(zip_list: ZipList(a)) -> List(a)

Get the next elements of the ZipList

Examples

new([1, 2], 3, [4, 5])
|> get_all_next
// => [4, 5]
pub fn get_all_previous(zip_list: ZipList(a)) -> List(a)

Get the previous elements of the ZipList

Examples

new([1, 2], 3, [4, 5])
|> get_all_previous
// => [1, 2]
pub fn get_next(zip_list: ZipList(a)) -> Result(a, Nil)

Get the next element of the ZipList Returns Error(Nil) if there is no next element

Examples

new([1, 2], 3, [4, 5])
|> get_next
// => Ok(4)
new([1, 2, 3], 4, [])
|> get_next
// => Error(Nil)
pub fn get_previous(zip_list: ZipList(a)) -> Result(a, Nil)

Get the previous element of the ZipList Returns Error(Nil) if there is no previous element

Examples

new([1, 2], 3, [4, 5])
|> get_previous
// => Ok(2)
new([], 3, [4, 5])
|> get_previous
// => Error(Nil)
pub fn index(zip_list: ZipList(a)) -> Int

Get the index of the current element in the ZipList

Examples

new([1, 2], 3, [4, 5])
|> index
// => 2
new([], 3, [4, 5])
|> index
// => 0
pub fn index_map(
  zip_list: ZipList(a),
  f: fn(a, Int) -> b,
) -> ZipList(b)

Map a function over the elements of the ZipList, passing the index of each element

Examples

let numbers = new([1, 2], 3, [4, 5])
index_map(numbers, fn(x, i) { x * i }) == new([0, 2], 6, [12, 20])
pub fn is_first(zip_list: ZipList(a)) -> Bool

Check if the current element is the first element of the ZipList

Examples

new([1, 2], 3, [4, 5])
|> is_first
// => False
new([], 3, [4, 5])
|> is_first
// => True
pub fn is_last(zip_list: ZipList(a)) -> Bool

Check if the current element is the last element of the ZipList

Examples

new([1, 2], 3, [4, 5])
|> is_last
// => False
new([1, 2], 3, [])
|> is_last
// => True
pub fn jump(zip_list: ZipList(a), index: Int) -> ZipList(a)

Move the cursor to the Nth element

Examples

let numbers = new([1, 2], 3, [4, 5])
jump(numbers, 1) == new([1], 2, [3, 4, 5])
let numbers = new([1, 2], 3, [4, 5])
jump(numbers, 5) == new([1, 2, 3, 4], 5, [])
pub fn last(zip_list: ZipList(a)) -> ZipList(a)

Move the cursor to the last element

Examples

let numbers = new([1, 2], 3, [4, 5])
last(numbers) == new([1, 2, 3, 4], 5, [])
let numbers = new([1, 2, 3, 4], 5, [])
last(numbers) == new([1, 2, 3, 4], 5, [])
pub fn length(zip_list: ZipList(a)) -> Int

Get the length of the ZipList

Examples

new([1, 2], 3, [4, 5])
|> length
// => 5
pub fn map(zip_list: ZipList(a), f: fn(a) -> b) -> ZipList(b)

Map a function over the elements of the ZipList

Examples

let numbers = new([1, 2], 3, [4, 5])
map(numbers, fn(x) { x * 2 }) == new([2, 4], 6, [8, 10])
pub fn new(
  previous previous: List(a),
  current current: a,
  next next: List(a),
) -> ZipList(a)

Create a new ZipList

Examples

let numbers = new([1, 2], 3, [4, 5])
current(numbers)
// => 3
pub fn next(zip_list: ZipList(a)) -> ZipList(a)

Move the cursor to the next element Returns the same ZipList if there is no next element

Examples

let numbers = new([1, 2], 3, [4, 5])
next(numbers) == new([1, 2, 3], 4, [5])
let numbers = new([1, 2, 3, 4, 5], 6, [])
next(numbers) == new([1, 2, 3, 4, 5], 6, [])
pub fn next_try(zip_list: ZipList(a)) -> Result(ZipList(a), Nil)

Move the cursor to the next element Returns Error(Nil) if there is no next element

Examples

let numbers = new([1, 2], 3, [4, 5])
next_try(numbers) == Ok(new([1, 2, 3], 4, [5]))
let numbers = new([1, 2, 3, 4, 5], 6, [])
next_try(numbers) == Error(Nil)
pub fn next_wrap(zip_list: ZipList(a)) -> ZipList(a)

Move the cursor to the next element, wrapping around to the first element if there is no next element

Examples

let numbers = new([1, 2], 3, [4, 5])
next_wrap(numbers) == new([1, 2, 3], 4, [5])
let numbers = new([1, 2, 3, 4, 5], 6, [])
next_wrap(numbers) == new([], 1, [2, 3, 4, 5, 6])
pub fn prepend_next(
  zip_list: ZipList(a),
  next: List(a),
) -> ZipList(a)

Prepend a list to the next elements of the ZipList

Examples

let numbers = new([1, 2], 3, [4, 5])
prepend_next(numbers, [6, 7]) == new([1, 2], 3, [6, 7, 4, 5])
pub fn prepend_previous(
  zip_list: ZipList(a),
  previous: List(a),
) -> ZipList(a)

Prepend a list to the previous elements of the ZipList

Examples

let numbers = new([1, 2], 3, [4, 5])
prepend_previous(numbers, [-1, 0]) == new([-1, 0, 1, 2], 3, [4, 5])
pub fn previous(zip_list: ZipList(a)) -> ZipList(a)

Move the cursor to the previous element Returns the same ZipList if there is no previous element

Examples

let numbers = new([1, 2], 3, [4, 5])
previous(numbers) == new([1], 2, [3, 4, 5])
let numbers = new([], 1, [2, 3, 4, 5])
previous(numbers) == new([], 1, [2, 3, 4, 5])
pub fn previous_try(
  zip_list: ZipList(a),
) -> Result(ZipList(a), Nil)

Move the cursor to the previous element Returns Error(Nil) if there is no previous element

Examples

let numbers = new([1, 2], 3, [4, 5])
previous_try(numbers) == Ok(new([1], 2, [3, 4, 5]))
let numbers = new([], 1, [2, 3, 4, 5])
previous_try(numbers) == Error(Nil)
pub fn previous_wrap(zip_list: ZipList(a)) -> ZipList(a)

Move the cursor to the previous element, wrapping around to the last element if there is no previous element

Examples

let numbers = new([1, 2], 3, [4, 5])
previous_wrap(numbers) == new([1, 2, 3], 4, [5])
let numbers = new([], 1, [2, 3, 4, 5])
previous_wrap(numbers) == new([1, 2, 3, 4], 5, [])
pub fn remove(zip_list: ZipList(a)) -> ZipList(a)

Remove the current element from the ZipList The cursor is moved to the next element if there is one, otherwise to the previous element Returns the same ZipList if there is no next or previous element

Examples

let numbers = new([1, 2], 3, [4, 5])
remove(numbers) == new([1, 2], 4, [5])
let numbers = new([1, 2], 3, [])
remove(numbers) == new([1], 2, [])
let numbers = new([], 1, [])
remove(numbers) == new([], 1, [])
pub fn remove_backwards(zip_list: ZipList(a)) -> ZipList(a)

Remove the current element from the ZipList The cursor is moved to the previous element if there is one, otherwise to the next element

Examples

let numbers = new([1, 2], 3, [4, 5])
remove_backwards(numbers) == new([1], 2, [3, 4, 5])
let numbers = new([], 1, [2, 3])
remove_backwards(numbers) == new([], 2, [3])
let numbers = new([], 1, [])
remove(numbers) == new([], 1, [])
pub fn remove_try(
  zip_list: ZipList(a),
) -> Result(ZipList(a), Nil)

Remove the current element from the ZipList The cursor is moved to the next element if there is one, otherwise to the previous element Returns Error(Nil) if there is no next or previous element

Examples

let numbers = new([1, 2], 3, [4, 5])
remove_try(numbers) == Ok(new([1, 2], 4, [5]))
let numbers = new([1, 2], 3, [])
remove_try(numbers) == Ok(new([1], 2, []))
let numbers = new([], 1, [])
remove_try(numbers) == Error(Nil)
pub fn replace(
  zip_list: ZipList(a),
  new_current: a,
) -> ZipList(a)

Replace the current element of the ZipList

Examples

let numbers = new([1, 2], 3, [4, 5])
replace(numbers, 6) == new([1, 2], 6, [4, 5])
pub fn singleton(current: a) -> ZipList(a)

Create a new ZipList with a single element

Examples

singleton(1) == new([], 1, [])
pub fn to_list(zip_list: ZipList(a)) -> List(a)

Convert a ZipList to a list

Examples

to_list(new([1, 2], 3, [4, 5]))
// => [1, 2, 3, 4, 5]
Search Document