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 reduce operator #30

Merged
merged 1 commit into from
Oct 30, 2023
Merged

feat: implement reduce operator #30

merged 1 commit into from
Oct 30, 2023

Conversation

geminicaprograms
Copy link
Contributor

@geminicaprograms geminicaprograms commented Oct 27, 2023

Uses the first and the following (if available) elements from this source and applies function f on them. The returned value is used as the next current value and f is applied again with the value received from a source. The operation is repeated until the source is drained. This is similar operation to fold but it uses the first source element as zero e.g.:

  Source.empty[Int].reduce(_ + _)    // throws NoSuchElementException("cannot reduce an empty source")
  Source.fromValues(1).reduce(_ + _) // 1
  val s = Source.fromValues(1, 2)
  s.reduce(_ + _)                    // 3
  s.receive()                        // ChannelClosed.Done

Note that when function f fails then exception is propagated up to the caller.

@adamw adamw changed the title feat: implement fold operator feat: implement reduce operator Oct 30, 2023
@adamw
Copy link
Member

adamw commented Oct 30, 2023

I fixed the title ;)

Uses the first and the following (if available) elements from this source
and applies function `f` on them. The returned value is used as the next
current value and `f` is applied again with the value received from a source.
The operation is repeated until the source is drained.
This is similar operation to `fold` but it uses the first source element as
`zero` e.g.:

  Source.empty[Int].reduce(_ + _)    // throws NoSuchElementException("cannot reduce an empty source")
  Source.fromValues(1).reduce(_ + _) // 1
  val s = Source.fromValues(1, 2)
  s.reduce(_ + _)                    // 3
  s.receive()                        // ChannelClosed.Done

Note that in case when function `f` thrown an exception the it is
propagated up to the caller.
Base automatically changed from feat_fold to master October 30, 2023 13:18
@geminicaprograms geminicaprograms merged commit c7710e2 into master Oct 30, 2023
4 checks passed
@geminicaprograms geminicaprograms deleted the feat_reduce branch October 30, 2023 13:19
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