Skip to content

Commit

Permalink
deprecate list.pop and list.pop_map
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri committed Jan 27, 2025
1 parent 8a68cac commit 180e951
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Fixed a bug that would result in `list.unique` having quadratic runtime.
- The `pop` and `pop_map` functions in the `list` module have been deprecated.

## v0.53.0 - 2025-01-23

Expand Down
23 changes: 16 additions & 7 deletions src/gleam/list.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,7 @@ pub fn key_filter(
/// // -> Error(Nil)
/// ```
///
@deprecated("This function will be removed in the next gleam_stdlib version")
pub fn pop(
in list: List(a),
one_that is_desired: fn(a) -> Bool,
Expand Down Expand Up @@ -1703,6 +1704,7 @@ fn pop_loop(haystack, predicate, checked) {
/// // -> Error(Nil)
/// ```
///
@deprecated("This function will be removed in the next gleam_stdlib version")
pub fn pop_map(
in haystack: List(a),
one_that is_desired: fn(a) -> Result(b, c),
Expand Down Expand Up @@ -1749,13 +1751,20 @@ fn pop_map_loop(
/// ```
///
pub fn key_pop(list: List(#(k, v)), key: k) -> Result(#(v, List(#(k, v))), Nil) {
pop_map(list, fn(entry) {
let #(k, v) = entry
case k {
k if k == key -> Ok(v)
_ -> Error(Nil)
}
})
key_pop_loop(list, key, [])
}

fn key_pop_loop(
list: List(#(k, v)),
key: k,
checked: List(#(k, v)),
) -> Result(#(v, List(#(k, v))), Nil) {
case list {
[] -> Error(Nil)
[#(k, v), ..rest] if k == key ->
Ok(#(v, reverse_and_prepend(checked, rest)))
[first, ..rest] -> key_pop_loop(rest, key, [first, ..checked])
}
}

/// Given a list of 2-element tuples, inserts a key and value into the list.
Expand Down

0 comments on commit 180e951

Please sign in to comment.