collections - Functional style in Scala to collect results from a method -
i have 2 lists zip , go through zipped result , call function. function returns list of strings response. want collect responses , not want have sort of buffer collect responses each iteration.
seq1.zip(seq2).foreach((x: (obj1, obj1)) => { callmethod(x._1, x._2) // method returns seq of string when called }
what want avoid create listbuffer , keep collecting it. clues functionally?
why not use map() transform each input corresponding output ? here's map()
operating in simple scenario:
scala> val l = list(1,2,3,4,5) scala> l.map( x => x*2 ) res60: list[int] = list(2, 4, 6, 8, 10)
so in case like:
seq1.zip(seq2).map((x: (obj1, obj1)) => callmethod(x._1, x._2))
given function returns seq of strings, could use flatmap()
flatten results 1 sequence.
Comments
Post a Comment