Slick file download with Haskell in 40 SLOC

Posted in: haskell, fp.


Lately I’ve submitted a patch to Hsenv featuring the ability to download a file over HTTP without external tools (e.g. curl), but using nothing more than awesome Haskell. I’ve posted the cool snippet on Reddit, and someone asked for a version with a progress bar, similar to what have been implemented in Python here:

Download a file with Python - SO

I came up with a nice version which does rudimental error handling over HTTP response codes, which implements a progress bar and that has a constant memory usage, because we still retain all the benefits of programming with streaming libraries, namely io-streams and http-streams. Without further ado, let me show you the code:

As you can see, it’s less than 40 lines of code! (ok, a bit more, but just because I’ve splitted the type signature and the error message on multiple lines to make it fit the blog template). Even better, as someone suggested on Reddit, we can make this code multi-platform using withSocketsDo before get. We need this on Windows machines only (to initialize the underlying socket), but due to the fact withSocketsDo is implemented as id on *nix platforms, everything will just work. Once again, Haskell rocks.


Loved this post? Stay update!

Comments