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

OR-5270 Operators:: Sequence Operators:: Querying values:: reduce(_:_:) #124

Merged
merged 1 commit into from
Aug 6, 2023

Conversation

crazymanish
Copy link
Owner

Context

Operators are publishers

  • In Combine, methods that perform an operation on values coming from a publisher are called operators.
  • Each Combine operator actually returns a publisher. Generally speaking, that publisher receives the upstream values, manipulates the data, and then sends that data downstream.

Sequence operators

  • Sequence operators are easiest to understand when you realize that publishers are just sequences themselves.
  • Sequence operators work with the collection of a publisher’s values, much like an array or set — which, of course, are just finite sequences!

Querying values

  • The following operators also deal with the entire set of values emitted by a publisher, but they don't produce any specific value that it emits.
  • Instead, these operators emit a different value representing some query on the publisher as a whole. A good example of this is the count operator.

In this PR

  • Operators:: Sequence Operators:: Finding values:: reduce(_:_:)
  • reduce(_:_:) Applies a closure that collects each element of a stream and publishes a final result upon completion.
  • Param: initialResult:: The value that the closure receives the first time it’s called.
  • Param: nextPartialResult:: A closure that produces a new value by taking the previously-accumulated value and the next element it receives from the upstream publisher.
  • Use reduce(::) to collect a stream of elements and produce an accumulated value based on a closure you provide.
  • https://developer.apple.com/documentation/combine/publishers/reduce/reduce(_:_:)

@crazymanish crazymanish requested review from buh and raihan August 6, 2023 18:15
@crazymanish crazymanish merged commit ea3a73d into main Aug 6, 2023
@crazymanish crazymanish deleted the OR-5270-p2 branch August 6, 2023 18:16
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.

[w25(18june-24june)] Operators:: Sequence Operators: Query the publisher
1 participant