`

Scala High-Order Methods

 
阅读更多

原创转载请注明出处:http://agilestyle.iteye.com/blog/2335327

 

map

The map method of a Scala collection applies its input function to all the elements in the collection and

returns another collection. The returned collection has the exact same number of elements as the collection

on which map was called. However, the elements in the returned collection need not be of the same type as

that in the original collection.

package org.fool.scala.higherordermethods

object HighOrderMethodsWithMap extends App {
  val xs = List(1, 2, 3, 4)
  val ys = xs.map((x: Int) => x * 10.0)
  val zs = xs.map { (x: Int) => x * 10.0 }
  val ws = xs map { (x: Int) => x * 10.0 }
  val vs = xs map { x => x * 10.0 }
  val us = xs map { _ * 10.0 }
  val ts = xs.map(_ * 10.0)

  println(ys)
  println(zs)
  println(ws)
  println(vs)
  println(us)
  println(ts)
}

Console Output


 

flatMap

The flatMap method of a Scala collection is similar to map. It takes a function as input, applies it to each

element in a collection, and returns another collection as a result. However, the function passed to flatMap

generates a collection for each element in the original collection. Thus, the result of applying the input

function is a collection of collections. If the same input function were passed to the map method, it would

return a collection of collections. The flatMap method instead returns a flattened collection.

package org.fool.scala.higherordermethods

object HighOrderMethodsWithFlatMap extends App {
  val line = "You are fucking bitch"
  val words = line.split(" ")

  println(words.length)
  words.foreach(println)

  val arrayOfChars = words.flatMap(_.toList)

  println(arrayOfChars.length)
  arrayOfChars.foreach(x => print(x + " "))
}

Console Output


 

filter

The filter method applies a predicate to each element in a collection and returns another collection consisting of only those elements for which the predicate returned true. A predicate is function that returns a

Boolean value. It returns either true or false.

package org.fool.scala.higherordermethods

object HighOrderMethodsWithFilter extends App {
  val xs = (1 to 100).toList
  val even = xs.filter(_ % 2 == 0)

  println(even)
}

Console Output


 

foreach

The foreach method of a Scala collection calls its input function on each element of the collection, but does

not return anything. It is similar to the map method. The only difference between the two methods is that map returns a collection and foreach does not return anything. It is a rare method that is used for its side effects. 

package org.fool.scala.higherordermethods

object HighOrderMethodsWithForEach extends App {
  val line = "You are a fucking bitch"
  val words = line.split(" ")
  words.foreach(println)
}

Console Output


 

reduce

The reduce method returns a single value. As the name implies, it reduces a collection to a single value. The input function to the reduce method takes two inputs at a time and returns one value. Essentially, the input function is a binary operator that must be both associative and commutative.

package org.fool.scala.higherordermethods

object HighOrderMethodsWithReduce extends App {
  val xs = List(2, 4, 6, 8, 10)
  val sum = xs.reduce((x, y) => x + y)
  val coolSum = xs.reduce(_ + _)
  println(sum)
  println(coolSum)

  val product = xs.reduce((x, y) => x * y)
  val coolProduct = xs.reduce(_ * _)
  println(product)
  println(coolProduct)

  val max = xs.reduce((x, y) => if (x > y) x else y)
  println(max)
  val min = xs.reduce((x, y) => if (x < y) x else y)
  println(min)

  val line = "You are a fucking bitch"
  val longestWord = line.split(" ").reduce((w1, w2) => if (w1.length > w2.length) w1 else w2)
  println(longestWord)
}

Console Output


 

Reference

Apress.Big.Data.Analytics.with.Spark.A.Practitioners.Guide.to.Using.Spark.for.Large.Scale.Data.Analysis.2015

 

  • 大小: 16.9 KB
  • 大小: 15.3 KB
  • 大小: 13.7 KB
  • 大小: 13.5 KB
  • 大小: 14.8 KB
分享到:
评论

相关推荐

    scala-parser-combinators-2.11-1.0.4-API文档-中文版.zip

    赠送jar包:scala-parser-combinators_2.11-1.0.4.jar; 赠送原API文档:scala-parser-combinators_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-parser-combinators_2.11-1.0.4-sources.jar; 赠送Maven依赖信息...

    scala-compiler-2.11.8-API文档-中英对照版.zip

    赠送jar包:scala-compiler-2.11.8.jar; 赠送原API文档:scala-compiler-2.11.8-javadoc.jar; 赠送源代码:scala-compiler-2.11.8-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.11.8.pom; 包含翻译后...

    scala-parser-combinators_2.12-1.1.0-API文档-中英对照版.zip

    赠送jar包:scala-parser-combinators_2.12-1.1.0.jar; 赠送原API文档:scala-parser-combinators_2.12-1.1.0-javadoc.jar; 赠送源代码:scala-parser-combinators_2.12-1.1.0-sources.jar; 赠送Maven依赖信息...

    scala-intellij-bin-2016.3.9

    "scala-intellij-bin-2016.3.9"是针对Scala语言的一个特定版本的IntelliJ IDEA插件,该版本为2016.3.9。这个插件是专门为Scala开发者设计的,旨在提高他们在IntelliJ IDEA中的开发体验。 Scala是一种多范式编程语言...

    scala-parser-combinators_2.11-1.0.4-API文档-中英对照版.zip

    赠送jar包:scala-parser-combinators_2.11-1.0.4.jar; 赠送原API文档:scala-parser-combinators_2.11-1.0.4-javadoc.jar; 赠送源代码:scala-parser-combinators_2.11-1.0.4-sources.jar; 包含翻译后的API...

    scala-java8-compat_2.11-0.7.0-API文档-中英对照版.zip

    赠送jar包:scala-java8-compat_2.11-0.7.0.jar; 赠送原API文档:scala-java8-compat_2.11-0.7.0-javadoc.jar; 赠送源代码:scala-java8-compat_2.11-0.7.0-sources.jar; 赠送Maven依赖信息文件:scala-java8-...

    scala-compiler-2.11.12-API文档-中文版.zip

    赠送jar包:scala-compiler-2.11.12.jar; 赠送原API文档:scala-compiler-2.11.12-javadoc.jar; 赠送源代码:scala-compiler-2.11.12-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.11.12.pom; 包含...

    scala-intellij-bin-2018.3.2.zip

    scala-intellij-bin-2018.3.2.zip插件,亲测可用!!!scala-intellij-bin-2018.3.2.zip插件,亲测可用!!!scala-intellij-bin-2018.3.2.zip插件,亲测可用!!!

    scala-ide-plugin-eclipse

    scala eclipse插件.对应scala版本:2.10--2.11,对应eclipes版本:4.4--...update site:http://download.scala-ide.org/sdk/lithium/e44/scala211/stable/site 下载地址:http://scala-ide.org/download/current.html

    scala-java8-compat_2.11-0.7.0-API文档-中文版.zip

    赠送jar包:scala-java8-compat_2.11-0.7.0.jar; 赠送原API文档:scala-java8-compat_2.11-0.7.0-javadoc.jar; 赠送源代码:scala-java8-compat_2.11-0.7.0-sources.jar; 赠送Maven依赖信息文件:scala-java8-...

    scala-intellij-bin-0.41

    "scala-intellij-bin-0.41"是专门为IntelliJ IDEA设计的一个Scala插件,版本号为0.41,用于增强IDE对Scala语言的支持。 这个插件的安装和使用对于Scala开发者至关重要,因为它可以提供以下关键功能: 1. 语法高亮...

    scala-library-2.11.8-API文档-中文版.zip

    赠送jar包:scala-library-2.11.8.jar; 赠送原API文档:scala-library-2.11.8-javadoc.jar; 赠送源代码:scala-library-2.11.8-sources.jar; 赠送Maven依赖信息文件:scala-library-2.11.8.pom; 包含翻译后的API...

    scala-compiler-2.12.7-API文档-中英对照版.zip

    赠送jar包:scala-compiler-2.12.7.jar; 赠送原API文档:scala-compiler-2.12.7-javadoc.jar; 赠送源代码:scala-compiler-2.12.7-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.12.7.pom; 包含翻译后...

    scala-reflect-2.11.8-API文档-中英对照版.zip

    赠送jar包:scala-reflect-2.11.8.jar; 赠送原API文档:scala-reflect-2.11.8-javadoc.jar; 赠送源代码:scala-reflect-2.11.8-sources.jar; 赠送Maven依赖信息文件:scala-reflect-2.11.8.pom; 包含翻译后的API...

    scala-SDK-4.7.0-vfinal-2.12-linux.gtk.x86_64.tar.gz

    scala-SDK-4.7.0-vfinal-2.12-linux.gtk.x86_64.tar.gz scala-SDK-4.7.0-vfinal-2.12-linux.gtk.x86_64.tar.gz

    scala-intellij-bin-2020.2.3.zip

    "scala-intellij-bin-2020.2.3.zip" 是一个包含IntelliJ IDEA的Scala插件的压缩包,特别针对Scala开发进行了优化。 该插件允许开发者在IntelliJ IDEA中高效地编写和调试Scala代码。以下是关于Scala和IntelliJ IDEA ...

    scala-high-performance-programming.pdf

    scala-high-performance-programming.pdf

    scala-compiler-2.11.0-API文档-中文版.zip

    赠送jar包:scala-compiler-2.11.0.jar; 赠送原API文档:scala-compiler-2.11.0-javadoc.jar; 赠送源代码:scala-compiler-2.11.0-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.11.0.pom; 包含翻译后...

    scala-intellij-bin-2021.3.6.zip

    "scala-intellij-bin-2021.3.6.zip"是一个压缩包,包含了用于在IntelliJ IDEA中支持Scala开发的特定版本插件。 这个版本的Scala插件(2021.3.6)是为IntelliJ IDEA 2021.3系列构建的,它提供了丰富的功能,以帮助...

    scala-compiler-2.12.7-API文档-中文版.zip

    赠送jar包:scala-compiler-2.12.7.jar; 赠送原API文档:scala-compiler-2.12.7-javadoc.jar; 赠送源代码:scala-compiler-2.12.7-sources.jar; 赠送Maven依赖信息文件:scala-compiler-2.12.7.pom; 包含翻译后...

Global site tag (gtag.js) - Google Analytics