I have a list of Tuple-2 like this:
val stuff = List( ("!thing","value"), ("otherthing","value") )
I want to pass over this list and create objects from the contents like this:
val processed = stuff.map{ case (label, value) =>
if(label.startsWith("!"))
BigThing(label.tail,value) // extends trait Thing
else
LittleThing(label,value) . // extends trait Thing
}
So far so good. Now the question...
While I'm mapping over these tuples, if the label starts with "!", I also want to create a Bonus(label.tail) object (also extends trait Thing) and add this object to the resulting list. This means that an input list of n items may result a list of >n items out. What's the most streamlined functional solution to this?
The final output desired is an expanded list of Thing. So the example above would theoretically produce (order unimportant):
List( BigThing("thing","value"), Bonus("thing"), LittleThing("otherthing","value") )