Scala High-Order Methods

本文介绍了Scala中常用的集合操作方法,包括map、flatMap、filter、foreach和reduce等,并通过实例展示了这些方法的应用方式及其区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原创转载请注明出处: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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值