Was tinkering about translating a Java module into Scala and thought this would be a good learning exercise on Maps/Placeholders... Is there a way to translate the essence of this code block into single (map?) statement? (This is what I've come up with so far for a Scala-ized version)
dataProcList foreach (processor => { // #1
val fieldName: String = processor.getWriteColumn // #2
val sqlValue: String = processor.getSqlValue(hashMapOfFieldValuePairs.get(fieldName)) // #3
sqlColumnValuePair += ((fieldName, sqlValue)) // #4
})
In short:
- A list of DataProcessor(s) is iterated
- From each DP, a string is extracted (call it "Alpha")
- Alphpa is used as the look-up key to a (externally defined) HashMap, which gets/returns a SQL-prep'd value (call it "Zed")
- Alpha and Zed are then paired and appended to their own List/Hash/Set/Whatever (called "sqlColumnValuePair")
Those lines have been expanded for readability; and portions could be distilled down using various map-ings (for example 1 & 2), but is there a way to do it in using a single statment? It feels like I should be able to condense it down that far...but I don't have enough Scala experience with Map-ings/placeholders to know if it is a fruitless endeavor, or not.
TIA.
(NOTE: I found the answer from this post to be a pretty educational summary on Scala's Map operations: Functional programming, Scala map and fold left)
EDIT: I removed a .getOrElse() operation from the original example, as I think it was just a distraction to the core problem.
EDIT 2: After some consideration, maybe a better way to phrase my learning objective here would be: "How to duplicate this functionality using the fewest number of operations." (Which I had assumed would be some combination of map operations, partial functions, and so forth.) And in doing so, hopefully gain a broader understanding of the set of Scala classes/operators that can be used in similar situations.