85th: The powerful expressive battle of the for expression in Scala

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.