Skip to content

Commit

Permalink
reintroduce iterator functions with @deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
miampf committed Nov 30, 2024
1 parent 9e18535 commit 5d2b309
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion src/glitzer/progress.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
/// }
/// ```
import gleam/io
import gleam/iterator.{type Iterator}
import gleam/option.{type Option}
import gleam/string
import gleam/string_tree.{type StringTree}
Expand Down Expand Up @@ -441,7 +442,71 @@ fn get_finished_fill(fill: StringTree, bar: ProgressStyle) -> StringTree {
}
}

/// Map an yielder to a function with a bar that ticks every run of the
/// Map an iterator to a function with a bar that ticks every run of the
/// function.
///
/// <details>
/// <summary>Example:</summary>
///
/// ```gleam
/// import glitzer/progress
///
/// fn example(bar) {
/// iterator.range(0, 100)
/// |> progress.map_iterator(fn(bar, element) {
/// progress.print_bar(bar)
/// // do some heavy calculations here >:)
/// })
/// }
/// ```
///
/// </details>
@deprecated("`map_iterator` was deprecated and will be removed in the next release. Use `map_yielder` instead.")
pub fn map_iterator(
over i: Iterator(a),
bar bar: ProgressStyle,
with fun: fn(ProgressStyle, a) -> b,
) -> Iterator(b) {
iterator.index(i)
|> iterator.map(fn(pair) {
let #(el, i) = pair
tick_bar_by_i(bar, i)
|> fun(el)
})
}

@deprecated("`map2_iterator` was deprecated and will be removed in the next release. Use `map2_yielder` instead.")
pub fn map2_iterator(
iterator1 i1: Iterator(a),
iterator2 i2: Iterator(b),
bar bar: ProgressStyle,
with fun: fn(ProgressStyle, a, b) -> c,
) -> Iterator(c) {
iterator.zip(i1, i2)
|> iterator.index
|> iterator.map(fn(pair) {
let #(pair, i) = pair
let #(el1, el2) = pair
tick_bar_by_i(bar, i)
|> fun(el1, el2)
})
}

@deprecated("`each_iterator` was deprecated and will be removed in the next release. Use `each_yielder` instead.")
pub fn each_iterator(
over i: Iterator(a),
bar bar: ProgressStyle,
with fun: fn(ProgressStyle, a) -> b,
) -> Nil {
iterator.index(i)
|> iterator.each(fn(pair) {
let #(el, i) = pair
tick_bar_by_i(bar, i)
|> fun(el)
})
}

/// Map a yielder to a function with a bar that ticks every run of the
/// function.
///
/// <details>
Expand Down

0 comments on commit 5d2b309

Please sign in to comment.