函數/方法的定義:java
def 方法名(參數名:參數類型):返回類型 ={
// 括號內的叫作方法體
// 方法體內的最後一行爲返回值,不須要return
}
複製代碼
package com.gwf.course03
object FunctionApp {
def main(args: Array[String]): Unit = {
println(add(1,1))
println(three())
println(three) // 沒有入參的函數,調用時是能夠省略的
sayHello()
sayHello
}
def add(x:Int,y:Int):Int={
x+y // 最後一行就是返回值,不須要返回
}
def three()=1+2 // 能夠自動判斷返回類型
def sayHello(): Unit ={ // Unit 表明沒有返回值
println("say hello")
}
}
複製代碼
2
3
3
say hello
say hello
Process finished with exit code 0
複製代碼
默認參數:在函數定義時,容許指定參數的默認值 $SPARK_HOME/conf/spark-defaults.confsql
spark中的應用 數組
object FunctionApp {
def main(args: Array[String]): Unit = {
println(speed(100,10))
println(speed(time=10,distance=100)) // 根據參數名進行傳參 // 不建議
}
def speed(distance:Float,time:Float ):Float={
distance/time
}
}
複製代碼
JDK5+ : 提供了可變參數bash
package com.gwf.course03
object FunctionApp {
def main(args: Array[String]): Unit = {
println(sum(1,2,3,4))
println(sum(1,2,3))
println(sum(Array(1,2):_*)) // 將數組傳入可變參數的函數
}
def sum(numbers:Int*)={
var result = 0
for(num <- numbers){
result += num
}
result
}
}
複製代碼
spark-sql 中的應用app
if(a>0) true else false
複製代碼
scala> 1 to 10 // 左閉右閉
res11: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> Range(1,10) // 左閉右開
res12: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> 1.to(10) // 等同於 1 to 10
res13: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> Range(1,10,2) // 能夠選擇步長
res14: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9)
scala> Range(1,10,0) // 步長不能夠爲0,死循環
java.lang.IllegalArgumentException: step cannot be 0.
at scala.collection.immutable.Range.<init>(Range.scala:86)
at scala.collection.immutable.Range$.apply(Range.scala:439)
... 32 elided
scala> Range(10,0,-1) // 能夠從大到小來
res16: scala.collection.immutable.Range = Range(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
scala> 1 until 10 // 左閉右開 底層調用的Range
res17: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
複製代碼
Range源碼ide
/** Make a range from `start` until `end` (exclusive) with given step value. * @note step != 0 */
def apply(start: Int, end: Int, step: Int): Range = new Range(start, end, step)
@SerialVersionUID(7618862778670199309L)
@deprecatedInheritance("The implementation details of Range makes inheriting from it unwise.", "2.11.0")
class Range(val start: Int, val end: Int, val step: Int)
...
@deprecated("This method will be made private, use `length` instead.", "2.11")
final val numRangeElements: Int = {
// 若是step==0則拋異常
if (step == 0) throw new IllegalArgumentException("step cannot be 0.")
else if (isEmpty) 0
else {
val len = longLength
if (len > scala.Int.MaxValue) -1
else len.toInt
}
}
...
}
複製代碼
經常使用循環函數
for(i<-1 to 10 if i%2==0){ // if做用在前面生成的列表基礎上
println(i)
}
val courses = Array("spark sql","spark streaming","storm","scala")
for(course<-courses){
println(course)
}
//course其實就是courses裏面的每一個元素
//==> 就是將左邊的couse做用上一個函數,變成另一個結果
// println 就是做用到course.上的一個函數
courses.foreach(course => println(course))
var (num,sum) = (100, 0)
while (num>0){
sum+=num
num -= 1
}
println(num)
複製代碼