2016-11-17 38 views
0

我是Scala的新手,但我有一些使用OCaml的经验。我想通过下面方式Scalaz定义的管道运营商:斯卡拉兹管道操作员连接列表方法

import scalaz._ 
import Scalaz._ 

def test = { 
    def length2(x:String) = List(x.length * 2) 
    "asdasd" |> length2 
} 

上面的代码工作正常。然而,当我想填补另一个函数来获得list的长度,它抛出一个编译错误:

def test = { 
    def length2(x:String) = List(x.length * 2) 
    "asdasd" |> length2 
    .length <======== I cannot do this... 
} 

此外,我可以把|>运营商在另一条线?喜欢这个?

def test = { 
    def length2(x:String) = List(x.length * 2) 
    "asdasd" 
    |> length2   <====== I cannot do this... 
} 

目前,我不知道该怎么做上面两件事Scala。我真的很抱歉,如果这个问题太天真了..但谁能告诉我是否可行Scala?谢谢!

回答

2

你需要通过“画眉” Combinator的一个功能,所以以下工作:

"asdf" |> length2 |> (_.length) 

如果要插入换行符,把运营商在该行的末尾:

"asdf" |> 
length2 |> 
(_.length) 

或以下也是正确的:

"asdf" 
.|> (length2) 
.|> (_.length) 
+0

这正是我想要的。非常感谢! – computereasy

+0

但是,似乎无法在左边用“|>”对齐“语句是可惜的.. – computereasy