Skip to content

Instantly share code, notes, and snippets.

Created November 24, 2012 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4141197 to your computer and use it in GitHub Desktop.
Save anonymous/4141197 to your computer and use it in GitHub Desktop.
scala map
object testests {
def main(args: Array[String]): Unit = {
def mapper:Int=>Int= x=>x*x
val s= Seq( -2,-2,2,2 )
val themax= s.map(mapper).max
val tmp= s.filter( mapper(_)==themax)
val tmp2= s.foldLeft(Seq[Int]())((res, elem) => {val e = mapper(elem); if(res.isEmpty || e>res.head) Seq(e) else if (e== res.head) res++Seq(e) else res })
val mappedS = s.map(mapper) //map done only once
val m = mappedS.max // find the max
val tmp3 = mappedS.filter(_ == m) // keep only maximal elements
println(tmp)
println(tmp2)
println(tmp3)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment