0
comment
on 12/9/2009 2:20 AM

Once every while I have to remind myself that I can program, and that the task at hand can be automated. On the most recent occasion, I was trying to write down the dependency graph for Yahoo User Interface (YUI) modules, when it finally occurred to me that the dependencies are documented in source code, and the source code is available online. The task was doable within 30 lines of F#. I would like to share the fun part, implementing an asynchronous WGET, designed as a function from a URL to a string of its contents. With using the built-in Async.Parallel predicate on 30 or so requests constructed, I was able to observe a significant improvement in the program's responsiveness. It is nice to have .NET network libraries available at your fingertips. Even when their APIs are not exactly convenient, you can typically wrap them in a nice manner.

1
2
3
4
5
6
7
8
9
    let Fetch (url: string) =
        let read (response: System.Net.WebResponse) =
            use reader = new System.IO.StreamReader(response.GetResponseStream())
            reader.ReadToEnd()
        async {
            let request = System.Net.WebRequest.Create url
            let! response = Async.FromBeginEnd(request.BeginGetResponse, request.EndGetResponse)
            return read response
        }
.
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us
Built with WebSharper