Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract values from a list of IO actions

Tags:

haskell

monads

So I've got something like:

[IO Blah, IO Blah, IO Blah]

and I really want just a list of Blahs, how can I do it?

P.S.: Yes, I am working within an IO function.

like image 477
Andriy Drozdyuk Avatar asked Mar 22 '12 22:03

Andriy Drozdyuk


2 Answers

Use sequence.

Evaluate each action in the sequence from left to right, and collect the results.

do blahs <- sequence listOfIoBlah
   -- now use blahs
like image 113
dave4420 Avatar answered Oct 18 '22 19:10

dave4420


Hoogle can be very useful in answering these sort of questions, if you know how to formulate the question as a type, for example: http://www.haskell.org/hoogle/?hoogle=%5BIO+a%5D+-%3E+IO+%5Ba%5D

like image 28
Daniel Pratt Avatar answered Oct 18 '22 20:10

Daniel Pratt