diff --git a/prelude/__internal__/List.mad b/prelude/__internal__/List.mad index b60c8728..f1c48153 100644 --- a/prelude/__internal__/List.mad +++ b/prelude/__internal__/List.mad @@ -1105,3 +1105,33 @@ export symmetricDifference = (z, a) => reduce( [], concat(z, a), ) + +/** + * Drop all instances of a given value from a list + * @since 0.24.2 + * @example + * dropEq("a", ["b", "a", "n", "a", "n", "a"]) // ["b", "n", "n"] + */ +dropEq :: Eq a => a -> List a -> List a +export dropEq = (needle, haystack) => where(haystack) { + [a, ...z] => + a == needle ? dropEq(needle, z) : [a, ...dropEq(needle, z)] + + _ => + [] +} + +/** + * Drop all instances of multiple given values from a list + * @since 0.24.2 + * @example + * dropEq([0, 2, 4, 6, 8], range(0, 10)) // [1, 3, 5, 7, 9] + */ +without :: Eq a => List a -> List a -> List a +export without = (needles, haystack) => where(haystack) { + [a, ...z] => + includes(a, needles) ? without(needles, z) : [a, ...without(needles, z)] + + _ => + [] +} diff --git a/prelude/__internal__/List.spec.mad b/prelude/__internal__/List.spec.mad index 08becd74..42507b1d 100644 --- a/prelude/__internal__/List.spec.mad +++ b/prelude/__internal__/List.spec.mad @@ -12,6 +12,7 @@ import { cut, difference, drop, + dropEq, dropLast, dropWhile, endsWith, @@ -46,6 +47,7 @@ import { takeLast, takeWhile, uniqueBy, + without, } from "./List" @@ -305,3 +307,10 @@ test( "symmetricDifference", () => assertEquals(symmetricDifference([7, 6, 5, 4, 3], [1, 2, 3, 4]), [7, 6, 5, 1, 2]), ) + +test( + "dropEq", + () => assertEquals(dropEq("a", ["b", "a", "n", "a", "n", "a", "s"]), ["b", "n", "n", "s"]), +) + +test("without", () => assertEquals(without([0, 2, 4, 6, 8], range(0, 10)), [1, 3, 5, 7, 9]))