diff --git a/pom.xml b/pom.xml index d23abab..e14d1c0 100644 --- a/pom.xml +++ b/pom.xml @@ -1,73 +1,88 @@ - - 4.0.0 - top.fjy8018 - helloscala - 1.0-SNAPSHOT - ${project.artifactId} - My wonderfull scala app - 2010 - - - My License - http://.... - repo - - + + 4.0.0 + top.fjy8018 + helloscala + 1.0-SNAPSHOT + ${project.artifactId} + My wonderfull scala app + 2010 + + + My License + http://.... + repo + + - - 1.8 - 1.8 - UTF-8 - 2.11.8 - + + 1.8 + 1.8 + UTF-8 + 2.11.8 + 2.3.1 + - - - org.scala-lang - scala-library - ${scala.version} - - + + + org.scala-lang + scala-library + ${scala.version} + - - src/main/scala - src/test/scala - - - org.scala-tools - maven-scala-plugin - 2.15.0 - - - - compile - testCompile - - - - -make:transitive - -dependencyfile - ${project.build.directory}/.scala_dependencies - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - 2.6 - - false - true - - - - **/*Test.* - **/*Suite.* - - - - - + + org.apache.spark + spark-core_2.11 + ${spark.version} + + + + + org.apache.spark + spark-sql_2.11 + 2.3.1 + + + + + + src/main/scala + src/test/scala + + + org.scala-tools + maven-scala-plugin + 2.15.0 + + + + compile + testCompile + + + + -dependencyfile + ${project.build.directory}/.scala_dependencies + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.6 + + false + true + + + + **/*Test.* + **/*Suite.* + + + + + diff --git a/src/main/scala/top/fjy8018/scala/FunctionApp.scala b/src/main/scala/top/fjy8018/scala/FunctionApp.scala index bd7cf41..fb21b93 100644 --- a/src/main/scala/top/fjy8018/scala/FunctionApp.scala +++ b/src/main/scala/top/fjy8018/scala/FunctionApp.scala @@ -12,6 +12,17 @@ object FunctionApp { // 调用无参方法可以不加括号 printHello + + printHello("scala") + + // 调用带默认值的方法不可以不加括号 + defaultParam() + defaultParam("new value") + + // 支持自定义参数顺序 + println(sum(y = 2,x = 1)) + + printAllInt(1,2,3,4,5,6) } def add(x:Int,y:Int):Int={ @@ -29,4 +40,27 @@ object FunctionApp { def printHello()= println("Hello world!") + /** + * 支持函数重载 + * @param n + */ + def printHello(n:String)=println("Hello world " + n) + + /** + * 支持参数默认值 + * @param n + */ + def defaultParam(n:String="defaultValue")=println("value:" + n) + + /** + * 可变参数 + * @see org.apache.spark.sql.Dataset#select(org.apache.spark.sql.TypedColumn, org.apache.spark.sql.TypedColumn, org.apache.spark.sql.TypedColumn, org.apache.spark.sql.TypedColumn, org.apache.spark.sql.TypedColumn) + * @param numbers + */ + def printAllInt(numbers:Int*): Unit ={ + for (number <- numbers){ + print(number +",") + } + } + } diff --git a/src/main/scala/top/fjy8018/scala/Loop.scala b/src/main/scala/top/fjy8018/scala/Loop.scala new file mode 100644 index 0000000..737ae8b --- /dev/null +++ b/src/main/scala/top/fjy8018/scala/Loop.scala @@ -0,0 +1,70 @@ +package top.fjy8018.scala + +/** + * F嘉阳 + * 2018-11-26 17:03 + */ +object Loop { + val list = Array("a","b","c","d") + + def main(args: Array[String]): Unit = { + printTest + println() + loop1 + println() + loop2() + println() + loop3 + println() + rangeTest1 + rangeTest2 + sumTest + } + + def printTest()=print(1 to 10) + + def loop1(): Unit ={ + for (l <- list){ + print(l) + } + } + + /** + * 类似java lambda函数表达式 + */ + def loop2(): Unit ={ + list.foreach(l => print(l)) + } + + /** + * 带条件循环表达式 + */ + def loop3(): Unit ={ + for (i<- 1 to 10 if i%2==0){ + print(i+" ") + } + } + + def rangeTest1(): Unit ={ + // 指定步长,Range为to函数的底层实现 + print(Range(1,10,2)) + println() + println(Range(10,1,-3)) + } + + /** + * until 左闭右开,to左闭右闭 + */ + def rangeTest2(): Unit ={ + println(1 until 10) + } + + def sumTest(): Unit ={ + var (num,sum) = (100,0) + while (num>0){ + sum += num + num -= 1 + } + println(sum) + } +}