【Scala】Scala语法中=>、->、<-与{}、()的使用

1. ()与{}

简单函数表达式用()
复杂函数表达式用{}

2. =>、->与<-

-> 使用为键对 k->v
<- 使用为for循环中

=>用法有四种:
(1) 表示函数的返回类型(Function Type)

// 定义函数
scala> def triple(x:Int):Int = x*3
triple: (x: Int)Int

scala> var y:(Int) => Int = triple
y: Int => Int = <function1>

scala> y(3)
res1: Int = 9

函数triple的类型就是 (x: Int) => Int 或者 Int => Int。左边是参数类型,右边是方法返回值类型。
备注: 当函数只有一个参数的时候,函数类型里面括起来函数参数的括号是可以省略的。

(2) 匿名函数

//通过匿名函数定义一个函数变量xx
scala> var xx = (x : Double) => x*10
xx: Double => Double = <function1>

scala> xx(2.2)
res7: Double = 22.0

//给一个高阶函数,传递一个函数
scala> val newList = List(1,2,3).map{(x:Int) => x*10}
newList: List[Int] = List(10, 20, 30)

匿名函数定义,=>左边是参数 右边是函数实现体 (x: Int)=>{}

(3) case语句

scala> val min = x>y match{
     | case true => y case false => x}
min: Int = 10

在模式匹配 match 和 try-catch 都用 “=>” 表示输出的结果或返回的值

(4) By-Name Parameters(传名参数)

//函数double 
scala> def doubles(x: => Int) = {
     println("Now doubling " + x)
     x*2   
 }
 doubles: (x: => Int)Int
 //调用函数
 scala> doubles(3)
 Now doubling 3
 res2: Int = 6
 
 scala> def f(x: Int): Int = {
   println(s"Calling f($x)")
   x+1
 }
 f: (x: Int)Int
 //调用函数
 scala> doubles(f(3))
 Calling f(3)
 Now doubling 4
 Calling f(3)
 res9: Int = 8

对于函数doubles而言,它的参数x就是by-name的。如果调用doubles的时候,直接给个普通的值或者非函数变量。那么doubles的执行结果就跟普通的函数没有区别。但是当把一个返回值为Int类型的函数,例如f(2),传递给doubles的时候。那么f(2)会被先计算出返回值3,返回值3传入doubles参与运算。运算完成以后得8,f(2)会被doubles在执行以后,再调用一遍

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>ai.raics</groupId> <artifactId>untitled1</artifactId> <version>1.0-SNAPSHOT</version> <name>untitled1</name> <description>Generated by spark-scala-archetype: Spark 3.5.6 + Scala 2.12.18</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <scala.version>2.12.18</scala.version> <scala.binary.version>2.12</scala.binary.version> <spark.version>3.5.6</spark.version> <maven.compiler.release>17</maven.compiler.release> <scalatest.version>3.2.18</scalatest.version> <scala.maven.plugin.version>4.9.0</scala.maven.plugin.version> <scalatest.maven.plugin.version>2.0.2</scalatest.maven.plugin.version> <surefire.plugin.version>3.2.5</surefire.plugin.version> <enforcer.plugin.version>3.4.1</enforcer.plugin.version> </properties> <dependencies> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_${scala.binary.version}</artifactId> <version>${spark.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_${scala.binary.version}</artifactId> <version>${spark.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.scalatest</groupId> <artifactId>scalatest_${scala.binary.version}</artifactId> <version>${scalatest.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.scalactic</groupId> <artifactId>scalactic_${scala.binary.version}</artifactId> <version>${scalatest.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>${enforcer.plugin.version}</version> <executions> <execution> <id>enforce-java</id> <goals><goal>enforce</goal></goals> <configuration> <rules> <requireJavaVersion> <version>[17,)</version> </requireJavaVersion> </rules> </configuration> </execution> </executions> </plugin> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>${scala.maven.plugin.version}</version> <executions> <execution> <id>scala-compile-first</id> <phase>process-resources</phase> <goals> <goal>add-source</goal> <goal>compile</goal> </goals> </execution> <execution> <id>scala-test-compile</id> <phase>process-test-resources</phase> <goals> <goal>testCompile</goal> </goals> </execution> </executions> <configuration> <scalaVersion>${scala.version}</scalaVersion> <args> <arg>-deprecation</arg> <arg>-feature</arg> <arg>-unchecked</arg> </args> </configuration> </plugin> <plugin> <groupId>org.scalatest</groupId> <artifactId>scalatest-maven-plugin</artifactId> <version>${scalatest.maven.plugin.version}</version> <configuration> <parallel>false</parallel> <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory> </configuration> <executions> <execution> <id>test</id> <phase>test</phase> <goals> <goal>test</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${surefire.plugin.version}</version> <configuration> <useSystemClassLoader>true</useSystemClassLoader> <trimStackTrace>true</trimStackTrace> </configuration> </plugin> </plugins> </build> </project>
11-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值