From 70a3a8927cd7e927ec96bc30f57377b32b080c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Tue, 9 Apr 2013 23:50:02 +0200 Subject: Scala : week2: implement singletonSet, and union --- Scala/funsets/src/main/scala/funsets/FunSets.scala | 4 +-- .../src/test/scala/funsets/FunSetSuite.scala | 31 +++++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Scala/funsets/src/main/scala/funsets/FunSets.scala b/Scala/funsets/src/main/scala/funsets/FunSets.scala index 594a49d..c6d5ea5 100644 --- a/Scala/funsets/src/main/scala/funsets/FunSets.scala +++ b/Scala/funsets/src/main/scala/funsets/FunSets.scala @@ -20,13 +20,13 @@ object FunSets { /** * Returns the set of the one given element. */ - def singletonSet(elem: Int): Set = ??? + def singletonSet(elem: Int): Set = (x => x == elem) /** * Returns the union of the two given sets, * the sets of all elements that are in either `s` or `t`. */ - def union(s: Set, t: Set): Set = ??? + def union(s: Set, t: Set): Set = (x => s(x) | t(x)) /** * Returns the intersection of the two given sets, diff --git a/Scala/funsets/src/test/scala/funsets/FunSetSuite.scala b/Scala/funsets/src/test/scala/funsets/FunSetSuite.scala index e75ba8c..e61a56c 100644 --- a/Scala/funsets/src/test/scala/funsets/FunSetSuite.scala +++ b/Scala/funsets/src/test/scala/funsets/FunSetSuite.scala @@ -101,7 +101,36 @@ class FunSetSuite extends FunSuite { } } - ignore("union contains all elements") { + test("singletonSet(1)") { + new TestSets { + assert(!contains(s1, -1), "fails with -1") + assert(!contains(s1, 0), "fails with 0") + assert(contains(s1, 1), "fails with 1") + assert(!contains(s1, 2), "fails with 2") + assert(!contains(s1, 3), "fails with 3") + } + } + + test("singletonSet(2)") { + new TestSets { + assert(!contains(s2, -1), "fails with -1") + assert(!contains(s2, 0), "fails with 0") + assert(!contains(s2, 1), "fails with 1") + assert(contains(s2, 2), "fails with 2") + assert(!contains(s2, 3), "fails with 3") + } + } + test("singletonSet(3)") { + new TestSets { + assert(!contains(s3, -1), "fails with -1") + assert(!contains(s3, 0), "fails with 0") + assert(!contains(s3, 1), "fails with 1") + assert(!contains(s3, 2), "fails with 2") + assert(contains(s3, 3), "fails with 3") + } + } + + test("union contains all elements") { new TestSets { val s = union(s1, s2) assert(contains(s, 1), "Union 1") -- cgit v1.1-2-g2b99