ranger

Functions

pub fn create(
  validate validate: fn(a) -> Bool,
  negate_step negate_step: fn(b) -> b,
  add add: fn(a, b) -> a,
  compare compare: fn(a, a) -> Order,
) -> fn(a, a, b) -> Result(Yielder(a), Nil)

returns a function that can be used to create a range

Examples

> let range =
>  create(
>    validate: fn(a) { string.length(a) == 1 },
>    negate_step: fn(s) { -1 * s },
>    add: fn(a: String, b: Int) {
>      let assert [code] = string.to_utf_codepoints(a)
>      let int_code = string.utf_codepoint_to_int(code)
>      let new_int_code = int_code + b
>      let assert Ok(new_code) = string.utf_codepoint(new_int_code)
>      string.from_utf_codepoints([new_code])
>    },
>    compare: string.compare,
>  )

> range("ab", "e", 1)
Error(Nil)

> let assert Ok(a_to_e) = range("a", "e", 1)
> a_to_e |> yielder.to_list
["a", "b", "c", "d", "e"]

> let assert Ok(z_to_p) = range("z", "p", 1)
> z_to_p |> yielder.to_list
["z", "y", "x", "w", "v", "u", "t", "s", "r", "q", "p"]

> let assert Ok(z_to_p) = range("z", "p", -2)
> z_to_p |> yielder.to_list
["z", "x", "v", "t", "r", "p"]

> let assert Ok(z_to_p) = range("z", "p", 3)
> z_to_p |> yielder.to_list
["z", "w", "t", "q"]
> let range =
>    create(
>      validate: fn(_) { True },
>      negate_step: fn(s) { -1.0 *. s },
>      add: fn(a, b) { a +. b },
>      compare: float.compare,
>    )

> let assert Ok(weird_step_case) = range(1.25, 4.5, -0.5)
> weird_step_case |> yielder.to_list
[1.25, 1.75, 2.25, 2.75, 3.25, 3.75, 4.25]

> let assert Ok(single_item_case) = range(1.25, 1.25, -0.25)
> single_item_case |> yielder.to_list
[1.25]

> let assert Ok(zero_step_case) = range(2.5, 5.0, 0)
> zero_step_case |> yielder.to_list
[2.5]
pub fn create_infinite(
  validate validate: fn(a) -> Bool,
  add add: fn(a, b) -> a,
  compare compare: fn(a, a) -> Order,
) -> fn(a, b) -> Result(Yielder(a), Nil)

returns a function that can be used to create an infinite range

should be used carefully because careless use of infinite yielders could crash your app

Examples

> let range =
>  create_infinite(
>    validate: fn(a) { string.length(a) == 1 },
>    add: fn(a: String, b: Int) {
>      let assert [code] = string.to_utf_codepoints(a)
>      let int_code = string.utf_codepoint_to_int(code)
>      let new_int_code = int_code + b
>      let assert Ok(new_code) = string.utf_codepoint(new_int_code)
>      string.from_utf_codepoints([new_code])
>    },
>    compare: string.compare,
>   )

> let assert Ok(from_a) = range("a", 1)
> from_a |> yielder.take(26) |> yielder.to_list
["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
  "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
Search Document