diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-04-10 00:08:19 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-10 18:03:23 +0100 |
commit | e2c8e84d0243f6b672f21c8b6281cbef61b8b760 (patch) | |
tree | 6fb0b528435c7b399e65cfbfb651be7fc5baac10 /Scala | |
parent | c5f1113df8159078ac3bfb6fbe8426db72248512 (diff) | |
download | coursera-e2c8e84d0243f6b672f21c8b6281cbef61b8b760.zip coursera-e2c8e84d0243f6b672f21c8b6281cbef61b8b760.tar.gz |
Scala : week2: implement filter
Diffstat (limited to 'Scala')
-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") + } + } } |