scala. 動態二維數組,scala 構造_Scala咖喱和自動類型依賴的封閉構造

 2023-11-19 阅读 17 评论 0

摘要:scala 構造Scala Currying is the process of transforming a function that takes multiple arguments into a single argument. Scala Currying是將包含多個參數的函數轉換為單個參數的過程。 scala. 動態二維數組,Consider an example of multiplying two numbers .Open t

scala 構造

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

Scala Currying是將包含多個參數的函數轉換為單個參數的過程。

scala. 動態二維數組,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數據類型。

scala元組、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

scala找不到或無法加載主類、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作為結果。

scala定義類。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.scalaobject TypeDependent {def main(args: Array[String]) {def forloop(rule: => Boolean)(body: => Unit): Unit =if (rule) {bodyforloop(rule)(body)}var i = 7forloop(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.scalaobject Typedep extends App {def t1(body: => Unit): Criteria =new Criteria(body)protected class Criteria(body: => Unit) {def condition(rule: => Boolean) {bodyif (!rule) condition(rule)}}var x = 7t1 {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 構造

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/183362.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息