Scalaz 管道运算符与列表方法连接 [英] Scalaz pipe operator connected with a list method

查看:36
本文介绍了Scalaz 管道运算符与列表方法连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Scala 的新手,但我在使用 OCaml 方面有一些经验.我想通过以下方式采用 Scalaz 中定义的管道运算符:

I am a newbie to Scala, but I have some experience in using OCaml. I am thinking to adopt the pipe operator defined in Scalaz in the following way:

import scalaz._
import Scalaz._

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

上面的代码工作正常.但是,当我想桥接另一个函数以获取list 的长度时,它会引发编译错误:

The above code works fine. However, when I want to bridge another function to get the length of the list, it throws an compilation error:

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

另外,我可以将 |> 运算符放在另一行吗?像这样吗?

Besides, can I put the |> operator in another line? like this?

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

目前,我不知道如何在 Scala 中完成上述两件事.如果这个问题太天真了,我真的很抱歉..但是谁能告诉我它在 Scala 中是否可行?谢谢!

Currently, I have no idea how to do the above two things in Scala. I am really sorry if this question is too naive.. But could anyone tell me whether it is feasible in Scala? Thank you!

推荐答案

您需要向画眉"组合子传递一个函数,因此以下工作:

You need to pass the "thrush" combinator a function, so the following works:

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

如果要插入换行符,请将运算符放在行尾:

If you want to insert line breaks, put the operator at the end of the line:

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

或者以下也有效:

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

这篇关于Scalaz 管道运算符与列表方法连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆