Skip to content
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

Merged
merged 6 commits into from
Oct 30, 2023
Merged

feat: implement fold operator #27

merged 6 commits into from
Oct 30, 2023

Conversation

geminicaprograms
Copy link
Contributor

@geminicaprograms geminicaprograms commented Oct 26, 2023

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:

  • the original exception is re-thrown
  • NoSuchElement exception is thrown when source fails without error
  • when function f fails with exception then this exception is propagated up to the caller

@geminicaprograms geminicaprograms force-pushed the feat_fold branch 3 times, most recently from 13f8b50 to 4bef471 Compare October 26, 2023 17:11
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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 {
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

geminicaprograms added a commit that referenced this pull request Oct 30, 2023
As mentioned in #27.
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.
geminicaprograms added a commit that referenced this pull request Oct 30, 2023
….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
Base automatically changed from chore_improvements to master October 30, 2023 13:18
@geminicaprograms geminicaprograms merged commit b6f2912 into master Oct 30, 2023
@geminicaprograms geminicaprograms deleted the feat_fold branch October 30, 2023 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants