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

猜你喜欢

转载自agilestyle.iteye.com/blog/2335327