This repository was archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
Remove dropRight and takeRight from Iterator #150
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -113,14 +113,6 @@ object View extends IterableFactory[View] { | |
| if (underlying.knownSize >= 0) (underlying.knownSize - normN) max 0 else -1 | ||
| } | ||
|
|
||
| /** A view that drops trailing elements of the underlying collection. */ | ||
| case class DropRight[A](underlying: Iterable[A], n: Int) extends View[A] { | ||
| def iterator() = underlying.iterator().dropRight(n) | ||
| protected val normN = n max 0 | ||
| override def knownSize = | ||
| if (underlying.knownSize >= 0) (underlying.knownSize - normN) max 0 else -1 | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed the |
||
|
|
||
| case class DropWhile[A](underlying: Iterable[A], p: A => Boolean) extends View[A] { | ||
| def iterator() = underlying.iterator().dropWhile(p) | ||
| } | ||
|
|
@@ -133,14 +125,6 @@ object View extends IterableFactory[View] { | |
| if (underlying.knownSize >= 0) underlying.knownSize min normN else -1 | ||
| } | ||
|
|
||
| /** A view that takes trailing elements of the underlying collection. */ | ||
| case class TakeRight[A](underlying: Iterable[A], n: Int) extends View[A] { | ||
| def iterator() = underlying.iterator().takeRight(n) | ||
| protected val normN = n max 0 | ||
| override def knownSize = | ||
| if (underlying.knownSize >= 0) underlying.knownSize min normN else -1 | ||
| } | ||
|
|
||
| case class TakeWhile[A](underlying: Iterable[A], p: A => Boolean) extends View[A] { | ||
| def iterator(): Iterator[A] = underlying.iterator().takeWhile(p) | ||
| } | ||
|
|
@@ -269,6 +253,8 @@ trait IndexedView[+A] extends View[A] with ArrayLike[A] { self => | |
| } | ||
| } | ||
|
|
||
| override def knownSize: Int = length | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That’s unrelated to the PR, actually, but I thought we could override |
||
|
|
||
| override def take(n: Int): IndexedView[A] = new IndexedView.Take(this, n) | ||
| override def takeRight(n: Int): IndexedView[A] = new IndexedView.TakeRight(this, n) | ||
| override def drop(n: Int): IndexedView[A] = new IndexedView.Drop(this, n) | ||
|
|
@@ -286,11 +272,10 @@ object IndexedView { | |
| def apply(i: Int) = underlying.apply(i) | ||
| } | ||
|
|
||
| class TakeRight[A](underlying: IndexedView[A], n: Int) | ||
| extends View.TakeRight(underlying, n) with IndexedView[A] { | ||
| override def iterator() = super.iterator() // needed to avoid "conflicting overrides" error | ||
| def length = underlying.length min normN | ||
| def apply(i: Int) = underlying.apply(i) | ||
| class TakeRight[A](underlying: IndexedView[A], n: Int) extends IndexedView[A] { | ||
| private[this] val delta = (underlying.length - (n max 0)) max 0 | ||
| def length = underlying.length - delta | ||
| def apply(i: Int) = underlying.apply(i + delta) | ||
| } | ||
|
|
||
| class Drop[A](underlying: IndexedView[A], n: Int) | ||
|
|
@@ -300,16 +285,13 @@ object IndexedView { | |
| def apply(i: Int) = underlying.apply(i + normN) | ||
| } | ||
|
|
||
| class DropRight[A](underlying: IndexedView[A], n: Int) | ||
| extends View.DropRight(underlying, n) with IndexedView[A] { | ||
| override def iterator() = super.iterator() | ||
| def length = (underlying.length - normN) max 0 | ||
| def apply(i: Int) = underlying.apply(i + normN) | ||
| class DropRight[A](underlying: IndexedView[A], n: Int) extends IndexedView[A] { | ||
| private[this] val len = (underlying.length - (n max 0)) max 0 | ||
| def length = len | ||
| def apply(i: Int) = underlying.apply(i) | ||
| } | ||
|
|
||
| class Map[A, B](underlying: IndexedView[A], f: A => B) | ||
| extends View.Map(underlying, f) with IndexedView[B] { | ||
| override def iterator() = super.iterator() | ||
| class Map[A, B](underlying: IndexedView[A], f: A => B) extends IndexedView[B] { | ||
| def length = underlying.length | ||
| def apply(n: Int) = f(underlying.apply(n)) | ||
| } | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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
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 is a cute algorithm, but I can't imagine it's faster than just going
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.
That would have the benefit of requiring only one traversal, indeed (excepted in the case of linear collections). I should give it a try.
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.
I tried it but didn’t notice a difference (in
List):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.
Wait, what? List shouldn't rebuild on
takeRightanyway--if it does, that needs to be fixed. Also, I didn't suggest the other method is faster, just more obviously correct.Uh oh!
There was an error while loading. Please reload this page.
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.
Indeed good point, that’s anyway something that we should fix… (#162 keeps track of that)