-
Notifications
You must be signed in to change notification settings - Fork 59
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
Proposal for contains
function
#35
Comments
I think that having two distinct functions doing literally the same but named after two synonyms (member and contains) could be a little bit confusing. Maybe all we need is just a new function to suit your original problem: {-| Creates a list composed of elements common to both provided lists.
-}
intersection : List a -> List a -> List a It would not hurt if there was also an: intersectionBy: ((a, a) -> Bool) -> List a -> List a -> List a` |
Is there still a want/need for this? I could write this :). |
I think it makes sense for No opinion on if |
I would vote for: The latter seems unfriendly to function composition. What would |
Maybe better name for: I am looking at the RamdaJS http://ramdajs.com/docs/ library. All that functions and their names were carefully though out, in the past there were many incompatible changes adjusting to new functions and as a result of discussions on a mailing list, so maybe we could use their achievements? P.S. |
Actually I think it should be |
I like It seems like we've abstracted from @stil4m 's original use case (which I think has been fruitful). I've been in his exact use case, and I made myself a helper function called What about this proposal? intersection : List a -> List a -> List a
intersection [ 1, 2 ] [ 2, 3 ] == [ 2 ]
doIntersect : List a -> List a -> Bool
doIntersect list0 list1 =
not <| List.isEmpty (intersection list0 list)
doIntersect [ 1, 2 ] [ 2, 3 ] == True |
Keep in mind that there already is a function for getting the intersection of two sets, namely Another question is how would one even define intersection for lists. In terms of handling duplicate values, should |
I am tempted to disagree with what you said about sets. I would think that one might want to preserve the order of a list, and get it's intersection with another list. But then that does force your question about what order the output would have. The order has to be based on either the first or second parameter. Since its based on one or the other, its more like a filter that uses one list on the other; not a true intersection. So anyway, good points, I agree. That brings us back to the original problem, which I am not sure we've fully formed. |
@Chadtech That is exactly what the original ticket was for. |
Right. I'm with ya. I still feel good about my proposal, if we just change the names (since they arent true intersections). How about only: List a -> List a -> List a
only list0 list1 =
List.filter ((flip List.member) list1) list0
hasOnly : List a -> List a -> Bool
hasOnly list0 list1 =
not <| List.isEmpty (only list0 list) |
Valid point! Intersection combines lists into a set. The function could be implemented, but it is out of scope here, as this is not what OP wanted. About the
or maybe:
About |
@Chadtech I like your proposal. My
Regarding
EDIT |
Parameter order Okay I see what you both mean about the parameter order. Yeah, it should be flipped. name How about hasOnly In response to @witoldsz, Yeah I am sure there is a more efficient implementation. Anyway @pzp1997, the purpose I had in mind was just to say if a list has any of the members from a different list. So, |
I would suggest renaming the
I used plural, because we are checking against a list of members, not a single member. |
@Chadtech if I now understand
This also made me realize that my version of
And given the likely implementation of I'm beginning to think that maybe the right idea is what was suggested in the original proposal. If we create an alias for Sorry to turn around and change my opinion so suddenly. I have been thinking about this for a while, yet only realized this today. My "vote" is in favor of adding a function that aliases
|
Often I'll find myself doing
someList |> List.filter (flip List.member myList)
.I assume that other people have this same construct to filter a list.
Would it be nice to have a function in this package that makes this code cleaner. Such as:
Then the resulting code would be nicer and more concise:
I can make a PR on this if my suggestion some kind of approval.
The text was updated successfully, but these errors were encountered: