微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Scala中自定义“let”表达式

我很乐意让我们构建类似于 Scala中的Haskell中的那个.我尝试了几种方法,但似乎没有一种方法.这是一些代码

object CustomLet extends App {
  val data = for (i <- 1 to 1024; j <- 1 to 512) yield (i % j) * i * (i + 1) - 1

  def heavyCalc() = { println("heavyCalc called"); data.sum }

  def doSomethingWithRes(res: Int) = {
    println(s"${res * res}")
    1
  }

  def cond(value: Int): Boolean = value > 256

  // not really usable,even though it's an expression (2x heavyCalc calls)
  def withoutLet() = if (cond(heavyCalc())) doSomethingWithRes(heavyCalc()) else 0

  // not an expression
  def letWithVal(): Int = {
    val res = heavyCalc()
    if (cond(res)) doSomethingWithRes(res)
    else 0
  }

  // a lot of code to simulate "let",at least it is an expression
  def letWithmatch(): Int = heavyCalc() match {
    case res => if (cond(res)) doSomethingWithRes(res) else 0
  }

  // not perfect solution from
  // https://stackoverflow.com/questions/3241101/with-statement-equivalent-for-scala/3241249#3241249
  def let[A,B](param: A)(body: A => B): B = body(param)

  // not bad,but I'm not sure if it Could handle more bindings at once
  def letWithApp(): Int = let(heavyCalc()) {res => if (cond(res)) doSomethingWithRes(res) else 0}

  List[(String,() => Int)](
    ("withoutLet",withoutLet),("letWithVal",letWithVal),("letWithMatch",letWithMatch),("letWithApp",letWithApp)
  ).foreach(
    item => item match {
      case (title,func) => {
        println(s"executing $title")
        val ret = func()
        println(s"$title finished with $ret")
        println()
      }
    }
  )
}

这是它的理想外观(只有一个绑定,更多可以分隔,;不确定in关键字):

// desired look
  def lettest(): Int =
    let res = heavyCalc() in
      if (cond(res)) doSomethingWithRes(res) else 0

我不确定它是否可能,但我没有大多数高级Scala之类的东西,比如宏,所以我无法分辨.

编辑1:要清楚,我期待的主要内容是:表达和相对简单的语法(如上所述).

解决方法

你可以使用前进管道:

object ForwardPipeContainer {
  implicit class ForwardPipe[A](val value: A) extends AnyVal {
    def |>[B](f: A => B): B = f(value)
  }
}

像这样使用:

import ForwardPipeContainer._

def f(i: Int) = i * i

println( f(3) |> (x => x * x) )

您可以在元组中放置多个参数:

println( (f(2),f(3)) |> (x => x._1 * x._2) )

如果与部分函数synatx结合使用会更好看:

println( (f(2),f(3)) |> { case (x,y) => x * y } )

这个答案是What is a good way of reusing function result in Scala的变种,两者都是基于Cache an intermediate variable in an one-liner,我得到了最初的想法.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐