Today "DT Big Data DreamWorks Video" 85th: The powerful expression of the for expressions in Scala
51CTO Video: http://edu.51cto.com/lesson/id-71503.html
(DT Big Data Dream factory Scala all videos, PPT and code in Baidu Cloud Disk Link: Http://url.cn/fSFPjS)
85 Talk about the power of Scala for expressions
The behavior of the higher-order function specifies the details of the data processing.
Case class Person (name:string,ismale:boolean,children:person*)//children variable parameter
Object for_expressive{
def main (args:array[string]) {
Val Lauren=person ("Lauren", false)//girl
Val Rocky=person ("Rocky", true)//Boy
Val Vivian=person ("Vivian", False,lauren,rocky)//Mom
Val persons=list (Lauren,rocky,vivian)//person list
Find a woman with a baby.
Val result=persons filter (person =!person.ismale) FlatMap (person=>
(Person.children map (child=> (person.name,child.name)))) Each child returns the mother's name and the baby's name
Flatmap is to synthesize several objects into one
List ((Vivian,lauren), (Vivian,rocky))
println (Result)
Val forresult=for (Person<-persons;if!person.ismale;child<-person.children)
Yield (person.name,child.name)
println (Forresult)
List ((Vivian,lauren), (Vivian,rocky))
The same output, but for more concise.
Actually the for is in map mode.
This is the way to convert the for compilation to the previous one.
For just to make the wording concise.
}
}
85th: The powerful expressive battle of the for expression in Scala