Skip to content

Adds Publisher.prefix(while:behavior:).#70

Merged
freak4pc merged 2 commits intoCombineCommunity:mainfrom
jasdev:prefix-inclusive
Jul 23, 2022
Merged

Adds Publisher.prefix(while:behavior:).#70
freak4pc merged 2 commits intoCombineCommunity:mainfrom
jasdev:prefix-inclusive

Conversation

@jasdev
Copy link
Member

@jasdev jasdev commented Dec 30, 2020

Figured we should add this to Ext after stumbling upon @jessegrosjean’s Swift Forums thread and @mayoff’s approach with composed operators.

@codecov
Copy link

codecov bot commented Dec 30, 2020

Codecov Report

Merging #70 (9d7032e) into main (730d272) will increase coverage by 0.07%.
The diff coverage is 99.23%.

❗ Current head 9d7032e differs from pull request most recent head 05d483e. Consider uploading reports for the commit 05d483e to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##             main      #70      +/-   ##
==========================================
+ Coverage   97.12%   97.20%   +0.07%     
==========================================
  Files          62       64       +2     
  Lines        3336     3467     +131     
==========================================
+ Hits         3240     3370     +130     
- Misses         96       97       +1     
Impacted Files Coverage Δ
Sources/Operators/PrefixWhileBehavior.swift 96.00% <96.00%> (ø)
Tests/PrefixWhileBehaviorTests.swift 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 730d272...05d483e. Read the comment docs.

@mayoff
Copy link

mayoff commented Mar 6, 2021

The goal is an operator that passes through all of the upstream values, until and including the first value that fails the predicate.

In the linked Swift forums thread, I implemented the requested operator as follows:

  • Turn each upstream value into a publisher that emits the value (in a wrapper), followed by an end-marker if the value fails the predicate. In my original version, I used Sequence.Publisher. In @jasdev's code, the Just ... append ... setFailureType expression produces these short publishers.

  • Use flatMap to flatten those short publishers into one publisher that republishes each upstream value, with an end-marker after each upstream value that fails the predicate.

  • Use prefix(while:) to terminate the stream at the first end-marker.

  • Use compactMap to unwrap the values.

In my original code, the wrappers and the end-marker were cases of enum Cut. Here, @jasdev has renamed enum Cut to enum PrefixInclusiveEvent.

@jasdev
Copy link
Member Author

jasdev commented Jul 15, 2022

(Cleaning up some ol’ PRs — @freak4pc any chance we could land this in the next release whenever you get a chance to review? 🙏🏽)

Copy link
Member

@freak4pc freak4pc left a comment

Choose a reason for hiding this comment

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

Sorry @jasdev & @mayoff,
Barely had any time lately and this totally slipped my mind. Thanks for the ping (and explanation), looks great.

@freak4pc freak4pc merged commit ca170dd into CombineCommunity:main Jul 23, 2022
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