当前位置:网站首页>Application scenarios of large arrows in Scala
Application scenarios of large arrows in Scala
2022-06-13 03:28:00 【TRX1024】
scala in “=>” Application scenarios of
1、 Represents the return type of a function
def main(args: Array[String]): Unit = {
// Define a function variable
var x: (Int) => Int = test
var x2: (Int) => String = test2
println(x(2)) // 4
println(x2(2)) // 2trx
}
def test(x: Int) = x * 2
def test2(x: Int) = x+"trx"
(Int)=>Int、(Int)=>String On the left is the parameter type , On the right is the return value type .
2、 Anonymous functions
def main(args: Array[String]): Unit = {
// Define an anonymous function
var fun = (x: Int) => x * 2
println(fun(10)) // 20
}
Anonymous function definition , On the left is the parameter On the right is the function implementation (x: Int)=>{}
3、case sentence
def main(args: Array[String]): Unit = {
val x = 10
val y = 10
val value = x + y match {
case 20 => x
case 30 => y
}
print(value)
}
In pattern matching match and try-catch Use both “=>” Represents the output result or returned value
4、By-Name Parameters( Named parameter )
The expression of a named parameter will not be evaluated before the function call , Instead, it will be wrapped into an anonymous function and passed as a function parameter , For example, a parameter whose parameter type is a nonparametric function is a named parameter .
object SayHello {
def main(args: Array[String]): Unit = {
println(" Value transfer call :")
delayed0(time())
println("===================")
println(" Call by name :")
delayed(time())
println("===================")
}
def time() = {
println(" Acquisition time , It's in nanoseconds ")
System.nanoTime
}
def delayed0(t : Long)={
println(" stay delayed0 In the way ")
println(" Parameters "+t)
t
}
def delayed(t: => Long) = { // The point is
println(" stay delayed In the way ")
println(" Parameters : " + t)
t
}
}
边栏推荐
- Pollution discharge fees of listed companies 2010-2020 & environmental disclosure level of heavy pollution industry - original data and calculation results
- Four ways of array traversal in PHP
- Simple use of qtreeview of QT (including source code + comments)
- Use PHP to count command line calls on your computer
- Panel for measuring innovation efficiency of 31 provinces in China (using Malmquist method)
- MySQL and PostgreSQL installation subtotal
- [JVM series 8] overview of JVM knowledge points
- 2021-08-30 distributed cluster
- This article takes you to learn DDD, basic introduction
- A personal understanding of interpreted and compiled languages
猜你喜欢
English语法_方式副词-位置
Simulink代码生成: 简单状态机及其代码
ONNX+TensorRT+YoloV5:基于trt+onnx得yolov5部署1
Parallel one degree relation query
Prefecture level city - air flow coefficient data - updated to 2019 (including 10m wind speed, boundary height, etc.)
Complex network analysis capability based on graph database
Azure SQL db/dw series (9) -- re understanding the query store (2) -- working principle
This article takes you to learn DDD, basic introduction
Panel for measuring innovation efficiency of 31 provinces in China (using Malmquist method)
视频播放屡破1000W+,在快手如何利用二次元打造爆款
随机推荐
2021-08-30 distributed cluster
Use of compact, extract and list functions in PHP
Understanding the ongdb open source map data foundation from the development of MariaDB
The most complete ongdb and neo4j resource portal in history
MySQL learning summary 10: detailed explanation of view use
Nuggets new oil: financial knowledge map data modeling and actual sharing
PHP uses the header function to download files
Qt之QTreeView的简单使用(含源码+注释)
How to Draw Useful Technical Architecture Diagrams
Video playback has repeatedly broken 1000w+, how to use the second dimension to create a popular model in Kwai
Graph data modeling tool
Complex network analysis capability based on graph database
Azure SQL db/dw series (14) -- using query store (3) -- common scenarios
通过Web ETL统一调度和管理DataX任务
Golang picks up: why do we need generics
MySQL learning summary 7: create and manage databases, create tables, modify tables, and delete tables
English grammar_ Mode adverb position
Reading notes of effective managers
CXGRID keeps the original display position after refreshing the data
Transaction processing in PDO