为什么我的 Haskell snd 实现不能在 Scala 中编译? [英] Why does my implementation of Haskell snd not compile in Scala?
本文介绍了为什么我的 Haskell snd 实现不能在 Scala 中编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我按照 Haskell 的定义定义了以下函数 snd
I defined the following function along the lines of Haskell snd
def snd[T](pair: (_, T)): T = pair._2
尝试将它与 List[ListNode[T]]
一起使用不会编译.为什么不呢?
Trying to use it with a List[ListNode[T]]
doesn't compile. Why not?
list
.reduceOption(snd)
哪里:
case class ListNode[T](data: T, var next: Option[ListNode[T]])(implicit ordering: Ordering[T]) extends Ordered[ListNode[T]] {...}
错误:
Type mismatch, expected: (NonInferedA1, NonInferedA1) => NonInferedA1, actual Tuple2[_, Nothing] => Nothing
推荐答案
reduce
和 reduceOption
方法要求函数具有元数 2
,而不是一元接受元组的函数.
Methods reduce
and reduceOption
require functions with arity 2
, not unary functions that take a tuple.
有区别
Function1[(X, Y), Z]
和
Function2[X, Y, Z]
第一个是一元的并接受一个元组,第二个是二元的.方法及其 eta 扩展也是如此.
The first one is unary and takes a tuple, the second one is binary. Same holds for methods and their eta-expansions.
这里按预期工作:
def twoArgSnd[T](a: Any, b: T): T = b
list.reduceOption(twoArgSnd[Int])
<小时>
还相关:
Also related:
这篇关于为什么我的 Haskell snd 实现不能在 Scala 中编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文