You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Consider changing minWith and maxWith APIs to be more like sortBy #65
sortBy has a nicer API than do minWith and maxWith.
In sortBy we generate a key for the objects to sort, and the comparator to use is automatically created based on this key. So we can do something as simple as
sortBy(prop('title'), list);
minWith and maxWith, by contrast, which don't even need to be sorted, require you to supply a comparator.
minWith(function(a, b) {return a.x - b.x;}, points)
This really should be
minWith(prop('x'), points);
Granted the current API offers some theoretic additional flexibility. But I can't see any realistic cases that would take advantage of it.
If there are no objections, I'll do this over the weekend.
i'm changing my mind on this. what if the objects in the list you are evaluating have complicated relations? e.g. uniqWith(function(a, b) { return a.x === b.y && a.y === b.y; }, list) does not get captured using this approach. Then the simple prop implementation of the *With functions is a special case of passing a more abstract comparator.
@buzzdecafe: I agree regarding uniqWith, et al. But I can't see any use cases for minWith / maxWith, although maybe we should rename to minBy / maxBy ?
@buzzdecafe I'm also not sure that your example makes real sense, as I think all of these comparisons really need to be symmetric in the two parameters. But the general point stands.
But yes, use *By for single-property comparisons, whether a natural property of the object or a synthesized one, *With for a more general function. That would be a cleaner API.
I think the objection stands for minWith on the same grounds. You can have a list of objects that have some crazy rules for determining which one is the smallest that is not captured in an attribute. Imagine there is some kind of pecking order ...
@buzzdecafe*By focuses on a single list item, generates a key for it. *With is the one that knows how to do the comparison between two different items. That was the issue that started me down this trail. There was no good reason to be writing a full-fledged comparator when I often wanted to either fetch or synthesize something from a single object.
at present pathBy takes a string as its first param. I'd like all of the *By fns to have the same semantics, and passing a function is overkill to pathBy. And I also don't wanna type-check the first arg
Yeah, that's a different beast, isn't it? I've been thinking about max/min/sort/uniq/difference, which I think could all share these semantics. But path is definitely different.
@megawac Yes, the trouble is that it would be nice if we could have a universal semantic for the *By and *With, which just got different meanings on the new path.
@megawac: We've been trying to avoid any unnecessary type-checking, making our functions as strongly-typed as feasible in JS. We definitely aren't interested in the Underscore/LoDash style of "if parameter 3 is a function do x, if it's an object, do y, and if it's a string do z." This is not to say that we have no type-checking, but there's relatively little, and that's mostly to support dynamic dispatch for extensions, such as the lazy lists, and (soon, I hope) the algebraic types.
@buzzdecafe That might be best. But can't think of a good name.
Activity
buzzdecafe commentedon Jun 21, 2014
+1
CrossEye commentedon Jun 21, 2014
Fixed in c1c986b
megawac commentedon Jun 22, 2014
I think this should this be done with
uniqWith,differenceWith, et. al. as wellbuzzdecafe commentedon Jun 22, 2014
i'm changing my mind on this. what if the objects in the list you are evaluating have complicated relations? e.g.
uniqWith(function(a, b) { return a.x === b.y && a.y === b.y; }, list)does not get captured using this approach. Then the simplepropimplementation of the*Withfunctions is a special case of passing a more abstract comparator.CrossEye commentedon Jun 22, 2014
EDIT: this was in response to @megawac's comment, not @buzzdecafe's.
I'm not so sure about that. I can easily see:
This can't necessarily be captured by a single key.
megawac commentedon Jun 22, 2014
Some other libraries allow you to return an array from these functions which will compare in order.
CrossEye commentedon Jun 22, 2014
@buzzdecafe: I agree regarding uniqWith, et al. But I can't see any use cases for
minWith/maxWith, although maybe we should rename tominBy/maxBy?buzzdecafe commentedon Jun 22, 2014
i was also thinking
*Byfor single-property comparisonCrossEye commentedon Jun 22, 2014
@buzzdecafe I'm also not sure that your example makes real sense, as I think all of these comparisons really need to be symmetric in the two parameters. But the general point stands.
But yes, use
*Byfor single-property comparisons, whether a natural property of the object or a synthesized one,*Withfor a more general function. That would be a cleaner API.buzzdecafe commentedon Jun 22, 2014
I think the objection stands for
minWithon the same grounds. You can have a list of objects that have some crazy rules for determining which one is the smallest that is not captured in an attribute. Imagine there is some kind of pecking order ...CrossEye commentedon Jun 22, 2014
@megawac Don't understand about the array. I'm mostly ok with
composeingreverseif necessary. How does returning an array help?megawac commentedon Jun 22, 2014
Would give you the same functionality from a
*Byfunction as a*With3 remaining items
CrossEye commentedon Jun 22, 2014
@buzzdecafe I like a more general
*Byfor these. The key could still be entirely synthetic:(untested, and don't know if we actually have
join, but should be clear.)CrossEye commentedon Jun 22, 2014
@megawac I agree, and it should be easy to implement the
*Byin terms of the*With.buzzdecafe commentedon Jun 22, 2014
@CrossEye that example seems like a
maxWithto me. What is the distinction ofByandWithin your mind?btw, gotta come up with a new name for
forkCrossEye commentedon Jun 22, 2014
@buzzdecafe
*Byfocuses on a single list item, generates a key for it.*Withis the one that knows how to do the comparison between two different items. That was the issue that started me down this trail. There was no good reason to be writing a full-fledged comparator when I often wanted to either fetch or synthesize something from a single object.buzzdecafe commentedon Jun 22, 2014
at present
pathBytakes a string as its first param. I'd like all of the*Byfns to have the same semantics, and passing a function is overkill topathBy. And I also don't wanna type-check the first argCrossEye commentedon Jun 22, 2014
Yeah, that's a different beast, isn't it? I've been thinking about
max/min/sort/uniq/difference, which I think could all share these semantics. Butpathis definitely different.megawac commentedon Jun 22, 2014
I disagree @buzzdecafe, I think a
lookupIteratorfunction would be sweet for choosing how to use the given argumentCrossEye commentedon Jun 22, 2014
@megawac Yes, the trouble is that it would be nice if we could have a universal semantic for the
*Byand*With, which just got different meanings on the newpath.megawac commentedon Jun 22, 2014
It wouldn't be hard to mix that in with an iterator
buzzdecafe commentedon Jun 22, 2014
since
pathByseems to be the problem child here, maybe the simplest solution is to come up with a new name for it.buzzdecafe commentedon Jun 22, 2014
let's rename
pathBytopathOnand useWithandBysemantics elsewhere as described above.CrossEye commentedon Jun 22, 2014
@megawac: We've been trying to avoid any unnecessary type-checking, making our functions as strongly-typed as feasible in JS. We definitely aren't interested in the Underscore/LoDash style of "if parameter 3 is a function do x, if it's an object, do y, and if it's a string do z." This is not to say that we have no type-checking, but there's relatively little, and that's mostly to support dynamic dispatch for extensions, such as the lazy lists, and (soon, I hope) the algebraic types.
@buzzdecafe That might be best. But can't think of a good name.
CrossEye commentedon Jun 22, 2014
@buzzdecafe That sounds good.
CrossEye commentedon Jul 11, 2014
The original goal of this issue was long ago satisfied. If we want to follow up on the remaining discussion, we can reopen later.