-
Notifications
You must be signed in to change notification settings - Fork 29
Replace mergemap with switchmap #30
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
Open
raveclassic
wants to merge
6
commits into
gcanti:master
Choose a base branch
from
raveclassic:replace-mergemap-with-switchmap
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
57c004f
build(npm): add fp-ts-laws dev dependency
raveclassic f4ed3a1
feat: replace Alternative instance with Plus
raveclassic be8bde6
feat: replace mergeMap with switchMap
raveclassic 3784907
test: fix distributivity test
raveclassic 2581340
test: fix distributivity test
raveclassic 3fc7c4b
test: fix distributivity test
raveclassic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { Kind, URIS } from 'fp-ts/lib/HKT' | ||
| import { Alt1 } from 'fp-ts/lib/Alt' | ||
|
|
||
| /** | ||
| * The `Plus` type class extends the `Alt` type class with a value that should be the left and right identity for `alt`. | ||
| * | ||
| * It is similar to `Monoid`, except that it applies to types of kind `* -> *`, like `Array` or `Option`, rather than | ||
| * concrete types like `string` or `number`. | ||
| * | ||
| * `Plus` instances should satisfy the following laws: | ||
| * | ||
| * 1. Left identity: `A.alt(zero, fa) == fa` | ||
| * 2. Right identity: `A.alt(fa, zero) == fa` | ||
| * 3. Annihilation: `A.map(zero, f) == zero` | ||
| * | ||
| * @since 0.7.0 | ||
| */ | ||
| export interface Plus1<F extends URIS> extends Alt1<F> { | ||
| readonly zero: <A>() => Kind<F, A> | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Theoretical we could replace
switchMapwithexhaustMapand would also get the memory problem solved and hold all laws right?In practice you are totally right that
switchMapshould be the defaultchain. Should we add for completeness alternativeMonadinstances likemergeMonadandexhaustMonad?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK
exhaustMapwaits for passedObservableto complete before subscribing to the new created one (result ofchain). Looks like an incorrect behavior and I can't think of a use case for that.Replacing
switchMapwithmergeMapis a matter ofconst m = {...observable, chain: (fa, f) => fa.pipe(mergeMap(f))}- is it really necessary to put it into the lib? What about other possible operator replacements? IMOswitchMapcovers 99% usecases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a theorical question sure for the use cases we are looking at it does different things.
For the
ReaderObservablethis approach would not be practical and it would follow the implementation style offp-tslike done for taskSeq. But of cause it could be part of another PR. Totally agree with the fact thatswitchMapcovers most of the usecases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean that there should be additional
mergeReaderObservableand the same for any other instance usingObservableas base?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thats what I would like to discuss to keep up with the
fp-tscoding style. @gcanti any opinions?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's tricky :) There's no way to test
.toNotBeonTestScheduler'sexpectObservable, so we need to passassert.notDeepStrictEqualas equality assertion function. Then we intentionally test that resulting observable does not equal marbles, because they are "incorrect" but should be the same as insuccess'RIGHT SIDE.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Waaaaaaaait... Let me recheck o_O
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, you are right, there's an error in the test. How could I miss it?... Anyway I updated the test and it still shows distributivity does not hold
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a proposal: for what I can undestand switching to
switchMapis important to you, while a discussion aboutAlt/Alternativeinstances, albeit being very interesting from a theoretical point of view, is way less important in practice (it's justmergein the end), and currently is a blocker.So what about:
mergeMapwithswitchMap(breaking change)filterMapimplementationAlt/Alternativeinstances fromObservable,ObservableEitherandReaderObservable(it's a breaking change but we are already doing a breaking change forswitchMap)@raveclassic @mlegenhausen what do you think?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gcanti We can keep
zeroas part ofPlustypeclass, it doesn't requirealtto be distributive. So thatObservableis aPlus & Alt, but not anAlternative. This is not a breaking change, everything stays structurally equal.But I'm open to any decision.