Skip to content

Commit 1d327dd

Browse files
giacomocavalierilpil
authored andcommitted
deprecate list.pop and list.pop_map
1 parent bb1360d commit 1d327dd

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Fixed a bug that would result in `list.unique` having quadratic runtime.
66
- Fixed the implementation of `list.key_set` to be tail recursive.
7+
- The `pop` and `pop_map` functions in the `list` module have been deprecated.
78

89
## v0.53.0 - 2025-01-23
910

src/gleam/list.gleam

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,7 @@ pub fn key_filter(
16571657
/// // -> Error(Nil)
16581658
/// ```
16591659
///
1660+
@deprecated("This function will be removed in the next gleam_stdlib version")
16601661
pub fn pop(
16611662
in list: List(a),
16621663
one_that is_desired: fn(a) -> Bool,
@@ -1697,6 +1698,7 @@ fn pop_loop(haystack, predicate, checked) {
16971698
/// // -> Error(Nil)
16981699
/// ```
16991700
///
1701+
@deprecated("This function will be removed in the next gleam_stdlib version")
17001702
pub fn pop_map(
17011703
in haystack: List(a),
17021704
one_that is_desired: fn(a) -> Result(b, c),
@@ -1743,13 +1745,20 @@ fn pop_map_loop(
17431745
/// ```
17441746
///
17451747
pub fn key_pop(list: List(#(k, v)), key: k) -> Result(#(v, List(#(k, v))), Nil) {
1746-
pop_map(list, fn(entry) {
1747-
let #(k, v) = entry
1748-
case k == key {
1749-
True -> Ok(v)
1750-
False -> Error(Nil)
1751-
}
1752-
})
1748+
key_pop_loop(list, key, [])
1749+
}
1750+
1751+
fn key_pop_loop(
1752+
list: List(#(k, v)),
1753+
key: k,
1754+
checked: List(#(k, v)),
1755+
) -> Result(#(v, List(#(k, v))), Nil) {
1756+
case list {
1757+
[] -> Error(Nil)
1758+
[#(k, v), ..rest] if k == key ->
1759+
Ok(#(v, reverse_and_prepend(checked, rest)))
1760+
[first, ..rest] -> key_pop_loop(rest, key, [first, ..checked])
1761+
}
17531762
}
17541763

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

0 commit comments

Comments
 (0)