Home > Software engineering >  Scala filtering through nested Seq Collection
Scala filtering through nested Seq Collection

Time:09-21

I'm trying to filter through a nested Seq collection.

The aim is to iterate through the myList Seq collection while identifying all 'Campaigns' in the Seq collection whose country is 'UK' and its banner has a width and height value as defined by the parameters h and w below.

c defines the country filter parameter.

So far the filtering code I've got below gives me the error:

error: value height is not a member of List[Banner]

val filteredList = myList.filter(c=> ((c.country == c) && (c.banners.height ==h) && (c.banners.width == w)))

Thanks in advance!

val h = 250 // height filter
val w = 300 // width filter
val c = "UK" // country filter

Sample of the Seq collection:

case class Campaign(id: Int, country: String, targeting: Targeting, banners: List[Banner], bid: Double)
case class Targeting(targetedSiteIds: Seq[String])
case class Banner(id: Int, src: String, width: Int, height: Int)

val myList = Seq(
                  Campaign( // Campaign 1
                      id=1, 
                      country="LT", 
                      targeting =Targeting( 
                          targetedSiteIds=Seq("0006a522ce0f4bbbbaa6b3c38cafaa0f") // TargetedSiteIds
                          ),
                      banners = List( 
                            Banner( 
                                id = 1, 
                                src = "https://business.eskimi.com/wp-content/uploads/2020/06/openGraph.jpeg", // URL source
                                width = 100, 
                                height = 200 
                                )
                            ),
                        bid = 5d 
                        ),
                 Campaign( // Campaign 2
                      id=1, 
                      country="UK", 
                      targeting =Targeting( 
                          targetedSiteIds=Seq("0006a522ce0f4bbbbaa6b3c38cafaa0f") // TargetedSiteIds
                          ),
                      banners = List( 
                            Banner( 
                                id = 1, 
                                src = "https://business.eskimi.com/wp-content/uploads/2020/06/openGraph.jpeg", // URL source
                                width = 300, 
                                height = 250 
                                )
                            ),
                        bid = 5d 
                        )
                    )

val filteredList = myList.filter(c=> ((c.country == c) && (c.banners.height ==h) && (c.banners.width == w))) 

CodePudding user response:

val filteredList =
  myList.filter(c => c.country == "UK" &&
                     c.banners.exists(_.height == 250)  &&
                     c.banners.exists(_.width == 300))

Or, probably more correct.

val filteredList =
  myList.filter(c => c.country == "UK" &&
                     c.banners.exists(b => b.height == 250 &&
                                           b.width == 300))
  • Related