1,python中的map,reduce,filter使用
>>> a=[1,2,3,5] >>> map(lambda x:x+x,a) [2, 4, 6, 10] >>> map(lambda x:2*x,a) [2, 4, 6, 10] >>> reduce(lambda x,y:x*y,a) 30 >>> filter(lambda x : x%2,a) [1, 3, 5]
2,scala中的map,reduce,filter使用
scala> val list2 = 1 to 10 toList warning: there were 1 feature warning(s); re-run with -feature for details list2: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) scala> list2.map(x=>x*x) res13: List[Int] = List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100) scala> list2.map(math.pow(_,2)) res14: List[Double] = List(1.0, 4.0, 9.0, 16.0, 25.0, 36.0, 49.0, 64.0, 81.0, 10 0.0) scala> val sum = (x:Int, y:Int) => {x + y} sum: (Int, Int) => Int = <function2> scala> list2.reduce(sum) res15: Int = 55 scala> list2.reduce(_+_) res16: Int = 55 scala> list2.sum res17: Int = 55 scala> list2.filter( _ % 2 != 0) res19: List[Int] = List(1, 3, 5, 7, 9) scala> list2.filterNot( _ % 2 != 0) res20: List[Int] = List(2, 4, 6, 8, 10)