Skip to main content
The "join" tag refers to the SQL JOIN clause. This question is not about SQL.
Source Link
Mast
  • 13.9k
  • 12
  • 57
  • 128

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 Labellabel is missing in the catalog, override with constant value of "No Catalog Label"No Catalog Label instead of leaving Labellabel as is.
The wCatalogwCatalog 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]Optional[Widget], it started looking messy with lots getOrElsegetOrElse. 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")
  }
}

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")
  }
}

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")
  }
}
The "join" tag refers to the SQL JOIN clause. This question is not about SQL.
Link
edited tags
Link
200_success
  • 145.7k
  • 22
  • 192
  • 481
full working sample code
Source Link
Loading
wrong name - wCategory should be wCatalog
Source Link
Loading
Source Link
Loading