I am having trouble with understanding how to convert this into a for-comprehension. Could someone please explain how this can be done?
(parseCommand(x)
.flatMap { c =>
calculate(c).map(r => renderResult(c, r))
})
.left
.map(e => e.value)
.merge
```
CodePudding user response:
You haven't provided enough information to answer your question.
- What type does
parseCommand()
return? - What type does
calculate()
return? - Why translate to a
for
comprehension? What's your goal?
Assuming that parseCommand()
and calculate()
return the same or compatible types, the the 1st map()
and flatMap()
can be translated like so:
(for {
c <- parseCommand(x)
r <- calculate(c)
} yield renderResult(c, r)
).left
.map(e => e.value)
.merge
...
The 2nd map()
can't be folded in to this for
because there can be only one map()
per for
comprehension. You could, however, turn it into its own, nested, for
comprehension, but remember that it makes no difference to the compiler, so the only real reason to use for
is to enhance code readability, and nested for
s seldom achieve that.
CodePudding user response:
In the below link, you may find some helpful information about converting flatMap/map into For comprehension :
CodePudding user response:
I managed to convert it like this:
(for {
c <- parseCommand(x)
r <- calculate(c)
} yield renderResult(c, r)).left.map(x => x.value).merge
}