Let's say I have a bunch of Widget, where a Widget is
class Widget (sku: Int, label: String, name: String, description: String)
And I have 2 sources of Widgets:
List[Widget] wInventory = getWidgetsInInventory()
List[Widget] wCatalog = getWidgetsFromCatalog()
I want to use the name & label from the Catalog to override any matching widget in Inventory. The match criteria is when sku == sku.
If the label is missing in the catalog, override with constant value of No Catalog Label instead of leaving label as is.
The wCatalog list may be empty. There may be no intersections at all, or both lists can contain the same set of sku's.
First draft of the code below. There must be a way to avoid running find twice, but because find returns Optional[Widget], it started looking messy with lots getOrElse. What's the cleanest way to do this?
case class MyWidget (sku: Int, label: String, name: String, description: String)
object HelloWorld {
def main(args: Array[String]) {
def setOverrideValues(inv : List[MyWidget], cat: List[MyWidget]) : List[MyWidget] = {
inv.map (k => {
val realName = cat.find( _.sku == k.sku).map(_.name)
val realLabel = cat.find( _.sku == k.sku).map(_.label)
k.copy(name = realName.getOrElse("No Catalog Label"), label=realLabel.getOrElse(k.label))
})
}
val invItem1 = new MyWidget(sku=1, label="invLabel1", name="invName1", description="invDesc1");
val invItem2 = new MyWidget(sku=2, label="invLabel2", name="invName2", description="invDesc2");
val catItem1 = new MyWidget(sku=1, label="catLabel1", name="catName1", description="catDesc1");
val wInventory = List(invItem1 , invItem2);
val wCatalog = List(catItem1)
val updated = setOverrideValues(wInventory, wCatalog)
println("done")
}
}
wCateogry? \$\endgroup\$wCatalog.namewill not compile, seeing aswCatalogis a list. \$\endgroup\$