get rid of IO in [IO XXX]

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

get rid of IO in [IO XXX]

info-2
What is the way to transform a list of [IO XXX] type to [XXX]?

Thanks,
Alexei
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: get rid of IO in [IO XXX]

Mihai Maruseac
Hi,

Use sequence. It has type t (m a) -> m (t a) where m is a Monad (like
IO) and t is a Traversable (like the list).

Documentation: http://hackage.haskell.org/package/base-4.10.0.0/docs/Prelude.html#v:sequence

I got to it using Hoogle:
https://www.haskell.org/hoogle/?hoogle=%5BIO+a%5D+-%3E+IO+%5Ba%5D

On Mon, Sep 4, 2017 at 9:04 AM,  <[hidden email]> wrote:
> What is the way to transform a list of [IO XXX] type to [XXX]?
>
> Thanks,
> Alexei
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



--
Mihai Maruseac (MM)
"If you can't solve a problem, then there's an easier problem you can
solve: find it." -- George Polya
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: get rid of IO in [IO XXX]

Alex Belanger
In reply to this post by info-2
You can turn `[IO a]` into `IO [a]` by traversing the Traversable and sequencing the actions using `sequence`.

Note that what it creates is a slightly different IO computation that re-organizes the results, you'll still need to run that IO in the end.

Typically, it's passed all the way down to your closest use of IO (often main for beginners) where you'll be able to finally get rid of it by performing the effects and doing something with the results. The flavor of choice depends on the situation but I find the IO monad very readable.

main :: IO ()
main = do
    listOfA <- sequence ioListOfA
    -- Use listOfA from here

Alex

On Sep 4, 2017 12:05 PM, <[hidden email]> wrote:
What is the way to transform a list of [IO XXX] type to [XXX]?

Thanks,
Alexei
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners