CombineCommunity / rxswift-to-combine-cheatsheet

RxSwift to Apple’s Combine Cheat Sheet
https://medium.com/gett-engineering/rxswift-to-apples-combine-cheat-sheet-e9ce32b14c5b
MIT License
2.04k stars 125 forks source link

ifEmpty mismatch #6

Closed stephencelis closed 5 years ago

stephencelis commented 5 years ago

Combine's "replaceEmpty(with:)" appears to be "ifEmpty(default:)", but the sheet has them reversed.

Combine seems to have no operator fo "ifEmpty(switchTo:)", though it can be achieved with "replaceEmpty(with: publisher).switchToLatest()"

freak4pc commented 5 years ago

Thanks about the first one - regarding the second one, I guess we could add it, even though it's more like comparing

ifEmpty(default: observable) 
 .switchLatest()

in RxSwift - it's not really a 1v1 operator comparison. What do you think?

freak4pc commented 5 years ago

Ok I resolved both in 3f18df3

stephencelis commented 5 years ago

I think it's useful to show solutions even if they're more verbose. Could use a ⚠️ to distinguish operators that aren't 1:1 mappings. Same as "flatMapLatest" which isn't really "switchToLatest" since there's no transform. You're stuck with something like "map { … return publisher }.switchToLatest()".

freak4pc commented 5 years ago

I've added it in the note instead as a direct mapping - felt better to me. The warning sign thing is also interesting, I'll give it some though :) Thank you!

stephencelis commented 5 years ago

Could also add a note that startWith(value) is merge(with: Publishers.Just(value))

stephencelis commented 5 years ago

And no prob! Thanks for making this resource! Think it'll help a lot of folks.

freak4pc commented 5 years ago

I'm worried about adding all these composition hacks but I guess if we want to provide a good bridge, it should be there.

It would be the same as using prepend with a Just in Combine, but that's more comparable to Observable.concat(.just("value"), stream) then startWith. Not too sure on where to put the line :)

stephencelis commented 5 years ago

Oh neat! Haven't explored enough to see prepend yet. Looks like prepend can be used the same way startWith is used, no? So could be considered a direct map.

freak4pc commented 5 years ago

Actually seems to be :) I hadn't noticed before but prepend has one overaload that takes a variadic array, and one that takes a different stream (like concat).