Haskell 问题有标准库解决方案吗?
我想使用 Data.List.groupBy 根据 snd
元素的相等性对元组列表进行分组。
我可以这样做:
groupBy (\l r -> snd l == snd r) listOfTuples
但它让我觉得比较函数中有太多的样板——特别是因为如果我进行更复杂的比较,它可能会变得更加混乱。我想做类似的事情:
groupBy (comparing snd) listOfTuples
但是比较的类型签名是 comparing :: (Ord a) => (b→a)→ b-> b->排序
,因此在此示例中无法编译。
我也可以这样做:
groupBy (\l r -> (comparing snd l r) == EQ) listOfTuples
但这并不比第一次尝试好。在我自己推出之前,是否有一个标准库解决方案可以解决这个问题?
I want to use Data.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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为标准库中曾经有
equating = on (==)
,尽管我现在似乎找不到它。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?