From 180e9514a3169665daa8786be2abbf0594ce2120 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 6fb78ec5..ae8733bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index fb66b890..a4e2b270 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -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, @@ -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), @@ -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.