I have a list of books:
case class Book(name:String,authors:List[String])
val books:List[Book] = List(
Book(name="Code in Scala",authors=List("Viny","Vinay")),
Book(name="Dance in 30 days",authors=List("Namratha","Namitha")),
Book(name="Cook in 30 days",authors=List("Pavan")),
Book(name="Gym in 30 days",authors=List("Nisanth","Vinay"))
)
Now i want to know the books where author name starts with "Vin". I have implemented this in for loop like below:
for(b<-books ; a <- b.authors ; if a.startsWith("Vin")) yield b.name
But i am unable to implement this with higher order function. I tried as below:
books flatMap (b=> b.authors.withFilter(a=>a.startsWith("Vin")).map(x=>x))
This gets me the Name of the authors but i am unable to access the book object.how can i resolve this? Main goal here is to convert/translate the "for loops" into higher order functions(flatmap/filter/map)