From c5f1113df8159078ac3bfb6fbe8426db72248512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 10 Apr 2013 00:03:54 +0200 Subject: Scala : week2: implement intersect and diff --- Scala/funsets/src/main/scala/funsets/FunSets.scala | 4 ++-- .../src/test/scala/funsets/FunSetSuite.scala | 24 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Scala/funsets/src/main/scala/funsets/FunSets.scala b/Scala/funsets/src/main/scala/funsets/FunSets.scala index c6d5ea5..1fd2d31 100644 --- a/Scala/funsets/src/main/scala/funsets/FunSets.scala +++ b/Scala/funsets/src/main/scala/funsets/FunSets.scala @@ -32,13 +32,13 @@ object FunSets { * Returns the intersection of the two given sets, * the set of all elements that are both in `s` and `t`. */ - def intersect(s: Set, t: Set): Set = ??? + def intersect(s: Set, t: Set): Set = (x => s(x) & t(x)) /** * Returns the difference of the two given sets, * the set of all elements of `s` that are not in `t`. */ - def diff(s: Set, t: Set): Set = ??? + def diff(s: Set, t: Set): Set = (x => s(x) & !t(x)) /** * Returns the subset of `s` for which `p` holds. diff --git a/Scala/funsets/src/test/scala/funsets/FunSetSuite.scala b/Scala/funsets/src/test/scala/funsets/FunSetSuite.scala index e61a56c..88dac91 100644 --- a/Scala/funsets/src/test/scala/funsets/FunSetSuite.scala +++ b/Scala/funsets/src/test/scala/funsets/FunSetSuite.scala @@ -138,4 +138,28 @@ class FunSetSuite extends FunSuite { assert(!contains(s, 3), "Union 3") } } + + test("intersect") { + new TestSets { + val s = intersect(s1, s2) + assert(!contains(s, 1), "Intersect none") + assert(!contains(s, 2), "Intersect none") + + val t = intersect(s1, s1) + assert(contains(t, 1), "Intersect 1") + + val v = intersect(s2, s2) + assert(contains(v, 2), "Intersect 2") + } + } + + test("diff") { + new TestSets { + val s = diff(s1, s2) + assert(!contains(s, 0), "Diff none") + assert(contains(s, 1), "Diff 1") + assert(!contains(s, 2), "Diff none") + assert(!contains(s, 3), "Diff none") + } + } } -- cgit v1.1-2-g2b99