-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Streaming and StreamingT dropWhile functions #856
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -222,10 +222,12 @@ sealed abstract class StreamingT[F[_], A] extends Product with Serializable { lh | |
* If no elements satisfy `f`, an empty stream will be returned. | ||
* | ||
* For example: | ||
* | ||
* StreamingT[List, Int](1, 2, 3, 4, 5, 6, 7).takeWhile(n => n != 4) | ||
* | ||
* Will result in: StreamingT[List, Int](1, 2, 3) | ||
* {{{ | ||
* scala> import cats.std.list._ | ||
* scala> val s = StreamingT[List, Int](1, 2, 3, 4, 5, 6, 7) | ||
* scala> s.takeWhile(n => n != 4).toList.flatten | ||
* res0: List[Int] = List(1, 2, 3) | ||
* }}} | ||
*/ | ||
def takeWhile(f: A => Boolean)(implicit ev: Functor[F]): StreamingT[F, A] = | ||
this match { | ||
|
@@ -244,14 +246,16 @@ sealed abstract class StreamingT[F[_], A] extends Product with Serializable { lh | |
* If no elements satisfy `f`, the current stream will be returned. | ||
* | ||
* For example: | ||
* | ||
* StreamingT[List, Int](1, 2, 3, 4, 5, 6, 7).dropWhile(n => n != 4) | ||
* | ||
* Will result in: StreamingT[List, Int](4, 5, 6, 7) | ||
* {{{ | ||
* scala> import cats.std.list._ | ||
* scala> val s = StreamingT[List, Int](1, 2, 3, 4, 5, 6, 7) | ||
* scala> s.dropWhile(n => n != 4).toList.flatten | ||
* res0: List[Int] = List(4, 5, 6, 7) | ||
* }}} | ||
*/ | ||
def dropWhile(f: A => Boolean)(implicit ev: Functor[F]): StreamingT[F, A] = | ||
this match { | ||
case Cons(a, ft) => if (f(a)) Empty() else Cons(a, ft.map(_.dropWhile(f))) | ||
case Cons(a, ft) => if (f(a)) Wait(ft.map(_.dropWhile(f))) else Cons(a, ft) | ||
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. Same comment as https://github.com/typelevel/cats/pull/856/files#r51714784 |
||
case Wait(ft) => Wait(ft.map(_.dropWhile(f))) | ||
case Empty() => Empty() | ||
} | ||
|
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
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.
I think we can save an allocation by doing something like this here:
case s @ Cons(a, lt) => if (f(a)) Wait(lt.map(_.dropWhile(f))) else s
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.
Good spot - Thanks! - In general, is there any reason to do
s @
and evaluate the expression tos
instead of just evaluating tothis
? Aesthetically I prefer usingthis
, but that is just personal preference - was wondering if there is any real reason I not aware of to prefer one of the other. Eitherways, I'll push through with thes @
; because the only reason I have any preference for usingthis
is aesthetic, and that likely comes from Java/C++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.
To answer your question: there are times when you get a finer-grained type from
s @ ...
(due to the pattern match...
) and then you would need to uses
instead ofthis
because you have better type information. If you don't need the extra type information then usingthis
would be just fine; at that point it's just a stylistic preference.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.
Cool - that makes a lot of sense - and thanks a million for following up!!!
I guess in this instance there would have been no value, as there is no additional type information required, but I can definitely imagine scenarios, where this could be valuable.
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.
Yep, totally agree with what @non said. It didn't occur to me that
this
would work fine in this example. I have no strong preference.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.
Thanks! - I actually don't have much preference - it's more natural defaults :-). I went with your suggestion anyway.