开发者

Is there a standard library solution to this Haskell problem?

I want to use Dat开发者_如何学JAVAa.List.groupBy to group a list of tuples based on the equality of the snd element.

I could do this:

groupBy (\l r -> snd l == snd r) listOfTuples

But it strikes me as too much boilerplate in the comparison function -- especially because it could get a lot more messy if I were doing a more complicated comparison. I would like to do something like:

groupBy (comparing snd) listOfTuples

but the type signature of comparing is comparing :: (Ord a) => (b -> a) -> b -> b -> Ordering, so it doesn't compile in this example.

I could also do:

groupBy (\l r -> (comparing snd l r) == EQ) listOfTuples

But this is no better than the first try. Is there a standard-library solution to this problem, before I roll-my-own?


groupBy ((==) `on` snd) listOfTuples

I think there used to be equating = on (==) in the standard libraries, though I can't seem to find it now.


Is this what you want?

groupBy ((==EQ) . comparing snd) listOfTuples
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜