Pushing a series of Scala map operations into a separate list -


was tinkering translating java module scala , thought learning exercise on maps/placeholders... there way translate essence of code block single (map?) statement? (this i've come far 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:

  1. a list of dataprocessor(s) iterated
  2. from each dp, string extracted (call "alpha")
  3. alphpa used look-up key (externally defined) hashmap, gets/returns sql-prep'd value (call "zed")
  4. alpha , zed paired , appended own list/hash/set/whatever (called "sqlcolumnvaluepair")

those lines have been expanded readability; , portions distilled down using various map-ings (for example 1 & 2), there way in using single statment? feels should able condense down far...but don't have enough scala experience map-ings/placeholders know if fruitless endeavor, or not.

tia.

(note: found answer post pretty educational summary on scala's map operations: functional programming, scala map , fold left)

edit: removed .getorelse() operation original example, think distraction core problem.

edit 2: after consideration, maybe better way phrase learning objective here be: "how duplicate functionality using fewest number of operations." (which had assumed combination of map operations, partial functions, , forth.) , in doing so, gain broader understanding of set of scala classes/operators can used in similar situations.

you comprehension (and idiomatic way it):

for {   processor <- dataproclist   fieldname = processor.getwritecolumn   fieldvalue <- hashmapoffieldvaluepairs.get(fieldname) } yield (fieldname -> processor.getsqlvalue(fieldvalue)) 

this amounts to: each processor, write column, in field-value hashmap (get option), each value in option (either 1 or 0 values), yield (roughly: add growing sequence of result values) pair fieldname , corresponding sqlvalue, if any.

note in case, there no getorelse call, such - if there isn't entry in hash map given field name, no (fieldname, sqlvalue) pair generated processor. final result have values filtered out (which may or may not match semantics after).


Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -