diff options
-rw-r--r-- | Scala/funsets/src/main/scala/funsets/FunSets.scala | 2 | ||||
-rw-r--r-- | Scala/funsets/src/test/scala/funsets/FunSetSuite.scala | 15 |
2 files changed, 16 insertions, 1 deletions
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") + } + } } |