-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add no-argument form of combinePrevious(), where previous value is optional #2632
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
Conversation
|
Thanks for the PR! There are 3 issues with this, however:
|
|
There is a similar discussion in #2561. |
Unfortunately this doesn't compile, as the type of producer
.map(Optional.init) // allow nil to be a valid initial value
.combinePrevious(nil)
.filter { previous, next in
guard let next = next else { fatalError() } // not ideal
guard let previous = previous else { return true }
return previous.value != next.value
}
.map { $1 }
.ignoreNil() // this is necessary because we had to lift the whole stream into Optional at the startThere's a few reasons this isn't ideal, but the main one for me is how much this version obscures the intent, because of the dance around Optional.
Yeah, perhaps this is actually overloading the name
Would you mind elaborating on this? Totally get there could be a latent problem here, I'm just having difficulty reasoning about it in my head. |
Sorry, I was typing from my phone and I missed that. In any case, you can easily add this as an extension in your project. I'm not in favor of adding an overload to |
|
Thanks for taking the time to review! 🙇 I'll go ahead and close this. If you get a chance, I'd love to know more about the closures with side effects issue. I had trouble working out if there's anything in the Design Guidelines addressing this particular issue. |
|
FWIW, I did some investigation on whether it's a problem for the block to have side effects in this case. I'm not sure if my reasoning is 100% correct, but I thought it might be useful to throw this out there. Here's a simplified object graph for the Because the closure has a reference to the However, I don't think this is a problem in this case, because there's no way to copy a If the same implementation was implemented for In this case the |
I personally found this operator useful when filtering a stream, such that the first value should always be passed through, but subsequent values should be compared to previous values. For example: