-
Notifications
You must be signed in to change notification settings - Fork 29
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
feat: implement fold
operator
#27
Conversation
13f8b50
to
4bef471
Compare
4bef471
to
5b42d1b
Compare
5b42d1b
to
c67e46c
Compare
abef2ed
to
d60ce81
Compare
e77c39d
to
2740fe7
Compare
2e33ffc
to
7a1086d
Compare
2740fe7
to
1529ba7
Compare
@@ -623,6 +623,42 @@ trait SourceOps[+T] { this: Source[T] => | |||
* }}} | |||
*/ | |||
def last(): T = lastOption().getOrElse(throw new NoSuchElementException("cannot obtain last from an empty source")) | |||
|
|||
/** Uses `zero` as the current value and applies function `f` on it and a value received from a source. The returned value is used as the |
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.
suggestion: Please see the scaladoc-related comments for last
/lastOption
:
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.
Done
import org.scalatest.matchers.should.Matchers | ||
import ox.* | ||
|
||
class SourceOpsFoldTest extends AnyFlatSpec with Matchers { |
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.
thought: Can we have another test case for a scenario where the function passed to fold
throws an exception?
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.
Done
7a1086d
to
856823e
Compare
1529ba7
to
ba005a8
Compare
The `scope` should be only used in internals.
The `ChannelClosed.Error` offers convenient `toThrowable` function. It should be favoured over the new functionality development.
As mentioned in #27.
The `fold` operation returns combined value retrieved from running function `f` on all source elements in a cumulative manner where result of the previous call is used as an input value to the next e.g.: Source.empty[Int].fold(0)((acc, n) => acc + n) // 0 Source.fromValues(2, 3).fold(5)((acc, n) => acc - n) // 0 Note that in case when `receive()` operation fails then ChannelClosedException.Error exception is thrown. Wheres in case when function `f` throws then this exception is propagated up to the caller.
856823e
to
4c456aa
Compare
ba005a8
to
775f8db
Compare
….scala` (#28) The following comments were applied: * use supervised instead of scoped * use toThrowable instead of ad-hoc exception creation * don't catch exceptions in headOption * also fixed comments about the source and receive function
The
fold
operation returns combined value retrieved from running functionf
on all source elements in a cumulative manner where result of the previous call is used as an input value to the next e.g.:Note that in case when
receive()
operation fails then:NoSuchElement
exception is thrown when source fails without errorf
fails with exception then this exception is propagated up to the caller