This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
Add withFilter #165
Merged
Merged
Add withFilter #165
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package strawman | ||
package collection | ||
|
||
import scala.{Boolean, Ordering, Unit} | ||
|
||
/** A template trait that contains just the `map`, `flatMap`, `foreach` and `withFilter` methods | ||
* of trait `Iterable`. | ||
* | ||
* @tparam A Element type (e.g. `Int`) | ||
* @tparam CC Collection type constructor (e.g. `List`) | ||
*/ | ||
class WithFilter[+A, +CC[_]](filtered: View[A], factory: IterableFactory[CC]) { | ||
|
||
def map[B](f: A => B): CC[B] = factory.fromIterable(View.Map(filtered, f)) | ||
|
||
def flatMap[B](f: A => IterableOnce[B]): CC[B] = factory.fromIterable(View.FlatMap(filtered, f)) | ||
|
||
def foreach[U](f: A => U): Unit = filtered.foreach(f) | ||
|
||
def withFilter(p: A => Boolean): WithFilter[A, CC] = new WithFilter(filtered.filter(p), factory) | ||
|
||
} | ||
|
||
class SortedWithFilter[+A, +CC1[_], +CC2[X] <: Iterable[X]]( | ||
filtered: View[A], | ||
iterableFactory: IterableFactory[CC1], | ||
sortedFactory: SortedIterableFactory[CC2] | ||
) extends WithFilter[A, CC1](filtered, iterableFactory) { | ||
|
||
def map[B : Ordering](f: A => B): CC2[B] = sortedFactory.sortedFromIterable(View.Map(filtered, f)) | ||
|
||
def flatMap[B : Ordering](f: A => IterableOnce[B]): CC2[B] = sortedFactory.sortedFromIterable(View.FlatMap(filtered, f)) | ||
|
||
override def withFilter(p: A => Boolean): SortedWithFilter[A, CC1, CC2] = | ||
new SortedWithFilter(filtered.filter(p), iterableFactory, sortedFactory) | ||
|
||
} | ||
|
||
class MapWithFilter[K, +V, +CC1[_], +CC2[X, Y] <: Map[X, Y]]( | ||
filtered: View[(K, V)], | ||
iterableFactory: IterableFactory[CC1], | ||
mapFactory: MapFactory[CC2] | ||
) extends WithFilter[(K, V), CC1](filtered, iterableFactory) { | ||
|
||
def map[K2, V2](f: ((K, V)) => (K2, V2)): CC2[K2, V2] = mapFactory.fromIterable(View.Map(filtered, f)) | ||
|
||
def flatMap[K2, V2](f: ((K, V)) => IterableOnce[(K2, V2)]): CC2[K2, V2] = mapFactory.fromIterable(View.FlatMap(filtered, f)) | ||
|
||
override def withFilter(p: ((K, V)) => Boolean): MapWithFilter[K, V, CC1, CC2] = | ||
new MapWithFilter(filtered.filter(p), iterableFactory, mapFactory) | ||
|
||
} | ||
|
||
class SortedMapWithFilter[K, +V, +CC1[_], +CC2[X, Y] <: Map[X, Y], +CC3[_, _]]( | ||
filtered: View[(K, V)], | ||
iterableFactory: IterableFactory[CC1], | ||
mapFactory: MapFactory[CC2], | ||
sortedMapFactory: SortedMapFactory[CC3] | ||
) extends MapWithFilter[K, V, CC1, CC2](filtered, iterableFactory, mapFactory) { | ||
|
||
def map[K2 : Ordering, V2](f: ((K, V)) => (K2, V2)): CC3[K2, V2] = | ||
sortedMapFactory.sortedFromIterable(View.Map(filtered, f)) | ||
|
||
def flatMap[K2 : Ordering, V2](f: ((K, V)) => IterableOnce[(K2, V2)]): CC3[K2, V2] = | ||
sortedMapFactory.sortedFromIterable(View.FlatMap(filtered, f)) | ||
|
||
override def withFilter(p: ((K, V)) => Boolean): SortedMapWithFilter[K, V, CC1, CC2, CC3] = | ||
new SortedMapWithFilter(filtered.filter(p), iterableFactory, mapFactory, sortedMapFactory) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/test/scala/strawman/collection/test/WithFilterTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package strawman | ||
package collection | ||
package test | ||
|
||
import immutable.{List, TreeSet, TreeMap} | ||
|
||
import scala.{Char, Int, Unit} | ||
import scala.Predef.{assert, ArrowAssoc} | ||
|
||
import org.junit.Test | ||
|
||
class WithFilterTest { | ||
|
||
@Test | ||
def iterables(): Unit = { | ||
val xs1 = | ||
for { | ||
x <- List(1, 2, 3) | ||
if x % 2 == 0 | ||
} yield x + 1 | ||
val xs1t: List[Int] = xs1 | ||
assert(xs1 == List(3)) | ||
} | ||
|
||
@Test | ||
def maps(): Unit = { | ||
val xs1 = | ||
for { | ||
(k, v) <- Map(1 -> 'a', 2 -> 'b', 3 -> 'c') | ||
if k % 2 == 0 | ||
} yield (v, k) | ||
val xs1t: Map[Char, Int] = xs1 | ||
assert(xs1 == Map('b' -> 2)) | ||
} | ||
|
||
@Test | ||
def sorted(): Unit = { | ||
val xs1 = | ||
for { | ||
x <- TreeSet(1, 2, 3) | ||
if x % 2 == 0 | ||
} yield x + 1 | ||
val xs1t: TreeSet[Int] = xs1 | ||
assert(xs1 == TreeSet(3)) | ||
} | ||
|
||
@Test | ||
def sortedMap(): Unit = { | ||
val xs1 = | ||
for { | ||
(k, v) <- TreeMap(1 -> 'a', 2 -> 'b', 3 -> 'c') | ||
if k % 2 == 0 | ||
} yield (v, k) | ||
val xs1t: TreeMap[Char, Int] = xs1 | ||
assert(xs1 == TreeMap('b' -> 2)) | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test fails on Dotty. This is probably something related to hashcodes. We observed a weird behavior here too. I’ll create an issue in Dotty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Dotty
xs1 == TreeSet()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually that’s not related to hashcodes, but maybe to inlining in Dotty. I added a commit that un-inlines some methods and now the tests pass on Dotty. I’m trying to minimize the problem.