scala - Converting nested for loops into for comprehension -
i can collect results @ inner-most for body list[output] , homecoming them. want utilize yield. how can method converted using for-yield pattern:
def useforcomprehension(input : input): list[output] = { (o <- splitinputintopieces(input)) { (restresults <- useforcomprehension(subtract(input, o)) ) { (w <- f3(o)) { yield w::restresults // !!!!! error } } } }
in scala, nested iteration handled adding additional <- clauses.
for example, let's have 2 lists l1 , l2 , want generate on every pair of elements (x,y) x in l1 , y in l2. syntax in scala is:
for { x <- l1 y <- l2 } yield (x,y) when no yield keyword follows for entire look results in type of unit, source of type error. useful performing side effects on iteration, example
for { x <- l1 y <- l2 } println((x,y)) for more info on comprehensions see what scala's yield?
scala for-comprehension
No comments:
Post a Comment