Scala Tips: case class

The case class is one of the most important for data wrangle . Here some use of the case class using Scala comprehensions such as for comprehension.

Here the implementation:

Object Model

case class Member (name: String, age: Int)                                       // defined class Member                                                                                                                                                                                           
case class Family (members: Seq[Member])                                         // defined class Family                                                                                                                                                                                           
                                                                                 //                                                                                                                                                                                                                
val father1 : Member = Member("F1", 40)                                          // father1: Member = Member(F1,40)                                                                                                                                                                                
val mother1 : Member = Member("M1", 30)                                          // mother1: Member = Member(M1,30)                                                                                                                                                                                
val son1 = Member("S1",10)                                                       // son1: Member = Member(S1,10)                                                                                                                                                                                   
val daughter1 = Member("D1",9)                                                   // daughter1: Member = Member(D1,9)                                                                                                                                                                               
                                                                                 //                                                                                                                                                                                                                
val family1 = Family(List(father1,mother1,son1,daughter1))                       // family1: Family = Family(List(Member(F1,40), Member(M1,30), Member(S1,10), Member(D1,9)))                                                                                                                      
                                                                                 //                                                                                                                                                                                                                
val father2 : Member = Member("F2", 40)                                          // father2: Member = Member(F2,40)                                                                                                                                                                                
val mother2 : Member = Member("M2", 30)                                          // mother2: Member = Member(M2,30)                                                                                                                                                                                
val son2 = Member("S2",10)                                                       // son2: Member = Member(S2,10)                                                                                                                                                                                   
val daughter2 = Member("D2",9)                                                   // daughter2: Member = Member(D2,9)                                                                                                                                                                               
                                                                                 //                                                                                                                                                                                                                
val family2 = Family(List(father2,mother2,son2,daughter2))                       // family2: Family = Family(List(Member(F2,40), Member(M2,30), Member(S2,10), Member(D2,9)))                                                                                                                      
                                                                                 //                                                                                                                                                                                                                
//get the names of the family members                                            //                                                                                                                                                                                                                
family1.members.map(_.name)                                                      // res0: Seq[String] = List(F1, M1, S1, D1)                                                                                                                                                                       
family2.members.map(_.name)                                                      // res1: Seq[String] = List(F2, M2, S2, D2)                                                                                                                                                                       
                                                                                 //                                                                                                                                                                                                                
val families = Seq(family1, family2)                                             // families: Seq[Family] = List(Family(List(Member(F1,40), Member(M1,30), Member(S1,10), Member(D1,9))), Family(List(Member(F2,40), Member(M2,30), Member(S2,10), Member(D2,9))))                                 
                                                                                 //                                                                                                                                                                                                                
//to get names of both the families                                              //                                                                                                                                                                                                                
families.map(_.members.map(_.name))                                              // res2: Seq[Seq[String]] = List(List(F1, M1, S1, D1), List(F2, M2, S2, D2))                                                                                                                                      
                                                                                 //                                                                                                                                                                                                                
//flatten both the families                                                      //                                                                                                                                                                                                                
families.map(_.members.map(_.name)).flatten                                      // res3: Seq[String] = List(F1, M1, S1, D1, F2, M2, S2, D2)                                                                                                                                                       
// or                                                                            //                                                                                                                                                                                                                
families.flatMap(_.members.map(_.name))                                          // res4: Seq[String] = List(F1, M1, S1, D1, F2, M2, S2, D2)                                                                                                                                                       
                                                                                 //                                                                                                                                                                                                                
//Lets filter                                                                    //                                                                                                                                                                                                                
families.flatMap(_.members.filter(_.age > 10)).map(_.age)                        // res5: Seq[Int] = List(40, 30, 40, 30)                                                                                                                                                                          
                                                                                 //                                                                                                                                                                                                                
//for comprehension                                                              //                                                                                                                                                                                                                
for {                                                                            // Family is : Family(List(Member(F1,40), Member(M1,30), Member(S1,10), Member(D1,9)))Family is : Family(List(Member(F2,40), Member(M2,30), Member(S2,10), Member(D2,9)))res6: Seq[String] = List(F1, M1, F2, M2) 
  family <- families                                                             //                                                                                                                                                                                                                
  //debug family                                                                 //                                                                                                                                                                                                                
  _ = print(s"Family is : ${family}")                                            //                                                                                                                                                                                                                
  member <- family.members                                                       //                                                                                                                                                                                                                
  if member.age > 10                                                             //                                                                                                                                                                                                                
} yield member.name                                                              //                                                                                                                                                                                                                
                                                                                 //                                                                                                                                                                                                                
//or                                                                             //                                                                                                                                                                                                                
                                                                                 //                                                                                                                                                                                                                
//Option                                                                         //                                                                                                                                                                                                                
families.find(_.members.exists(_.age > 10))                                      // res7: Option[Family] = Some(Family(List(Member(F1,40), Member(M1,30), Member(S1,10), Member(D1,9))))                                                                                                           
for {                                                                            // res8: Option[String] = Some(F1)                                                                                                                                                                                
  // at least one member of the family should satisfy this condition to select   //                                                                                                                                                                                                                
  family <- families.find(_.members.exists(_.age > 10))                          //                                                                                                                                                                                                                
  member <- family.members.find(_.age > 10)                                      //                                                                                                                                                                                                                
} yield member.name                                                              //                                                                                                                                                                                                                
                                                                                 //                                                                                                                                                                                                                
//if you need Seq from the Option                                                //                                                                                                                                                                                                                
families.find(_.members.exists(_.age > 10)).toSeq                                // res9: Seq[Family] = List(Family(List(Member(F1,40), Member(M1,30), Member(S1,10), Member(D1,9))))                                                                                                              
                                                                                 //                                                                                                                                                                                                                
                                                                                 //                                                                                                                                                                                                                

These are the notes created from the video tutorial1. I would like to recommend this for beginners who want more details.

Reference


  1. Scala Beginner Programming Recipes, by Antonio Salazar Cardozo, Published by Packt, Publishing, 2017 

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python