Scala<标识符>

1. 标识符

  • 变量名、函数名、类名等统称为标识符Scala可以使用任何字符来作标识符,比如 ()!#%&×+-/:<>=?@\^|~ 等。

  • 反引号中可以使用任何字符序列。

    val √ = scala.math.sqrt _
    √(2)
    val `val` = 42
    println(`val`)
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

2. 中置操作符

  • 操作符要接两个参数,分别位于操作符两边。

  • a 标识符 b 等价于 a.标识符(b) 。

    1 to 10
    等价于 1.to(10)

    1 -> 10
    等价于 1 .->(10) 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

3. 一元操作符

  • 只有一个参数的操作符成为一元操作符,操作符的位置可能位于参数的前或后,所有有下面两种情况。

  • a 标识符 等价于 a.标识符() ,标识符 a 等价于 a.unary_标识符 。

    1 toString
    等价于 1.toString()

    val a = 42
    -a
    等价于 a.unary_-
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

4. 赋值操作符

  • 赋值操作符的形式为 操作符=,表达式是 a 操作符= b ,等价于 a = a 操作符 b 。
    a += 1
    等价于 a = a + 1
 
  • 1
  • 2
  • 1
  • 2
  • <=、>=、!=、==、===、=/= 等不是赋值操作符,

5. 优先级

  • 后置操作符优先级低于中置操作符,a 中置操作符 b 后置操作符 等价于 (a 中置操作符 b) 后置操作符 。

  • 符号优先级如下表所示:

    操作符 优先级
    除下面字符以外的操作符 1(最高)
    * / % 2
    + - 3
    : 4
    < > 5
    != 6
    & 7
    ^ 8
    | 9
    赋值操作符 10(最低)

6. 结合性

  • Scala除冒号操作符赋值操作符右结合,所有其他操作符都是左结合
    // 构造列表List
    1 :: 2 :: Nil   // :: 右结合
    1 :: (2 :: Nil)
    (1 :: 2) :: Nil // 错误
 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
  • 右结合的的第二个参数就是方法,比如 2::Nil 等价于 Nil.::(2)

7. apply和update方法

  • 函数或方法位于赋值语句的等号左侧,调用的是update方法,否则调用的apply方法
    val scores = new scala.collection.mutable.HashMap[String, Int]
    scores("Bob") = 100 
    等价于调用:scores.update("Bob", 100) 

    val bobsScore = scores("Bob")
    等价于调用:scores.apply("Bob")
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

8. 提取器unapply

  • 提取器:是一个带有unapply方法的对象,可以当做是伴生对象apply的方法的反操作。

  • unapply接受一个对象,从中提取值。

    class Fraction(n: Int, d: Int) {
      private val num: Int = if (d == 0) 1 else n;
      private val den: Int = if (d == 0) 0 else d;
      def *(other: Fraction) = new Fraction(num * other.num, den * other.den)
    }

    // unapply返回的就是构造该对象的两个值
    object Fraction {
      def apply(n: Int, d: Int) = new Fraction(n, d)
      def unapply(input: Fraction) =
        if (input.den == 0) None else Some((input.num, input.den))
    }

    object Main extends App {  
      var Fraction(a, b) = Fraction(3, 4) * Fraction(2, 5)
      println(a)
      println(b)
    }
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 每个样例类都会自动具备apply方法和unapply方法,样例类后面章节会讲解,这里只要了解就可以。

  • 提取器可以测试输入而不真正将其值提取出来,只返回一个Boolean。


9. unapplySeq方法

  • 使用unapplySeq方法可以提取任意长度的值序列,它返回一个Option[Seq[A]],A是被提取的类型
    // Name 提取器可以产生所有组成部分的序列
    // 关于 Option,Some,None后面章节会详细讲解,这里只需了解upapplySeq的作用就可以。
    object Name {
      def unapplySeq(input: String): Option[Seq[String]] =
        if (input.trim == "") None else Some(input.trim.split("\\s+"))
    }
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
<?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
任务描述 基于数据清洗后的深圳通刷卡数据集(存放在/data/workspace/myshixun/Datas/SZcardDatas.csv),对地铁各线路的收益进行统计。 数据集说明 清洗之后的数据集字段没有改变,依旧是11个字段,字段说明如下: 字段名 字段类型 含义 card_no String 卡号 deal_date String 交易日期时间 deal_type String 交易类型 deal_money String 实际交易金额/分为单位 deal_value String 票价/分为单位 equ_no String 设备编码 company_name String 公司名称 station String 线路站点 car_no String 车牌号 conn_mark String 联程标记 close_date String 结算日期 数据中无列名 部分数据展示如下: FHEGHDGDC,2018-09-01 07:52:39,地铁入站,0,0,265025119,地铁七号线,福民,进AGM25-26,0,2018-09-01 00:00:00 FHFCCEIDF,2018-09-01 07:41:05,巴士,120,200,227000076,金华南巴士,332(蛇口),粤BU2975,0,2018-09-01 00:00:00 CBCGCDJDJ,2018-09-01 07:34:23,巴士,160,200,227000038,金华南巴士,331(松岗),粤BU2355,0,2018-09-01 00:00:00 CBIEHFGCB,2018-09-01 07:50:14,地铁出站,380,400,268016106,地铁一号线,世界之窗站,OGT-106,0,2018-09-01 00:00:00 DDABDEEDD,2018-09-01 07:01:08,巴士,160,200,227000175,金华南巴士,M433(福永),粤BW2968,0,2018-09-01 00:00:00 CBAEHADJA,2018-09-01 07:45:45,地铁入站,0,0,265026113,地铁七号线,皇岗口岸,进AGM18-19,0,2018-09-01 00:00:00 FFGAIJJBJ,2018-09-01 07:57:52,地铁出站,285,300,261016120,地铁三号线,水贝,AGM-120,0,2018-09-01 00:00:00 FIJIJFJFA,2018-09-01 07:56:51,地铁入站,0,0,261021116,地铁三号线,丹竹头,AGM-116,0,2018-09-01 00:00:00 FHIBEGJBD,2018-09-01 07:54:44,地铁入站,0,0,265011117,地铁七号线,西丽湖,进AGM24-25,0,2018-09-01 00:00:00 BIGFGGFAG,2018-09-01 07:49:56,地铁入站,0,0,265014107,地铁七号线,珠光,进AGM17-18,0,2018-09-01 00:00:00 FIJCEEDIB,2018-09-01 07:50:30,地铁出站,380,400,260027108,地铁二号线,香蜜,IGT-108,0,2018-09-01 00:00:00 MySQL 数据库 szcard 连接方式: url:jdbc:mysql://127.0.0.1:3306/szcard?useUnicode=true&characterEncoding=utf-8; 用户名:root; 密码:123123。 tb_subway_line_income 表结构: 字段名 数据存储类型 字段含义 subway_line varchar(255) 地铁线路 subway_line_income float(20,2) 地铁线路的收益(元) 编程要求 1.根据清洗之后的数据集进行地铁线路收益分析。 筛选出deal_type 字段为 地铁出站 的数据; 数据中 company_name 的值为地铁线路名称,deal_money 为实际交易金额,将交易金额以分为单位转换成以元为单位,统计该值,为地铁各线路的收益。 2.将查询结果存储到 MySQL 数据库 szcard 的 tb_subway_line_income(表已经提前创建)。 MySQL 查询到的数据格式示例如下: tb_subway_line_income 表中的数据: subway_line subway_line_income 地铁五号线 338649.5 地铁十一号线 126988.35 地铁二号线 146091.65 地铁四号线 117323.9 地铁一号线 306612.3 地铁七号线 93256.5 地铁三号线 275816.05 地铁九号线 78589.6 注意事项 1.任务完成后请点击"测评"按钮,验证统计结果是否正确,程序未通过的情况下,可以点击测试集查看具体问题; 2.此项目是Maven项目,pom.xml如下 <dependencies> <!--scala--> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.version}</version> </dependency> <!--Spark SQL--> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_2.11</artifactId> <version>${spark.version}</version> </dependency> <!--解析IP地址--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.14</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency> <!--MySQL 驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> </dependencies> 程序中已经导入了部分类,如果需要用到其他类请自行导入或自行编写pom.xml文件。 评测时间较长,请耐心等待一会 考核点说明 1.是否掌握对字符进行转换操作; 2.最后计算出的收益是否将原本以分为单位的交易金额转换成以元为单位。 2.是否将分析后的数据存入 szcard 数据库 tb_subway_line_income 表中。 测试说明 点击评测后,平台会对您提交代码进行测试验证,若测试结果与预期输出一致,则算通关。
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值