原创文章,转载请注明出处!

原文地址: http://www.ptbird.cn/2016/07/19/scala-chuji-demo-22/

There I am,in the world more exciting!

by postbird

www.ptbird.cn



package com.ptbird.scala /** * Created by postbird on 2016/5/26. */ object HighLevelFuncTest { def main(args: Array[String]): Unit = { (1 to 9).map("*" * _).foreach(println _) // * // ** // *** // **** // ***** // ****** // ******* // ******** // ********* (1 to 9).filter(_ % 2 == 0).foreach(println) // 2 // 4 // 6 // 8 println((1 to 9).reduceLeft(_ + _)) //45 从右向左 "There I am , in the world more exciting !".split(" ").sortWith(_.length < _.length).foreach(println _) // I // , // ! // am // in // the // more // There // world // exciting /** **************************************将函数作为参数进行传递 ***********************************************/ //函数是一等公民 val triple = (x: Int) => { x * 3 } Array(1, 2, 3).map((x: Int) => { 3 * x }).foreach(print) //369 Array(1, 2, 3).map(triple(_)).foreach(print) //369 def high_order_function(f: (Int) => Int): Int = f(12) println(high_order_function(triple)) //36 //将triple函数作为参数传递给函数,第一个int是传递的参数的返回类型 println(high_order_function((x) => x + 1)) println(high_order_function(1 + _)) //13 } }