scala 构造_Scala咖喱和自动类型依赖的封闭构造

scala 构造

Scala Currying is the process of transforming a function that takes multiple arguments into a single argument.

Scala Currying是将包含多个参数的函数转换为单个参数的过程。

Consider an example of multiplying two numbers .Open the scala REPL shell and create the multiply method as

考虑一个将两个数字相乘的示例。打开scala REPL shell并创建乘法方法为

def multiply(a:Int,b:Int) = a*b
Output:multiply: (a: Int, b: Int)Int

The multiply function accepts two arguments a and b of Integer data type and return the result of a*b which is of Integer data type.

乘法函数接受Integer数据类型的两个参数a和b并返回a * b的结果,该结果是Integer数据类型。

Invoke the method as multiply(4,5) and you will get output as res10:Int = 20

将方法调用为乘法(4,5),您将获得输出为res10:Int = 20

Now let us apply the currying for this function as;

现在让我们将此函数应用为currying;

def multiply(a:Int) = (b:Int) => a*b

Output: multiply: (a: Int)Int => Int

输出:乘以:(a:Int)Int => Int

The multiply function takes a single argument a of Integer data type in the function declaration. Inside the function it takes one more parameter b of Integer data type and return a* b as the result.

乘法函数在函数声明中采用Integer数据类型的单个参数a。 在函数内部,它需要再加上一个Integer数据类型的参数b并返回a * b作为结果。

Suppose if we invoke the function with single argument the function closure reference is returned as

假设如果我们使用单个参数调用函数,则函数闭包引用返回为

multiply(5)
res10: Int => Int = <function1>

Invoke the function by passing two arguments as

通过将两个参数传递为来调用该函数

multiply(4)(5)
res11:Int = 20

自动类型相关的封闭构造 (Automatic Type Dependent Closure Construction)

In scala, parameterless function names allows as parameters of methods. When such methods are called the nullary functions are evaluated and not the actual parameters for the parameterless function names. The nullary function encapsulates computation of the corresponding parameter called call-by-name evaluation.

在scala中,无参数函数名称允许作为方法的参数。 调用此类方法时,将评估无效函数,而不评估无参数函数名称的实际参数。 无效函数封装了称为“按名字求值”评估的相应参数的计算。

Let’s look at an example to understand this more clearly.

让我们看一个例子,以更清楚地理解这一点。

TypeDependent.scala

TypeDependent.scala

package com.journaldev.scala

object TypeDependent {
  def main(args: Array[String]) {
    def forloop(rule: => Boolean)(body: => Unit): Unit =
      if (rule) {
        body
        forloop(rule)(body)
      }
    var i = 7
    forloop(i > 2) {
      println("i: " + i)
      i -= 1
    }
  }
}

We are creating an object TypeDependent which defines a method “forloop”. This method takes two parameters rule and body. Whenever the formal parameters are used in the body of forloop, the implicitly created nullary functions will be evaluated. Here we are implementing for loop, below image shows the output of above program.

我们正在创建一个对象TypeDependent ,该对象定义了一种方法“ forloop”。 该方法采用规则和正文两个参数。 每当在forloop主体中使用形式参数时,都会评估隐式创建的null函数。 这里我们实现了for循环,下图显示了上面程序的输出。

Now let us have a look at more complex example with more operations.

现在让我们看一下具有更多操作的更复杂的示例。

Typedep.scala

Typedep.scala

package com.journaldev.scala

object Typedep extends App {

  def t1(body: => Unit): Criteria =
    new Criteria(body)
  protected class Criteria(body: => Unit) {
    def condition(rule: => Boolean) {
      body
      if (!rule) condition(rule)
    }
  }
  var x = 7
  t1 {
    println("x: " + x)
    x -= 1
  } condition (x == 2)
}

We are creating a scala object which contains a method t1 that accepts body parameter of Unit data type and returns an instance of class Criteria. The criteria class defines has a method “condition” which checks the condition if x == 2 after decrementing the value of x. Below image shows the output of the above program.

我们正在创建一个scala对象,其中包含方法t1,该方法接受Unit数据类型的body参数并返回Criteria类的实例。 标准类定义了一个方法“ condition”,该方法在递减x的值后,如果x == 2,则检查条件。 下图显示了以上程序的输出。

Personally I don’t like this feature a lot because of complexity, but it’s good to know.

就个人而言,由于复杂性,我不太喜欢此功能,但是很高兴知道。

翻译自: https://www.journaldev.com/8534/scala-currying-and-automatic-type-dependent-closure-construction

scala 构造

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值