-
Notifications
You must be signed in to change notification settings - Fork 300
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
Make initial option in map/reduce optional #114
Conversation
Thinking on this further, maybe we should ditch the |
Yeah, this does sound like a breaking change... Maybe there's a case where someone is checking for I can't think of a concrete example off the top of my head. The code looks like it would be a bit cleaner without |
Alright, I did find a real-world example of using In this dataset, items had a The code ended up looking something like this:
I think it would be possible to tweak the stylesheet to just get the best rank within the points, and then if it's worse than In this case, removing the option for |
@redbmk thangs for digging into this! The example is pretty confusing, but as far as I understand, in this case, the difference would essentially be: clusterQuality = bestRank('clustered', quality1, quality2, ...) // with initial
clusterQuality = bestRank(quality1, quality2, ...) // without initial The second seems more flexible to me because it retains more information (and it's better to tweak the final filter IMO) . But if you want the same behavior as previously, you'd just set up It seems to me that removing |
Going to merge this as is (making |
Ah, yeah actually what you said about the To nitpick, JavaScript reduce actually does allow for an optional initial value. But regardless, I don't know that it's actually necessary in this case. If there are no points, then there will be no cluster, so no need for an initial value. |
@redbmk there's some more context on initial value use in this Wikipedia article, and as far as I understand, it mostly comes down to 1) supporting the use case of resulting value of a different type than the items, and 2) not throwing an error on empty arrays. For type coercion, we have the |
Yeah, sounds good to me. The more I think about it, the less I think we need |
See discussion in #114 for context.
See discussion in #114 for context.
This makes
map
/reduce
more convenient to use by making it possible to omit theinitial
function in most cases.When
initial
is omitted, the reducer will use the mapped values of the first reduced item as the initial value. So trivial cases like+
,min
andmax
will work seamlessly without the need forinitial
.It's kind of breaking, but I doubt many people depended on
initial
returning{}
by default, so maybe it's OK to release this as a minor semver update.cc @redbmk