Skip to content

Commit

Permalink
feat: add difference and symmetricDifference to List
Browse files Browse the repository at this point in the history
  • Loading branch information
brekk committed Apr 19, 2024
1 parent f972ee3 commit 78975b5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
45 changes: 39 additions & 6 deletions prelude/__internal__/List.mad
Original file line number Diff line number Diff line change
Expand Up @@ -783,12 +783,24 @@ export zip = (as, bs) => where(#[as, bs]) {
* @since 0.6.0
*/
includes :: Eq a => a -> List a -> Boolean
export includes = (x, list) => where(list) {
[] =>
false

[a, ...xs] =>
a == x || includes(x, xs)
export includes = (x, list) => {
matched = false
subcheck = (lll) => if (!matched) {
where(lll) {
[] =>
{}

[a, ...xs] =>
do {
if (a == x) do {
matched := true
}
subcheck(xs)
}
}
}
subcheck(list)
return matched
}
// a == x ? true : includes(x, xs)

Expand Down Expand Up @@ -1087,3 +1099,24 @@ export cut = (a, xs) => {
Nothing
}
}
/**
* Finds the set (i.e. no duplicates) of all elements in the first list not contained in the second list.
* @example
* difference([1,2,3,4], [7,6,5,4,3]) // [1,2]
* difference([7,6,5,4,3], [1,2,3,4]) // [7,6,5]
*/
difference :: Eq a => List a -> List a -> List a
export difference = (z, a) => reduce((stack, c) => includes(c, a) ? stack : [...stack, c], [], z)

/**
* Finds the set (i.e. no duplicates) of all elements contained in the first or second list, but not both.
* @example
* symmetricDifference([1,2,3,4], [7,6,5,4,3]) // [1,2,7,6,5]
* symmetricDifference([7,6,5,4,3], [1,2,3,4]) // [7,6,5,1,2]
*/
symmetricDifference :: Eq a => List a -> List a -> List a
export symmetricDifference = (z, a) => reduce(
(stack, c) => includes(c, a) && includes(c, z) ? stack : [...stack, c],
[],
concat(z, a),
)
14 changes: 14 additions & 0 deletions prelude/__internal__/List.spec.mad
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
concat,
contains,
cut,
difference,
drop,
dropLast,
dropWhile,
Expand Down Expand Up @@ -38,6 +39,7 @@ import {
sort,
sortDesc,
startsWith,
symmetricDifference,
tail,
tails,
take,
Expand Down Expand Up @@ -291,3 +293,15 @@ test("all - false", () => assertEquals(all((x) => x > 4, [1, 2, 10]), false))
test("cut", () => assertEquals(cut(5, range(1, 11)), Just(#[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])))
test("cut - negative index", () => assertEquals(cut(-1, range(1, 11)), Nothing))
test("cut - invalid index", () => assertEquals(cut(16, range(1, 11)), Nothing))

test("difference", () => assertEquals(difference([1, 2, 3, 4], [7, 6, 5, 4, 3]), [1, 2]))
test("difference - swap", () => assertEquals(difference([7, 6, 5, 4, 3], [1, 2, 3, 4]), [7, 6, 5]))

test(
"symmetricDifference",
() => assertEquals(symmetricDifference([1, 2, 3, 4], [7, 6, 5, 4, 3]), [1, 2, 7, 6, 5]),
)
test(
"symmetricDifference",
() => assertEquals(symmetricDifference([7, 6, 5, 4, 3], [1, 2, 3, 4]), [7, 6, 5, 1, 2]),
)

0 comments on commit 78975b5

Please sign in to comment.