From e2c8e84d0243f6b672f21c8b6281cbef61b8b760 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 10 Apr 2013 00:08:19 +0200 Subject: Scala : week2: implement filter --- Scala/funsets/src/main/scala/funsets/FunSets.scala | 2 +- Scala/funsets/src/test/scala/funsets/FunSetSuite.scala | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Scala/funsets/src/main/scala/funsets/FunSets.scala b/Scala/funsets/src/main/scala/funsets/FunSets.scala index 1fd2d31..8b7778d 100644 --- a/Scala/funsets/src/main/scala/funsets/FunSets.scala +++ b/Scala/funsets/src/main/scala/funsets/FunSets.scala @@ -43,7 +43,7 @@ object FunSets { /** * Returns the subset of `s` for which `p` holds. */ - def filter(s: Set, p: Int => Boolean): Set = ??? + def filter(s: Set, p: Int => Boolean): Set = (x => s(x) & p(x)) /** * The bounds for `forall` and `exists` are +/- 1000. diff --git a/Scala/funsets/src/test/scala/funsets/FunSetSuite.scala b/Scala/funsets/src/test/scala/funsets/FunSetSuite.scala index 88dac91..7c3fd53 100644 --- a/Scala/funsets/src/test/scala/funsets/FunSetSuite.scala +++ b/Scala/funsets/src/test/scala/funsets/FunSetSuite.scala @@ -162,4 +162,19 @@ class FunSetSuite extends FunSuite { assert(!contains(s, 3), "Diff none") } } + + test("filter") { + new TestSets { + val s = filter(s1, (x => x == 1)) + assert(!contains(s, 0), "Filter none") + assert(contains(s, 1), "Filter 1") + assert(!contains(s, 2), "Filter none") + assert(!contains(s, 3), "Filter none") + val v = filter(s1, (x => x == 0)) + assert(!contains(v, 0), "Filter none") + assert(!contains(v, 1), "Filter none") + assert(!contains(v, 2), "Filter none") + assert(!contains(v, 3), "Filter none") + } + } } -- cgit v1.1-2-g2b99