Scala elegant way to convert a multiple optional objects to a different object if at least one of the objects defined

Johnny

The trivial approach (with if,else), is known. I'm thinking about how Scala can help me to do it in a more elegant way:

    def prepareData(baseObj: BaseObj): Option[NextObj] = {
      val maybeDataOne = Option(baseObj.getDataOne)
      val maybeDataTwo = Option(baseObj.getDataTwo)

      // return None if no DataOne or DataTwo defined
      // return Some(NextObj) if at least one of the Datas defined.

      // trivial solution:
      if(maybeDataOne.isDefined || maybeDataTwo.isDefined) {
        Some(NextObj(
          dataOne = baseObj.dataOne,
          dataTwo = baseObj.dataTwo
        ))
      } else None
    }

    //DataOne and DataTwo will be mapped to NextObj, if, at least one, is defined
    case class NextObj(d1: Option[DataOne], d2: Option[DataTwo])

Dima
   maybeDataOne orElse maybeDataTwo map { _ => nextObj }

One way to make this look even prettier is to equip your NextObject class with a .toOption method:

def toOption = d1 orElse d2 map { _ => this }

Then you can just write NextObject(maybeDataOe, maybeDataTwo).toOption at the call site.

Or maybe this:

   object NextObject {
       def opt(d1: Option[DataOne], d2: Option[DataTwo]) = 
          d1 orElse d2 map { _ => apply(d1, d2) }
   }

and then just NextObject.opt(maybeDataOne, maybeDataTwo)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Elegant way of fetching multiple objects in custom order

From Dev

Convert multiple JSON strings to different objects with one method

From Dev

convert an array of objects to one object of objects

From Dev

convert an object to multiple objects in javascript

From Dev

Elegant way to convert json to list of objects in Python 3

From Dev

The most elegant way to collect nonEmpty object fields from the list of objects

From Dev

Initializing many objects in an elegant way

From Dev

how to convert an object with objects in only one object

From Dev

Selecting multiple objects in sql and seeing if at least 1 has different value?

From Dev

How to convert an array of objects (of different lengths) into one object that has different properties from the elements in the original array?

From Dev

R Shiny: req() *at least one of* multiple objects/values

From Dev

Best way to convert array of objects into Object?

From Javascript

Convert and filter array of objects to one object

From Dev

Convert Array of Objects in to one comma separated Object

From Dev

Convert array of objects into an array of one object

From Dev

Convert Javascript array of objects into one object

From Dev

Is there a way to have one hammer event for multiple objects?

From Dev

How to convert an array with one object and multiple keys into an array of multiple objects using those keys and their values?

From Javascript

Convert object with multiple arrays to array of objects

From Dev

How to convert an object to array of multiple objects in javascript

From Dev

Convert a array of objects to a multiple keys of object

From Dev

How to convert multiple objects in an array to a new object?

From Dev

How to convert JSON object with multiple objects into a single set of objects

From Java

Map one object to multiple objects in java stream

From Dev

How to add one object into multiple objects -JOLT

From Dev

AutoMapper one source object to multiple destination objects

From Dev

how to push multiple objects into one object serially

From Dev

split one object into multiple objects based on the property

From Dev

How to split one object with an array in it to multiple objects

Related Related

  1. 1

    Elegant way of fetching multiple objects in custom order

  2. 2

    Convert multiple JSON strings to different objects with one method

  3. 3

    convert an array of objects to one object of objects

  4. 4

    convert an object to multiple objects in javascript

  5. 5

    Elegant way to convert json to list of objects in Python 3

  6. 6

    The most elegant way to collect nonEmpty object fields from the list of objects

  7. 7

    Initializing many objects in an elegant way

  8. 8

    how to convert an object with objects in only one object

  9. 9

    Selecting multiple objects in sql and seeing if at least 1 has different value?

  10. 10

    How to convert an array of objects (of different lengths) into one object that has different properties from the elements in the original array?

  11. 11

    R Shiny: req() *at least one of* multiple objects/values

  12. 12

    Best way to convert array of objects into Object?

  13. 13

    Convert and filter array of objects to one object

  14. 14

    Convert Array of Objects in to one comma separated Object

  15. 15

    Convert array of objects into an array of one object

  16. 16

    Convert Javascript array of objects into one object

  17. 17

    Is there a way to have one hammer event for multiple objects?

  18. 18

    How to convert an array with one object and multiple keys into an array of multiple objects using those keys and their values?

  19. 19

    Convert object with multiple arrays to array of objects

  20. 20

    How to convert an object to array of multiple objects in javascript

  21. 21

    Convert a array of objects to a multiple keys of object

  22. 22

    How to convert multiple objects in an array to a new object?

  23. 23

    How to convert JSON object with multiple objects into a single set of objects

  24. 24

    Map one object to multiple objects in java stream

  25. 25

    How to add one object into multiple objects -JOLT

  26. 26

    AutoMapper one source object to multiple destination objects

  27. 27

    how to push multiple objects into one object serially

  28. 28

    split one object into multiple objects based on the property

  29. 29

    How to split one object with an array in it to multiple objects

HotTag

Archive