Community for F#

Blog articles of Community for F#

on 2/20/2012 5:01 AM
on 2/19/2012 11:30 PM
This will be the last post on rebuilding the MailboxProcessor using TDF, here’s a quick discussion of the missing pieces… First, lets start with the simple ones, these don’t really require much discussion. DefaultTimeout 1 2 3 4 5 let mutable defaultTimeout = Timeout.Infinite member x.DefaultTimeout with get() = defaultTimeout and set(value) = defaultTimeout <- value This simply provides a mutable property using Timeout.Infinite as a default setting. CurrentQueueLength 1 member x.CurrentQueueLength() = incomingMessages.Count Another simple one, this methods uses into the underlying BufferBlock to extract its current queue length using its Count property. TryReceive 1 2 3 4 5 6 member x.TryReceive(?timeout) = let ts = TimeSpan.FromMilliseconds(float <| defaultArg time out defaultTimeout) Async.AwaitTask <| incomingMessages.ReceiveAsync(ts) .ContinueWith(fun (tt:Task<_>) -> if tt.IsCanceled || tt.IsFaulted then None else Some tt.Result) Here we get a little help from TPL to apply a continuation on completion using ContinueWith. We use a lambda to return either None, in a time out condition, or Some tt.Result when we successfully receive an item. TryPostAndReply 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 type AsyncResultCell<'a>() = ... member x.TryWaitResultSynchronously(timeout:int) = //early completion check if source.Task.IsCompleted then Some source.Task.Result //now force a wait for the task to complete else if source.Task.Wait(timeout) then Some source.Task.Result else None member x.TryPostAndReply(replyChannelMsg, ?timeout) :'Reply option = let timeout = defaultArg timeout defaultTimeout let resultCell = AsyncResultCell<_>() let msg = replyChannelMsg(new AsyncReplyChannel<_>(fun reply -> resultCell.RegisterResult(reply))) if incomingMessages.Post(msg) then resultCell.TryWaitResultSynchronously(timeout) else None Things get a little more interesting from here on in. Firstly we need to add a new synchronisation member to the AsyncResultCell<'a> type: TryWaitResultSynchronously. We again enlist the help of the TPL primitives to check for the early completion using source.Task.IsCompleted returning the result if it is there, otherwise we use the Task property’s Wait method to check the item returns within the time out interval. In the usual manner, Some source.Task.Result is returned or None for a failure. PostAndReply 1 2 3 4 member x.PostAndReply(replyChannelMsg, ?timeout) : 'Reply = match x.TryPostAndReply(replyChannelMsg, ?timeout = timeout) with | None -> raise (TimeoutException("PostAndReply timed out")) | Some result -> result This one wraps a call to TryPostAndReply with some pattern matching. In the event of a time out None is returned from TryPostAndReply in this instance we raise a TimeoutException otherwise we unwrap the result from the option using | Some result -> result. TryScan 1 2 3 4 5 6 7 8 9 10 11 12 13 14 member x.TryScan((scanner: 'Msg -> Async<_> option), timeout): Async<_ option> = let ts = TimeSpan.FromMilliseconds( float timeout) let rec loopForMsg = async { let! msg = Async.AwaitTask <| incomingMessages.ReceiveAsync(ts) .ContinueWith(fun (tt:Task<_>) -> if tt.IsCanceled || tt.IsFaulted then None else Some tt.Result) match msg with | Some m -> let res = scanner m match res with | None -> return! loopForMsg | Some res -> return! res | None -> return None} loopForMsg This one also uses the same ContinueWith functionality in the recursive loopForMsg function, perhaps some of these functions could extracted out and refactored but I prefer to keep the code like this to better explain what’s going on. The the code is available on GitHub anyway so feel free to clean up any detritus and send me a pull request. Again we use pattern matching to keep calling the loopForMsg function until the result is returned or a time out occurs. Scan 1 2 3 4 5 member x.Scan(scanner, timeout) = async { let! res = x.TryScan(scanner, timeout) match res with | None -> return raise(TimeoutException("Scan TimedOut")) | Some res -> return res } Finally we have Scan, this is much like PostAndReply in that it just acts as a wrapper around TryScan making use of pattern matching throwing an exception on a time out. That sums up the last few pieces, completing the TDF implementation of the MailboxProcessor. I think this series of posts has shown the elegance of F#’s asynchronous workflows. The use of recursive functions and the compositional nature of asynchronous workflows really helps when you are doing this type of programming. Its also very nice on the eye, each section being clearly defined. The more astute of you may have noticed something a little different. Scan and TryScan are destructive in this implementation, the unmatched messages are purged from the internal queue. Although I could have mirrored the same functionality of the MailboxProcessor by using an internal list to keep track of unmatched messages, this leads to performing checks during Receive and Scan and their derivatives to make sure that this list is used first when switching from Scan and Receive functionality. I think the separation of concerns are a little fuzzy in the MailboxProcessor. The scan function seems like an after thought, even if you don’t use Scan you still pay a price for it as there are numerous checks between the internal queue and the unmatched messages list. You can also run into issues while using Scan and TryScan that can result in out of memory conditions due to the inherent unbounded nature. I will briefly describe and explore the conditions that can lead to that in the next post. In the implementation presented here we can get bounded checking by passing in an optional DataflowBlockOptions and setting a value for the BoundedCapacity property. EDIT: The code for this series of articles is now available on GitHub: FSharpDataflow Until next time…
on 2/19/2012 10:12 AM
For Functional Programming eXchange 2012 and I’ve tried to put together a programming that mixes the best the functional programming community has to give. I wanted both talks that show how functional programming languages can be used more effectively and that show off new up and coming language and new language features. I also wanted talks that gave feedback from project that were implemented using functional languages. I’m pleased to say we have plenty of both. The day will kick off with David Pollak talking about Visi.Pro. One of the areas I’ve been thinking about a lot lately, outside of functional programming, is how tablets will affect the way we work. Clearly up take in the consumer market it is growing, and the new fashion of “Bring your own device” meet that tablets are making there way into the enterprise. But can people do useful work with these new devices? And if so how will they do it? David has come up an interesting answer, he’s betting that to make effective use of these new devices we need a new way to program them and has come up with the visi.io language and the visi.pro platform to help people create software and models using their iPads. If you care about trends in the tech industry, definitely one to watch. Next we have 3 cases studies that show how Haskell, F# and Scala have been used to build complex real world application. First up Erik Hesselink will be talking about “SilkApp” a web application for help users to visual data in a more structured way, then Loic Denuziere will talk about creating the site fpish.net a large complex community site written in F# using WebShaper, finally Kevin Wright will talk about using AKKA to create a high-throughput and low latency RESTful/streaming event service at zeebox. In the afternoon will see some more theoretical talks that’ll shows us how functional programming can be applied more effectively. First Andres Löh will talk about creating DSLs in Haskell, then Tomas Petricek will talk about F# 3.0’s new feature, Type Providers, Miles Sabin will talk about advanced uses of Scala and Bruce Durling will round of the day with an overview of Clojure and Incanter, clojure’s powerful data visualization library. And if that wasn’t enough there’ll be chance to meet and socialize with our speakers and other FPX delegates all day along and quite possibly all evening long in the pub afterwards. UPDATE: Just occurred to me I didn’t put a link into the full agenda, so here it is.
on 2/16/2012 3:58 AM
Seattle F# user group meeting Feb 21, 2012 Tuesday, February 21, 2012, 6:00 PM. Microsoft Building 99, Room 1919-C, Redmond, WA (map) 47.641712 -122.140670 As with the previous meetup, we have two sessions + one tiny program contest: Ryan Riley  (F# MVP) : Web Apps and APIs with F# Most .NET web applications today use ASP.NET WebForms or MVC. However, several F# libraries offer new ways to build web APIs and applications help reduce code and offer better abstractions, especially for single-page applications (SPA). We'll start with an existing web application written in C# using both WebForms and MVC and transition first to a F# application that follows the same patterns, then transition again to using several tools that expose the additional power offered by F#. F# team member Jack Hu (MSFT): F#, The American Dream Since F# became a first-class programming language in Visual Studio 2010, it has been gaining popularity among the financial and scientific communities. In this talk, we will showcase several F# applications in the context of financial investments. We will highlight F#'s value propositions, through the themes of simplicity, powerfulness and programmer satisfaction. We'll also take a look at current industry adoptions, who uses F# and what they gain from it. The majority of the talk will be slightly technical and look at some of innovative aspects of F# 3.0 that help to simplify programming and achieve great results ' F# 3.0 typeProvider ' F# 3.0 Query ' FsharpChart ' .Net Integration ' Async programming ' Units of measure ' TryFsharp.net   F# programming contest question will be disclosed at end of the meeting and winner will be announced at the next meeting.   Special thanks to Chris Brockett from MSR for reserving a nice place for this meeting!   MSR (Microsoft building 99, room 1919-C)
on 2/14/2012 2:52 AM
The folks at SkillsMatter in London are hosting the Functional Programming eXchange 2012, on March 16, 2012, in London! Join the Functional Programming community for a one-day conference jam-packed with talks, open-space discussions and brainstorming. Learn and share the latest innovative ideas, best tools and practices in the different languages (scala, clojure, haskell, F#) and environments. Read the full programme and book tickets here. What: Functional Programming eXchange 2012When: March 16th 2012, Breakfast and registration at 8.30Where: Skills Matter, 116-120 Goswell Road, London, EC1V 7DPTwitter: #functionalpxProgram and tickets: http://bit.ly/DSFunpx   Enjoy! Don
IntelliFactory Offices Copyright (c) 2004-2011 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us
Built with WebSharper