环境配置

IDE: https://www.jetbrains.com/idea/

子雨大数据之Spark入门教程(Scala版)

/* implement */

语言特性

Online compiler: https://scastie.scala-lang.org/

只记录 “不太一样” 的特性。

一、常量变量

常量 val, 变量 var。

二、循环

  1. var name = "hello"
  2. for (n <- name)
  3. {
  4.   println(n)
  5. }

嵌套循环,遍历数字和数组元素。

  1. def main(args: Array[String]): Unit = {
  2.  
  3. for (a <- 1 to 10) {
  4. for (b <- 1 to 10) {
  5. if (a == b) {
  6. println("a = " + a + ", b = " + b)
  7. }
  8. }
  9. }

  10. //嵌套循环的等价新写法
    for (a <- 1 to 10; b <- 1 to 10 if a == b) {
    ...
    }
  11. val arr = Array[Int](1,2,3,4,5)
  12. for(elem <- arr) {
  13. println(elem1)
  14. }
  15. }

until 则不包括最后一个元素。

  1. val arr = Array[Int](1,2,3,4,5)
  2. val arr1 = new Array[Int](arr.length)
  3. for (index <- 0 until arr.length) {
  4. arr1(index) = arr(index) + 10
  5. }
  6.  
  7. println(arr1.toBuffer)

类似与lambda的写法。

  1. val arr = Array[Int](1,2,3,4,5)
  2. val ret = for (a <- arr) yield a + 10
  3. println(ret.toBuffer)

三、条件赋值

支持混合表达。

  1. def main(args: Array[String]): Unit = {
  2.  
  3. val x = 1
  4. val y = if (x > 0) 1 else -1
  5. println(y)
  6.  
  7. val m = if (x > 2) 1
  8. println(m)
  9.  
  10. val n = if (x > 2) 1 else ()
  11. println(n)
  12.  
  13. val k = if (x < 0) 0 else if (x >= 1) 1 else -1
  14. print(k)
  15. }

四、函数

  1. def ml(a: Int, b: Int): Unit = {
  2. println("hello, " + (a+b))
  3. }
  4.  
  5. def main(args: Array[String]): Unit = {
    // 1.匿名函数
  6. val f1 = (a:Int, b: Int) => a + b
  7. println(f1(10, 20))
  8.  
  9. // 2.普通函数
  10. ml(10,20)
  11.  
  12. }

/* implement */

[Spark] Scala programming - basic level的更多相关文章

  1. PAT basic level 1001-1019 解题笔记

    1002 写出这个数 采用字符串输入数据,再对每位减去字符‘0’,得到该位相应的整数 int len=s.length();//字符串的长度 ; ;i<len;i++)//每位减去‘0’,逐位相 ...

  2. C#版 - PAT乙级(Basic Level)真题 之 1024.科学计数法转化为普通数字 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. PAT Bas ...

  3. C#版 - PAT乙级(Basic Level)真题 之 1021.个位数统计 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - P ...

  4. PAT (Basic Level) Practise (中文)-1039. 到底买不买(20)

    PAT (Basic Level) Practise (中文)-1039. 到底买不买(20) http://www.patest.cn/contests/pat-b-practise/1039 小红 ...

  5. PAT (Basic Level) Practise (中文)- 1022. D进制的A+B (20)

    PAT (Basic Level) Practise (中文)-  1022. D进制的A+B (20)  http://www.patest.cn/contests/pat-b-practise/1 ...

  6. PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20)

    PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20) http://www.patest.cn/contests/pat-b-practise/1024 ...

  7. PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)

    PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)   http://www.patest.cn/contests/pat-b-practise/1025 ...

  8. PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15)

    PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15)    http://www.patest.cn/contests/pat-b-practise/10 ...

  9. PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20)

    PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20)  http://www.patest.cn/contests/pat-b-practise/1027 本题 ...

随机推荐

  1. 工作中常用的Git操作

    粘贴自:微信公众号:程序员共成长 分支操作: git branch 创建分支 git branch -b 创建并切换到新建的分支上 git checkout 切换分支 git branch 查看分支列 ...

  2. async 与 await

    async 关键字 1.含义:表示函数是一个异步函数,意味着该函数的执行不会阻塞后面代码的执行 2.定义与调用 async function hello (flag) { if (flag) { re ...

  3. GDB调试器教程

    启动和退出GDBGDB(GNU Project Debugger)几乎适用于所有类Unix系统,小巧方便且不失功能强大,Linux/Unix程序员经常用它来调试程序. 总的来说有几下几种方法启动GDB ...

  4. .configurable:可配执行 .enumerble:枚举性 .writable:可读写性 .value:数据值

    configurable:控制属性能否被删除,只有当属性的configurable特性的值为true时,该属性才能够被删除. 默认值为false,即不可删除) var person = {}; Obj ...

  5. CodeForces 837F - Prefix Sums | Educational Codeforces Round 26

    按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforc ...

  6. PHP mysqli_insert_id() 函数

    定义和用法 mysqli_insert_id() 函数返回最后一个查询中自动生成的 ID(通过 AUTO_INCREMENT 生成). 语法 mysqli_insert_id(connection); ...

  7. 科大讯飞语音转文字,ffmpeg将wav转成pcm二进制音频文件

    百度和讯飞和其他厂都提供了语音识别的接口,这里使用讯飞的识别将本地手机录的音频文件转成文字 以下注意事项: 1.X_Param 参数开始的时候带了空格导致验证不过,原因是讯飞将字符串做了repelce ...

  8. Yet Another Division Into Teams

    E. Yet Another Division Into Teams 首先要想明白一个东西,就是当一个小组达到六个人的时候,它一定可以拆分成两个更优的小组. 这个题可以用动态规划来写,用一个数组来保存 ...

  9. Codeforces 1051 D.Bicolorings(DP)

    Codeforces 1051 D.Bicolorings 题意:一个2×n的方格纸,用黑白给格子涂色,要求分出k个连通块,求方案数. 思路:用0,1表示黑白,则第i列可以涂00,01,10,11,( ...

  10. isPrototypeOf,instanceof, hasOwnProperty,in的作用与区别

    isPrototypeOf 作用:检测一个对象是否是另一个对象的原型.或者说一个对象是否被包含在另一个对象的原型链中 function Fn(name){ this.name=name; } var ...