Created
November 24, 2012 19:56
-
-
Save anonymous/4141197 to your computer and use it in GitHub Desktop.
scala map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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