Skip to content

Commit

Permalink
feat: added difference and symmetricDifference and union
Browse files Browse the repository at this point in the history
  • Loading branch information
brekk committed Apr 3, 2024
1 parent 2abe4d3 commit c037022
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
33 changes: 33 additions & 0 deletions prelude/__internal__/List.mad
Original file line number Diff line number Diff line change
Expand Up @@ -1085,3 +1085,36 @@ 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),
)

/**
* Combines two lists into a set (i.e. no duplicates) composed of the elements of each list
* @example
* union([1, 2, 3], [2, 3, 4]) // [1, 2, 3, 4]
*/
union :: Eq a => List a -> List a -> List a
export union = (a, z) => reduce(
(stack, c) => includes(c, stack) ? stack : [...stack, c],
[],
concat(a, z),
)
17 changes: 17 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,11 +39,13 @@ import {
sort,
sortDesc,
startsWith,
symmetricDifference,
tail,
tails,
take,
takeLast,
takeWhile,
union,
uniqueBy,
} from "./List"

Expand Down Expand Up @@ -288,3 +291,17 @@ 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]),
)

test("union", () => assertEquals(union(range(1, 11), range(11, 21)), range(1, 21)))

0 comments on commit c037022

Please sign in to comment.