### Examples for all function Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Usage examples for the all function with different inputs. ```gleam empty() |> all(fn(n) { n % 2 == 0 }) // -> True ``` ```gleam from_list([2, 4, 6, 8]) |> all(fn(n) { n % 2 == 0 }) // -> True ``` ```gleam from_list([2, 4, 5, 8]) |> all(fn(n) { n % 2 == 0 }) // -> False ``` -------------------------------- ### Use transform Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Example of using transform to implement an index-tracking operation. ```gleam from_list(["a", "b", "c"]) |> transform(0, fn(i, el) { Next(#(i, el), i + 1) }) |> to_list // -> [#(0, "a"), #(1, "b"), #(2, "c")] ``` -------------------------------- ### Use take_while Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Example of filtering elements using take_while. ```gleam from_list([1, 2, 3, 2, 4]) |> take_while(satisfying: fn(x) { x < 3 }) |> to_list // -> [1, 2] ``` -------------------------------- ### prepend Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Adds a new element to the start of a yielder. ```APIDOC ## prepend(yielder, element) ### Signature `pub fn prepend(yielder: Yielder(a), element: a) -> Yielder(a)` ### Description Add a new element to the start of a yielder. ``` -------------------------------- ### Take elements from a Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Examples showing how to take a specific number of elements from a Yielder. ```gleam from_list([1, 2, 3, 4, 5]) |> take(up_to: 3) |> to_list // -> [1, 2, 3] ``` ```gleam from_list([1, 2]) |> take(up_to: 3) |> to_list // -> [1, 2] ``` -------------------------------- ### Use first on a Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Examples of retrieving the first element from a Yielder. ```gleam from_list([1, 2, 3]) |> first // -> Ok(1) ``` ```gleam empty() |> first // -> Error(Nil) ``` -------------------------------- ### Find elements in a Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Examples demonstrating how to find the first element matching a predicate in a Yielder. ```gleam find(from_list([1, 2, 3]), fn(x) { x > 2 }) // -> Ok(3) ``` ```gleam find(from_list([1, 2, 3]), fn(x) { x > 4 }) // -> Error(Nil) ``` ```gleam find(empty(), fn(_) { True }) // -> Error(Nil) ``` -------------------------------- ### range Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder of integers, starting at a given start integer and stepping by one to a given end integer. ```APIDOC ## range(from, to) ### Signature `pub fn range(from start: Int, to stop: Int) -> Yielder(Int)` ### Description Creates a yielder of ints, starting at a given start int and stepping by one to a given end int. ``` -------------------------------- ### Use find_map on a Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Examples of using find_map to transform and retrieve the first successful result from a Yielder. ```gleam find_map(from_list(["a", "1", "2"]), int.parse) // -> Ok(1) ``` ```gleam find_map(from_list(["a", "b", "c"]), int.parse) // -> Error(Nil) ``` ```gleam find_map(from_list([]), int.parse) // -> Error(Nil) ``` -------------------------------- ### Use flat_map on a Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Example of using flat_map to expand elements into a new Yielder. ```gleam from_list([1, 2]) |> flat_map(fn(x) { from_list([x, x + 1]) }) |> to_list // -> [1, 2, 2, 3] ``` -------------------------------- ### Prepend Element to Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Adds a new element to the start of a yielder. ```gleam let yielder = from_list([1, 2, 3]) |> prepend(0) yielder.to_list // -> [0, 1, 2, 3] ``` -------------------------------- ### Index Yielder elements Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Wraps each yielded element with its index starting from 0. ```gleam pub fn index( over yielder: Yielder(element), ) -> Yielder(#(element, Int)) ``` ```gleam from_list(["a", "b", "c"]) |> index |> to_list // -> [#("a", 0), #("b", 1), #("c", 2)] ``` -------------------------------- ### yield Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Add a new element to the start of a yielder, designed for use with use expressions. ```APIDOC ## pub fn yield(element: a, next: fn() -> Yielder(a)) -> Yielder(a) ### Description Add a new element to the start of a yielder. This function is for use with `use` expressions, to replicate the behaviour of the `yield` keyword found in other languages. ### Parameters - **element** (a) - The element to yield. - **next** (fn() -> Yielder(a)) - A function returning the rest of the yielder. ``` -------------------------------- ### reduce Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Reduces a yielder to a single value using a combining function, starting from the first element. ```APIDOC ## reduce(over: Yielder(e), with: fn(e, e) -> e) -> Result(e, Nil) ### Description Reduces the yielder by applying a function to the accumulator and the next element. Returns Ok with the final result, or Error if the yielder is empty. ### Parameters - **over** (Yielder(e)) - Required - The yielder to reduce. - **with** (fn(e, e) -> e) - Required - The function to combine elements. ``` -------------------------------- ### Reduce a Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Combines elements of a yielder starting from the first element. Returns Error if the yielder is empty. ```gleam pub fn reduce( over yielder: Yielder(e), with f: fn(e, e) -> e, ) -> Result(e, Nil) ``` ```gleam from_list([]) |> reduce(fn(acc, x) { acc + x }) // -> Error(Nil) ``` ```gleam from_list([1, 2, 3, 4, 5]) |> reduce(fn(acc, x) { acc + x }) // -> Ok(15) ``` -------------------------------- ### Drop elements while predicate is true Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Drops elements from the start of the yielder as long as the predicate function returns True. ```gleam pub fn drop_while( in yielder: Yielder(element), satisfying predicate: fn(element) -> Bool, ) -> Yielder(element) ``` ```gleam from_list([1, 2, 3, 4, 2, 5]) |> drop_while(satisfying: fn(x) { x < 4 }) |> to_list // -> [4, 2, 5] ``` -------------------------------- ### Get the last element Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Retrieves the final element of a Yielder, returning Error(Nil) if empty. This operation is linear in time. ```gleam pub fn last(yielder: Yielder(element)) -> Result(element, Nil) ``` ```gleam empty() |> last // -> Error(Nil) ``` ```gleam range(1, 10) |> last // -> Ok(10) ``` -------------------------------- ### take Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder that only yields the first N elements. ```APIDOC ## pub fn take(from yielder: Yielder(e), up_to desired: Int) -> Yielder(e) ### Description Creates a yielder that only yields the first `desired` elements. If the yielder does not have enough elements, all of them are yielded. ### Parameters - **yielder** (Yielder(e)) - The source yielder. - **desired** (Int) - The maximum number of elements to yield. ### Returns - **Yielder(e)** - A yielder limited to the first `desired` elements. ``` -------------------------------- ### run Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Evaluates all elements in a yielder. ```APIDOC ## run(yielder: Yielder(e)) -> Nil ### Description Evaluates all elements emitted by the given yielder, useful for triggering side effects. ### Parameters - **yielder** (Yielder(e)) - Required - The yielder to evaluate. ``` -------------------------------- ### Generate partial sums with scan Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Demonstrates using scan to accumulate values from a list into a new sequence. ```gleam // Generate a sequence of partial sums from_list([1, 2, 3, 4, 5]) |> scan(from: 0, with: fn(acc, el) { acc + el }) |> to_list // -> [1, 3, 6, 10, 15] ``` -------------------------------- ### Step constructors Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Constructors for the Step type. ```gleam Next(element: element, accumulator: accumulator) ``` ```gleam Done ``` -------------------------------- ### step Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Eagerly accesses the first value of a yielder. ```APIDOC ## pub fn step(yielder: Yielder(e)) -> Step(e, Yielder(e)) ### Description Eagerly accesses the first value of a yielder, returning a `Next` that contains the first value and the rest of the yielder. If called on an empty yielder, `Done` is returned. ### Parameters - **yielder** (Yielder(e)) - The source yielder. ### Returns - **Step(e, Yielder(e))** - A `Next` variant containing the first element and the remaining yielder, or `Done` if empty. ``` -------------------------------- ### Create a single-element Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Defines the single function signature and shows its usage. ```gleam pub fn single(elem: element) -> Yielder(element) ``` ```gleam single(1) |> to_list // -> [1] ``` -------------------------------- ### each Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Traverse a yielder, calling a function on each element. ```APIDOC ## each ### Signature `pub fn each(over yielder: Yielder(a), with f: fn(a) -> b) -> Nil` ### Description Traverse a yielder, calling a function on each element. ``` -------------------------------- ### Create a Yielder from a list Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Converts a standard list into a Yielder. ```gleam pub fn from_list(list: List(element)) -> Yielder(element) ``` ```gleam from_list([1, 2, 3, 4]) |> to_list // -> [1, 2, 3, 4] ``` -------------------------------- ### Run a Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Evaluates all elements in a yielder to trigger side effects. ```gleam pub fn run(yielder: Yielder(e)) -> Nil ``` -------------------------------- ### Add elements with yield Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Prepends elements to a yielder using the use syntax to mimic the yield keyword. ```gleam pub fn yield(element: a, next: fn() -> Yielder(a)) -> Yielder(a) ``` ```gleam let yielder = { use <- yield(1) use <- yield(2) use <- yield(3) empty() } yielder |> to_list // -> [1, 2, 3] ``` -------------------------------- ### Create Range Yielders Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Generates a sequence of integers between two bounds. ```gleam range(from: 1, to: 5) |> to_list // -> [1, 2, 3, 4, 5] ``` ```gleam range(from: 1, to: -2) |> to_list // -> [1, 0, -1, -2] ``` ```gleam range(from: 0, to: 0) |> to_list // -> [0] ``` -------------------------------- ### Step through a Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Defines the step function and shows how to extract the first element and the remainder of the sequence. ```gleam pub fn step(yielder: Yielder(e)) -> Step(e, Yielder(e)) ``` ```gleam let assert Next(first, rest) = from_list([1, 2, 3, 4]) |> step first // -> 1 rest |> to_list // -> [2, 3, 4] ``` ```gleam empty() |> step // -> Done ``` -------------------------------- ### repeat Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates an infinite yielder that returns the same value. ```APIDOC ## repeat(x: element) -> Yielder(element) ### Description Creates a yielder that returns the same value infinitely. ### Parameters - **x** (element) - Required - The value to repeat. ``` -------------------------------- ### zip Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Zips two yielders together, emitting values from both until the shorter one runs out. ```APIDOC ## pub fn zip(left: Yielder(a), right: Yielder(b)) -> Yielder(#(a, b)) ### Description Zips two yielders together, emitting values from both until the shorter one runs out. ### Parameters - **left** (Yielder(a)) - The first yielder. - **right** (Yielder(b)) - The second yielder. ``` -------------------------------- ### Define first function Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Signature for the first function which retrieves the initial element of a Yielder. ```gleam pub fn first(from yielder: Yielder(e)) -> Result(e, Nil) ``` -------------------------------- ### at Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Retrieves the nth element from a yielder by index. ```APIDOC ## at ### Signature `pub fn at(in yielder: Yielder(e), get index: Int) -> Result(e, Nil)` ### Description Returns nth element yielded by the given yielder, where 0 means the first element. If there are not enough elements in the yielder, Error(Nil) is returned. For any index less than 0 this function behaves as if it was set to 0. ``` -------------------------------- ### empty Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder that yields no elements. ```APIDOC ## empty() ### Description Creates a yielder that yields no elements. ### Signature `pub fn empty() -> Yielder(element)` ### Example ```gleam empty() |> to_list // -> [] ``` ``` -------------------------------- ### index Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Wraps values yielded from a yielder with indices. ```APIDOC ## index(over: Yielder(element)) -> Yielder(#(element, Int)) ### Description Wraps values yielded from a yielder with indices, starting from 0. ### Signature `pub fn index(over yielder: Yielder(element)) -> Yielder(#(element, Int))` ``` -------------------------------- ### Create a Yielder with unfold Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder from an initial accumulator and a function that returns either Done or Next. ```gleam pub fn unfold( from initial: acc, with f: fn(acc) -> Step(element, acc), ) -> Yielder(element) ``` ```gleam unfold(from: 5, with: fn(n) { case n { 0 -> Done n -> Next(element: n, accumulator: n - 1) } }) |> to_list // -> [5, 4, 3, 2, 1] ``` -------------------------------- ### Iterate with a function Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates an infinite Yielder by repeatedly applying a function to an initial value. ```gleam pub fn iterate( from initial: element, with f: fn(element) -> element, ) -> Yielder(element) ``` ```gleam iterate(1, fn(n) { n * 3 }) |> take(5) |> to_list // -> [1, 3, 9, 27, 81] ``` -------------------------------- ### Fold a Yielder with try_fold Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Demonstrates using try_fold to process a yielder, returning an error if a condition is not met. ```gleam from_list([1, 2, 3, 4]) |> try_fold(0, fn(acc, i) { case i < 3 { True -> Ok(acc + i) False -> Error(Nil) } }) // -> Error(Nil) ``` -------------------------------- ### map Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder from an existing yielder and a transformation function. ```APIDOC ## map(over: Yielder(a), with: fn(a) -> b) -> Yielder(b) ### Description Creates a yielder from an existing yielder and a transformation function. Each element in the new yielder will be the result of calling the given function on the elements in the given yielder. This function does not evaluate the elements of the yielder, the computation is performed when the yielder is later run. ### Parameters - **over** (Yielder(a)) - The source yielder. - **with** (fn(a) -> b) - The transformation function. ### Returns - **Yielder(b)** - A new yielder with transformed elements. ``` -------------------------------- ### Chunk elements by key Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Groups consecutive elements into chunks based on a key function. ```gleam pub fn chunk( over yielder: Yielder(element), by f: fn(element) -> key, ) -> Yielder(List(element)) ``` ```gleam from_list([1, 2, 2, 3, 4, 4, 6, 7, 7]) |> chunk(by: fn(n) { n % 2 }) |> to_list // -> [[1], [2, 2], [3], [4, 4, 6], [7, 7]] ``` -------------------------------- ### Define all function Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Signature for the all function, which checks if all elements in a yielder satisfy a predicate. ```gleam pub fn all( in yielder: Yielder(element), satisfying predicate: fn(element) -> Bool, ) -> Bool ``` -------------------------------- ### Create an empty Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder that yields no elements. ```gleam pub fn empty() -> Yielder(element) ``` ```gleam empty() |> to_list // -> [] ``` -------------------------------- ### Create a Single-Element Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder that yields exactly one element. ```gleam once(fn() { 1 }) |> to_list // -> [1] ``` -------------------------------- ### Define any function Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Signature for the any function, which checks if at least one element in a yielder satisfies a predicate. ```gleam pub fn any( in yielder: Yielder(element), satisfying predicate: fn(element) -> Bool, ) -> Bool ``` -------------------------------- ### Define take_while Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Signature for the take_while function which yields elements while a predicate is true. ```gleam pub fn take_while( in yielder: Yielder(element), satisfying predicate: fn(element) -> Bool, ) -> Yielder(element) ``` -------------------------------- ### from_list Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder that yields each element from the given list. ```APIDOC ## from_list(list: List(element)) -> Yielder(element) ### Description Creates a yielder that yields each element from the given list. ### Signature `pub fn from_list(list: List(element)) -> Yielder(element)` ``` -------------------------------- ### Cycle a Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Repeats a yielder infinitely. Use take to limit the output. ```gleam pub fn cycle(yielder: Yielder(a)) -> Yielder(a) ``` ```gleam from_list([1, 2]) |> cycle |> take(6) |> to_list // -> [1, 2, 1, 2, 1, 2] ``` -------------------------------- ### Chunk Yielder into fixed sizes Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Defines the sized_chunk function and demonstrates chunking behavior with full and partial chunks. ```gleam pub fn sized_chunk( over yielder: Yielder(element), into count: Int, ) -> Yielder(List(element)) ``` ```gleam from_list([1, 2, 3, 4, 5, 6]) |> sized_chunk(into: 2) |> to_list // -> [[1, 2], [3, 4], [5, 6]] ``` ```gleam from_list([1, 2, 3, 4, 5, 6, 7, 8]) |> sized_chunk(into: 3) |> to_list // -> [[1, 2, 3], [4, 5, 6], [7, 8]] ``` -------------------------------- ### Prepend Yielder Signature Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html The function signature for prepending an element. ```gleam pub fn prepend(yielder: Yielder(a), element: a) -> Yielder(a) ``` -------------------------------- ### sized_chunk Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder that emits chunks of a given size from the source yielder. ```APIDOC ## pub fn sized_chunk(over yielder: Yielder(element), into count: Int) -> Yielder(List(element)) ### Description Creates a yielder that emits chunks of given size. If the last chunk does not have `count` elements, it is yielded as a partial chunk. For any `count` less than 1, this function behaves as if it was set to 1. ### Parameters - **yielder** (Yielder(element)) - The source yielder. - **count** (Int) - The size of each chunk. ### Returns - **Yielder(List(element))** - A yielder of lists, where each list is a chunk of the specified size. ``` -------------------------------- ### first Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Returns the first element yielded by the given yielder, if it exists. ```APIDOC ## first(from: Yielder(e)) -> Result(e, Nil) ### Description Returns the first element yielded by the given yielder, if it exists, or `Error(Nil)` otherwise. ### Parameters - **from** (Yielder(e)) - The yielder to retrieve the first element from. ### Returns - **Result(e, Nil)** - The first element or `Error(Nil)` if the yielder is empty. ``` -------------------------------- ### take_while Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder that yields elements from the input yielder as long as the provided predicate function returns True. ```APIDOC ## take_while ### Signature `pub fn take_while(in yielder: Yielder(element), satisfying predicate: fn(element) -> Bool) -> Yielder(element)` ### Description Creates a yielder that yields elements while the predicate returns `True`. ### Example ```gleam from_list([1, 2, 3, 2, 4]) |> take_while(satisfying: fn(x) { x < 3 }) |> to_list // -> [1, 2] ``` ``` -------------------------------- ### Scan a Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder that yields intermediate results of a fold operation. ```gleam pub fn scan( over yielder: Yielder(element), from initial: acc, with f: fn(acc, element) -> acc, ) -> Yielder(acc) ``` -------------------------------- ### single Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder that yields the given element exactly once. ```APIDOC ## pub fn single(elem: element) -> Yielder(element) ### Description Creates a yielder that yields the given element exactly once. ### Parameters - **elem** (element) - The element to be yielded. ### Returns - **Yielder(element)** - A yielder containing the single element. ``` -------------------------------- ### iterate Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder that infinitely applies a function to a value. ```APIDOC ## iterate(from: element, with: fn(element) -> element) -> Yielder(element) ### Description Creates a yielder that infinitely applies a function to a value. ### Parameters - **from** (element) - The initial value. - **with** (fn(element) -> element) - The function to apply to the value. ### Returns - **Yielder(element)** - A new yielder. ``` -------------------------------- ### unfold Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder from a given function and accumulator. The function is called on the accumulator and returns either Done or Next. ```APIDOC ## pub fn unfold(from: acc, with: fn(acc) -> Step(element, acc)) -> Yielder(element) ### Description Creates a yielder from a given function and accumulator. The function is called on the accumulator and returns either `Done`, indicating the yielder has no more elements, or `Next` which contains a new element and accumulator. ### Parameters - **from** (acc) - The initial accumulator value. - **with** (fn(acc) -> Step(element, acc)) - The function to generate the next element and accumulator. ``` -------------------------------- ### drop Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Evaluates and discards the first N elements in a yielder, returning a new yielder. ```APIDOC ## drop ### Signature `pub fn drop(from yielder: Yielder(e), up_to desired: Int) -> Yielder(e)` ### Description Evaluates and discards the first N elements in a yielder, returning a new yielder. If the yielder does not have enough elements an empty yielder is returned. ``` -------------------------------- ### to_list Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Evaluates a yielder and collects all its elements into a list. Note that this will not terminate if the yielder is infinite. ```APIDOC ## to_list ### Signature `pub fn to_list(yielder: Yielder(element)) -> List(element)` ### Description Evaluates a yielder and returns all the elements as a list. If called on a yielder of infinite length then this function will never return. ### Example ```gleam from_list([1, 2, 3]) |> map(fn(x) { x * 2 }) |> to_list // -> [2, 4, 6] ``` ``` -------------------------------- ### scan Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder that yields intermediate fold results. ```APIDOC ## scan(over: Yielder(element), from: acc, with: fn(acc, element) -> acc) -> Yielder(acc) ### Description Behaves like fold, but yields intermediate results as a new yielder. ### Parameters - **over** (Yielder(element)) - Required - The source yielder. - **from** (acc) - Required - The initial accumulator value. - **with** (fn(acc, element) -> acc) - Required - The stateful function. ``` -------------------------------- ### cycle Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder that repeats a given yielder infinitely. ```APIDOC ## cycle ### Signature `pub fn cycle(yielder: Yielder(a)) -> Yielder(a)` ### Description Creates a yielder that repeats a given yielder infinitely. ``` -------------------------------- ### Repeat a value Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder that returns the same value infinitely. ```gleam pub fn repeat(x: element) -> Yielder(element) ``` ```gleam repeat(10) |> take(4) |> to_list // -> [10, 10, 10, 10] ``` -------------------------------- ### Define to_list Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Signature for the to_list function which evaluates a yielder into a list. ```gleam pub fn to_list(yielder: Yielder(element)) -> List(element) ``` -------------------------------- ### repeatedly Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder that yields values from a function. ```APIDOC ## repeatedly(f: fn() -> element) -> Yielder(element) ### Description Creates a yielder that yields values created by calling a given function repeatedly. ### Parameters - **f** (fn() -> element) - Required - The function to call for each yield. ``` -------------------------------- ### once Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder that yields exactly one element provided by calling the given function. ```APIDOC ## once(f) ### Signature `pub fn once(f: fn() -> element) -> Yielder(element)` ### Description Creates a yielder that yields exactly one element provided by calling the given function. ``` -------------------------------- ### interleave Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Alternates between two yielders. ```APIDOC ## interleave(left: Yielder(element), with: Yielder(element)) -> Yielder(element) ### Description Creates a yielder that alternates between the two given yielders until both have run out. ### Signature `pub fn interleave(left: Yielder(element), with right: Yielder(element)) -> Yielder(element)` ``` -------------------------------- ### find Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Finds the first element in a yielder that satisfies a predicate. ```APIDOC ## find(in haystack: Yielder(a), one_that is_desired: fn(a) -> Bool) ### Description Finds the first element in a given yielder for which the given function returns `True`. Returns `Error(Nil)` if the function does not return `True` for any of the elements. ``` -------------------------------- ### Check for elements with any Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Checks if any element in the yielder satisfies the given predicate. ```gleam empty() |> any(fn(n) { n % 2 == 0 }) // -> False ``` ```gleam from_list([1, 2, 5, 7, 9]) |> any(fn(n) { n % 2 == 0 }) // -> True ``` ```gleam from_list([1, 3, 5, 7, 9]) |> any(fn(n) { n % 2 == 0 }) // -> False ``` -------------------------------- ### Concatenate nested lists with Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Demonstrates flattening a list of lists using map, concat, and to_list. ```gleam [[1, 2], [3, 4]] |> map(from_list) |> concat |> to_list // -> [1, 2, 3, 4] ``` -------------------------------- ### Once Yielder Signature Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html The function signature for creating a single-element yielder. ```gleam pub fn once(f: fn() -> element) -> Yielder(element) ``` -------------------------------- ### Take elements from a Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Defines the take function signature for limiting the number of yielded elements. ```gleam pub fn take( from yielder: Yielder(e), up_to desired: Int, ) -> Yielder(e) ``` -------------------------------- ### concat Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Joins a list of yielders into a single yielder. ```APIDOC ## concat ### Signature `pub fn concat(yielders: List(Yielder(a))) -> Yielder(a)` ### Description Joins a list of yielders into a single yielder. This function does not evaluate the elements of the yielder, the computation is performed when the yielder is later run. ``` -------------------------------- ### append Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Appends two yielders, producing a new yielder. The computation is deferred until the resulting yielder is evaluated. ```APIDOC ## append ### Signature `pub fn append(to first: Yielder(a), suffix second: Yielder(a)) -> Yielder(a)` ### Description Appends two yielders, producing a new yielder. This function does not evaluate the elements of the yielders, the computation is performed when the resulting yielder is later run. ``` -------------------------------- ### Find an element in a Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Returns the first element that satisfies the predicate, or Error(Nil) if none match. ```gleam pub fn find( in haystack: Yielder(a), one_that is_desired: fn(a) -> Bool, ) -> Result(a, Nil) ``` -------------------------------- ### Repeatedly yield values Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder that yields values generated by a function. ```gleam pub fn repeatedly(f: fn() -> element) -> Yielder(element) ``` ```gleam repeatedly(fn() { 7 }) |> take(3) |> to_list // -> [7, 7, 7] ``` -------------------------------- ### Range Yielder Signature Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html The function signature for creating a range of integers. ```gleam pub fn range(from start: Int, to stop: Int) -> Yielder(Int) ``` -------------------------------- ### transform Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a new yielder from an existing one using a stateful function that can yield elements or halt the sequence. ```APIDOC ## transform ### Signature `pub fn transform(over yielder: Yielder(a), from initial: acc, with f: fn(acc, a) -> Step(b, acc)) -> Yielder(b)` ### Description Creates a yielder from an existing yielder and a stateful function that may short-circuit. `f` takes arguments `acc` for current state and `el` for current element from underlying yielder, and returns either `Next` with yielded element and new state value, or `Done` to halt the yielder. ### Example ```gleam from_list(["a", "b", "c"]) |> transform(0, fn(i, el) { Next(#(i, el), i + 1) }) |> to_list // -> [#(0, "a"), #(1, "b"), #(2, "c")] ``` ``` -------------------------------- ### length Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Counts the number of elements in the given yielder. ```APIDOC ## length(over: Yielder(e)) -> Int ### Description Counts the number of elements in the given yielder. This function has to traverse the entire yielder to count its elements, so it runs in linear time. ### Parameters - **over** (Yielder(e)) - The yielder to count. ### Returns - **Int** - The total count of elements. ``` -------------------------------- ### Group Yielder elements Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Groups elements into a dictionary based on a key function. ```gleam pub fn group( in yielder: Yielder(element), by key: fn(element) -> key, ) -> dict.Dict(key, List(element)) ``` ```gleam from_list([1, 2, 3, 4, 5, 6]) |> group(by: fn(n) { n % 3 }) // -> dict.from_list([#(0, [3, 6]), #(1, [1, 4]), #(2, [2, 5])]) ``` -------------------------------- ### Define find_map function Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Signature for the find_map function which searches for the first element that maps to an Ok result. ```gleam pub fn find_map( in haystack: Yielder(a), one_that is_desired: fn(a) -> Result(b, c), ) -> Result(b, Nil) ``` -------------------------------- ### drop_while Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder that drops elements while the predicate returns True. ```APIDOC ## drop_while ### Signature `pub fn drop_while(in yielder: Yielder(element), satisfying predicate: fn(element) -> Bool) -> Yielder(element)` ### Description Creates a yielder that drops elements while the predicate returns `True`, and then yields the remaining elements. ``` -------------------------------- ### chunk Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Groups elements of a yielder into chunks based on a key function. ```APIDOC ## chunk ### Signature `pub fn chunk(over yielder: Yielder(element), by f: fn(element) -> key) -> Yielder(List(element))` ### Description Creates a yielder that emits chunks of elements for which f returns the same value. ``` -------------------------------- ### Zip two yielders Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Combines two yielders into a single yielder of tuples, stopping when the shortest yielder is exhausted. ```gleam pub fn zip( left: Yielder(a), right: Yielder(b), ) -> Yielder(#(a, b)) ``` ```gleam from_list(["a", "b", "c"]) |> zip(range(20, 30)) |> to_list // -> [#("a", 20), #("b", 21), #("c", 22)] ``` -------------------------------- ### flat_map Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a new yielder by applying a transformation function to each element and flattening the results. ```APIDOC ## flat_map(over: Yielder(a), with: fn(a) -> Yielder(b)) -> Yielder(b) ### Description Creates a yielder from an existing yielder and a transformation function. Each element in the new yielder will be the result of calling the given function on the elements in the given yielder and then flattening the results. ### Parameters - **over** (Yielder(a)) - The source yielder. - **with** (fn(a) -> Yielder(b)) - A function that transforms each element into a new yielder. ### Returns - **Yielder(b)** - A new flattened yielder. ``` -------------------------------- ### Traverse a Yielder with each Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Executes a side-effecting function for every element in the yielder. ```gleam pub fn each(over yielder: Yielder(a), with f: fn(a) -> b) -> Nil ``` ```gleam empty() |> each(io.println) // -> Nil ``` ```gleam from_list(["Tom", "Malory", "Louis"]) |> each(io.println) // -> Nil // Tom // Malory // Louis ``` -------------------------------- ### any Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Returns True if any element emitted by the yielder satisfies the given predicate, False otherwise. This function short-circuits once it finds a satisfying element. ```APIDOC ## any(yielder: Yielder(element), predicate: fn(element) -> Bool) -> Bool ### Description Returns True if any element emitted by the yielder satisfies the given predicate, False otherwise. An empty yielder results in False. ### Parameters - **in** (Yielder(element)) - Required - The yielder to check. - **satisfying** (fn(element) -> Bool) - Required - The predicate function to apply to each element. ``` -------------------------------- ### Fold a Yielder until a condition Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Reduces a Yielder using a function that can signal early termination via list.ContinueOrStop. ```gleam pub fn fold_until( over yielder: Yielder(e), from initial: acc, with f: fn(acc, e) -> list.ContinueOrStop(acc), ) -> acc ``` ```gleam import gleam/list let f = fn(acc, e) { case e { _ if e < 4 -> list.Continue(e + acc) _ -> list.Stop(acc) } } from_list([1, 2, 3, 4]) |> fold_until(from: 0, with: f) // -> 6 ``` -------------------------------- ### group Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Groups elements from a yielder into a dictionary based on a key function. ```APIDOC ## group(in: Yielder(element), by: fn(element) -> key) -> dict.Dict(key, List(element)) ### Description Returns a Dict(k, List(element)) of elements from the given yielder grouped with the given key function. The order within each group is preserved from the yielder. ### Signature `pub fn group(in yielder: Yielder(element), by key: fn(element) -> key) -> dict.Dict(key, List(element))` ``` -------------------------------- ### fold_until Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Reduces a yielder of elements into a single value with early termination support. ```APIDOC ## fold_until ### Signature `pub fn fold_until(over yielder: Yielder(e), from initial: acc, with f: fn(acc, e) -> list.ContinueOrStop(acc)) -> acc` ### Description Reduces a yielder of elements into a single value by calling a given function on each element in turn, but uses list.ContinueOrStop to determine whether or not to keep iterating. ``` -------------------------------- ### Define try_fold Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Signature for the try_fold function which performs a fallible fold operation. ```gleam pub fn try_fold( over yielder: Yielder(e), from initial: acc, with f: fn(acc, e) -> Result(acc, err), ) -> Result(acc, err) ``` -------------------------------- ### Combine Yielders with map2 Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Combines two yielders into one, dropping extra elements if lengths differ. ```gleam let first = from_list([1, 2, 3]) let second = from_list([4, 5, 6]) map2(first, second, fn(x, y) { x + y }) |> to_list // -> [5, 7, 9] ``` ```gleam let first = from_list([1, 2]) let second = from_list(["a", "b", "c"]) map2(first, second, fn(i, x) { #(i, x) }) |> to_list // -> [#(1, "a"), #(2, "b")] ``` -------------------------------- ### Map a Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Transforms elements of a yielder using a mapping function. ```gleam from_list([1, 2, 3]) |> map(fn(x) { x * 2 }) |> to_list // -> [2, 4, 6] ``` -------------------------------- ### Calculate Yielder length Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Counts the total number of elements in a Yielder by traversing it entirely. ```gleam pub fn length(over yielder: Yielder(e)) -> Int ``` ```gleam empty() |> length // -> 0 ``` ```gleam from_list([1, 2, 3, 4]) |> length // -> 4 ``` -------------------------------- ### Append two yielders Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Appends two yielders into a new one. Evaluation is deferred until the yielder is consumed. ```gleam pub fn append( to first: Yielder(a), suffix second: Yielder(a), ) -> Yielder(a) ``` ```gleam from_list([1, 2]) |> append(from_list([3, 4])) |> to_list // -> [1, 2, 3, 4] ``` -------------------------------- ### try_fold Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html A variant of fold that processes elements and can return an error to stop the folding process early. ```APIDOC ## try_fold ### Signature `pub fn try_fold(over yielder: Yielder(e), from initial: acc, with f: fn(acc, e) -> Result(acc, err)) -> Result(acc, err)` ### Description A variant of fold that might fail. The folding function should return `Result(accumulator, error)`. If the returned value is `Ok(accumulator)` try_fold will try the next value in the yielder. If the returned value is `Error(error)` try_fold will stop and return that error. ``` -------------------------------- ### Interleave two Yielders Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Alternates between two yielders until both are exhausted. ```gleam pub fn interleave( left: Yielder(element), with right: Yielder(element), ) -> Yielder(element) ``` ```gleam from_list([1, 2, 3, 4]) |> interleave(from_list([11, 12, 13, 14])) |> to_list // -> [1, 11, 2, 12, 3, 13, 4, 14] ``` ```gleam from_list([1, 2, 3, 4]) |> interleave(from_list([100])) |> to_list // -> [1, 100, 2, 3, 4] ``` -------------------------------- ### Map2 Yielder Signature Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html The function signature for combining two yielders. ```gleam pub fn map2( yielder1: Yielder(a), yielder2: Yielder(b), with fun: fn(a, b) -> c, ) -> Yielder(c) ``` -------------------------------- ### Define transform Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Signature for the transform function which creates a yielder using a stateful function. ```gleam pub fn transform( over yielder: Yielder(a), from initial: acc, with f: fn(acc, a) -> Step(b, acc), ) -> Yielder(b) ``` -------------------------------- ### fold Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Reduces a yielder of elements into a single value. ```APIDOC ## fold ### Signature `pub fn fold(over yielder: Yielder(e), from initial: acc, with f: fn(acc, e) -> acc) -> acc` ### Description Reduces a yielder of elements into a single value by calling a given function on each element in turn. If called on a yielder of infinite length then this function will never return. ``` -------------------------------- ### Define flat_map function Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Signature for the flat_map function which transforms and flattens a Yielder. ```gleam pub fn flat_map( over yielder: Yielder(a), with f: fn(a) -> Yielder(b), ) -> Yielder(b) ``` -------------------------------- ### filter Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a yielder from an existing yielder and a predicate function, keeping only elements where the predicate returns True. ```APIDOC ## filter(yielder: Yielder(a), keeping predicate: fn(a) -> Bool) ### Description Creates a yielder from an existing yielder and a predicate function. The new yielder will contain elements from the first yielder for which the given function returns `True`. This function does not evaluate the elements of the yielder, the computation is performed when the yielder is later run. ### Example ```gleam from_list([1, 2, 3, 4]) |> filter(int.is_even) |> to_list // -> [2, 4] ``` ``` -------------------------------- ### Fold a Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Reduces a Yielder into a single value. Note that this will not terminate if used on an infinite Yielder. ```gleam pub fn fold( over yielder: Yielder(e), from initial: acc, with f: fn(acc, e) -> acc, ) -> acc ``` ```gleam from_list([1, 2, 3, 4]) |> fold(from: 0, with: fn(acc, element) { element + acc }) // -> 10 ``` -------------------------------- ### last Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Returns the last element in the given yielder. ```APIDOC ## last(yielder: Yielder(element)) -> Result(element, Nil) ### Description Returns the last element in the given yielder. Returns Error(Nil) if the yielder is empty. This function runs in linear time. ### Parameters - **yielder** (Yielder(element)) - The yielder to process. ### Returns - **Result(element, Nil)** - The last element or an error if empty. ``` -------------------------------- ### Filter a Yielder Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Creates a new yielder containing only elements that satisfy the provided predicate. ```gleam pub fn filter( yielder: Yielder(a), keeping predicate: fn(a) -> Bool, ) -> Yielder(a) ``` ```gleam import gleam/int from_list([1, 2, 3, 4]) |> filter(int.is_even) |> to_list // -> [2, 4] ``` -------------------------------- ### flatten Source: https://hexdocs.pm/gleam_yielder/gleam/yielder.html Flattens a yielder of yielders into a single yielder. ```APIDOC ## flatten ### Signature `pub fn flatten(yielder: Yielder(Yielder(a))) -> Yielder(a)` ### Description Flattens a yielder of yielders, creating a new yielder. This function does not evaluate the elements of the yielder, the computation is performed when the yielder is later run. ```