Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate list.pop and list.pop_map #795

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fixed a bug that would result in `list.unique` having quadratic runtime.
- Fixed the implementation of `list.key_set` to be tail recursive.
- 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 @@ -1657,6 +1657,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 @@ -1697,6 +1698,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 @@ -1743,13 +1745,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 == key {
True -> Ok(v)
False -> 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