[Spark] Scala programming - basic level
环境配置
IDE: https://www.jetbrains.com/idea/
/* implement */
语言特性
Online compiler: https://scastie.scala-lang.org/
只记录 “不太一样” 的特性。
一、常量变量
常量 val, 变量 var。
二、循环
- var name = "hello"
- for (n <- name)
- {
- println(n)
- }
嵌套循环,遍历数字和数组元素。
- def main(args: Array[String]): Unit = {
- for (a <- 1 to 10) {
- for (b <- 1 to 10) {
- if (a == b) {
- println("a = " + a + ", b = " + b)
- }
- }
- }
//嵌套循环的等价新写法
for (a <- 1 to 10; b <- 1 to 10 if a == b) {
...
}- val arr = Array[Int](1,2,3,4,5)
- for(elem <- arr) {
- println(elem1)
- }
- }
until 则不包括最后一个元素。
- val arr = Array[Int](1,2,3,4,5)
- val arr1 = new Array[Int](arr.length)
- for (index <- 0 until arr.length) {
- arr1(index) = arr(index) + 10
- }
- println(arr1.toBuffer)
类似与lambda的写法。
- val arr = Array[Int](1,2,3,4,5)
- val ret = for (a <- arr) yield a + 10
- println(ret.toBuffer)
三、条件赋值
支持混合表达。
- def main(args: Array[String]): Unit = {
- val x = 1
- val y = if (x > 0) 1 else -1
- println(y)
- val m = if (x > 2) 1
- println(m)
- val n = if (x > 2) 1 else ()
- println(n)
- val k = if (x < 0) 0 else if (x >= 1) 1 else -1
- print(k)
- }
四、函数
- def ml(a: Int, b: Int): Unit = {
- println("hello, " + (a+b))
- }
- def main(args: Array[String]): Unit = {
// 1.匿名函数- val f1 = (a:Int, b: Int) => a + b
- println(f1(10, 20))
- // 2.普通函数
- ml(10,20)
- }
/* implement */
[Spark] Scala programming - basic level的更多相关文章
- PAT basic level 1001-1019 解题笔记
1002 写出这个数 采用字符串输入数据,再对每位减去字符‘0’,得到该位相应的整数 int len=s.length();//字符串的长度 ; ;i<len;i++)//每位减去‘0’,逐位相 ...
- C#版 - PAT乙级(Basic Level)真题 之 1024.科学计数法转化为普通数字 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. PAT Bas ...
- C#版 - PAT乙级(Basic Level)真题 之 1021.个位数统计 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - P ...
- PAT (Basic Level) Practise (中文)-1039. 到底买不买(20)
PAT (Basic Level) Practise (中文)-1039. 到底买不买(20) http://www.patest.cn/contests/pat-b-practise/1039 小红 ...
- 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 ...
- PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20)
PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20) http://www.patest.cn/contests/pat-b-practise/1024 ...
- PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)
PAT (Basic Level) Practise (中文)-1025. 反转链表 (25) http://www.patest.cn/contests/pat-b-practise/1025 ...
- PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15)
PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15) http://www.patest.cn/contests/pat-b-practise/10 ...
- PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20)
PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20) http://www.patest.cn/contests/pat-b-practise/1027 本题 ...
随机推荐
- 工作中常用的Git操作
粘贴自:微信公众号:程序员共成长 分支操作: git branch 创建分支 git branch -b 创建并切换到新建的分支上 git checkout 切换分支 git branch 查看分支列 ...
- async 与 await
async 关键字 1.含义:表示函数是一个异步函数,意味着该函数的执行不会阻塞后面代码的执行 2.定义与调用 async function hello (flag) { if (flag) { re ...
- GDB调试器教程
启动和退出GDBGDB(GNU Project Debugger)几乎适用于所有类Unix系统,小巧方便且不失功能强大,Linux/Unix程序员经常用它来调试程序. 总的来说有几下几种方法启动GDB ...
- .configurable:可配执行 .enumerble:枚举性 .writable:可读写性 .value:数据值
configurable:控制属性能否被删除,只有当属性的configurable特性的值为true时,该属性才能够被删除. 默认值为false,即不可删除) var person = {}; Obj ...
- CodeForces 837F - Prefix Sums | Educational Codeforces Round 26
按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforc ...
- PHP mysqli_insert_id() 函数
定义和用法 mysqli_insert_id() 函数返回最后一个查询中自动生成的 ID(通过 AUTO_INCREMENT 生成). 语法 mysqli_insert_id(connection); ...
- 科大讯飞语音转文字,ffmpeg将wav转成pcm二进制音频文件
百度和讯飞和其他厂都提供了语音识别的接口,这里使用讯飞的识别将本地手机录的音频文件转成文字 以下注意事项: 1.X_Param 参数开始的时候带了空格导致验证不过,原因是讯飞将字符串做了repelce ...
- Yet Another Division Into Teams
E. Yet Another Division Into Teams 首先要想明白一个东西,就是当一个小组达到六个人的时候,它一定可以拆分成两个更优的小组. 这个题可以用动态规划来写,用一个数组来保存 ...
- Codeforces 1051 D.Bicolorings(DP)
Codeforces 1051 D.Bicolorings 题意:一个2×n的方格纸,用黑白给格子涂色,要求分出k个连通块,求方案数. 思路:用0,1表示黑白,则第i列可以涂00,01,10,11,( ...
- isPrototypeOf,instanceof, hasOwnProperty,in的作用与区别
isPrototypeOf 作用:检测一个对象是否是另一个对象的原型.或者说一个对象是否被包含在另一个对象的原型链中 function Fn(name){ this.name=name; } var ...