From 84779ed7c16652860ec0170d08446b86d3c5cf36 Mon Sep 17 00:00:00 2001 From: Giacomo Cavalieri Date: Mon, 27 Jan 2025 17:07:18 +0100 Subject: [PATCH] deprecate list.pop and list.pop_map --- CHANGELOG.md | 1 + src/gleam/list.gleam | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4982cd24..efdce7a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index 2dc9512a..c0ce0a07 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -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, @@ -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), @@ -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.