如何编写以无点样式返回Applicative的函数?

时间:2015-08-11 13:52:45

标签: scalaz applicative pointfree

这是我之前的question

的后续内容

假设我有一些函数返回scalaz.Validaton

type Status[A] = ValidationNel[String, A]

val isPositive: Int => Status[Int] = 
  x => if (x > 0) x.success else s"$x not positive".failureNel

val isNegative: Int => Status[Int] = 
  x => if (x < 0) x.success else s"$x not negative".failureNel

我可以写一个新函数makeX

case class X(x1: Int, // should be positive
             x2: Int, // should be positive
             x3: Int) // should be negative

val makeX: (Int, Int, Int) => Status[X] = (x1, x2, x3) => 
  (isPositive(x1) |@| isPositive(x2) |@| isNegative(x3)) (X.apply _)

我的问题是:如何以无点样式编写makeX

1 个答案:

答案 0 :(得分:1)

这是一种让它无表情的时髦方式:

val makeX: (Int, Int, Int) => Status[X] = isPositive(_) |@| isPositive(_) |@| isNegative(_) apply X