Last active
December 20, 2015 21:19
-
-
Save lu911/6196865 to your computer and use it in GitHub Desktop.
Scala Function Mapping
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
import scala.collection.mutable.ArrayBuffer | |
import scala.collection.mutable.LinkedList | |
import scala.collection.mutable.BitSet | |
import java.util.LinkedHashSet | |
object Main { | |
def main(args: Array[String]): Unit = { | |
new FunctionMapping() | |
} | |
} | |
class FunctionMapping { | |
val list = List("a", "b", "c", "d", "e", "f", "g") | |
val methods = List(map, flatMap, collect, foreach) | |
def ulcase(s: String) = List(s.toUpperCase(), s.toLowerCase()) | |
def map(): Unit = { | |
println("-"*20 + " list.map(ulcase) " + "-"*20) | |
println(list.map(ulcase)) | |
} | |
def flatMap(): Unit = { | |
println("-"*20 + " list.flatMap(ulcase) " + "-"*20) | |
println(list.flatMap(ulcase)) | |
} | |
def collect(): Unit = { | |
println("-"*20 + " list.collect(function) " + "-"*20) | |
println(list.collect{ case "a" => "A"; case "b" => "B"; case "c" => "C"; case "d" => "D"; case "e" => "E"; case "f" => "F"; case "g" => "G" }) | |
} | |
def foreach(): Unit = { | |
println("-"*20 + " list.foreach(println) " + "-"*20) | |
println(list.foreach(println)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment