Suppose I have two functions to get orders and order items:
def getOrders(): Option[List[Int]] = ... def getOrderItems(orderId: Int): Option[List[Int]] = ...
Note that both functions return Option[List]
since each function may fail.
Now I would like to get Option
of List
of all order items as follows:
- return
Some[List]
if both functions returnSome
and None
if any of them returns None.
I tried to compose these functions with for
(see below) but it did not work.
val allOrderItems = for { orderIds <- getOrders(); orderId <- orderIds; orderItems <- getOrderItems(orderId) } yield orderItems
How can I build a function getAllOrderItems():Option[List[Int]]
using functions getOrders
and getOrderItems
?
monad transformer
looks like an advanced topic. Besides, I would needscalaz
for that. How would you suggest do that withbreakOut
? – MichaeltoList
looks really simple. – Michael