加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Scala函数组合

发布时间:2020-12-16 10:07:51 所属栏目:安全 来源:网络整理
导读:def t[A] = (l:List[A]) = l taildef r[A] = (r:List[A]) = r reversedef tr[A] :List[A] = List[A] = t compose rtr(List(1,2,3,4)) List(3,1) 正如所料. 但不知何故,我试过的每个变体(有几种类型的注释) def tr = tail compose reverse 失败(未找到 – 值
def t[A] = (l:List[A]) => l tail
def r[A] = (r:List[A]) => r reverse
def tr[A] :List[A] => List[A] = t compose r

tr(List(1,2,3,4))

List(3,1)

正如所料.

但不知何故,我试过的每个变体(有几种类型的注释)

def tr = tail compose reverse

失败(未找到 – 值尾).我错过了一些明显的东西,但我被困住了.

解决方法

在scala中,A => B将被视为Function1,它定义了方法组合,所以如果你想使用tail compose reverse,你需要给scala编译器提示它是什么“类”,然后将“方法”改为“函数”.

这个例子 :

def tr [A] =((x:List [A])=> x.tail)compose((x:List [A])=> x.reverse)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读