Blog articles of Community for F#

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
About this group
- Founded: 7/22/2011
- Owners: Loic Denuziere, Adam Granicz, Ryan Riley
- Members: 9
- Past events: 22
- Upcoming events: 0 Log in to join
Featured group
| New England F# User Group 4 past events |
Latest blog articles by c4fs (see all)
- F#/C# Contract Position for Silverlight/HTML 5 User Interface Development at Microsoft Research, Cambridge, UK
- Testing a jQuery Plugin with ExpectThat and Mocha
- From Iteratees to Conduits
- Undertone – Programmable music in F#
- First F# Seattle Meetup This Saturday, Redmond
- London F# Meetup Group this Thursday: Pacman Kata
- Making F# Windows Phone Development a Little Easier
- F# 3.0 at TechDays France, Feb 7, Paris!
- F#, WebSharper, JavaScript, HTML5, Mobile etc.
- F# Training in London in January and February: Functional Programming in .NET and Real World F# Programming
- Microsoft Releases Local, Distributed and Cloud Numerics Library, with F# Samples
- developerFusion Article: An Introduction to FSharpx
- Come and work with the F# group at Microsoft Research in Cambridge!
- Some F# Programming Jobs in London
- F# courses and talks (Winter 2012 and beyond...)
- HTTP and Functional Programming
- Introducing ExpectThat: A CoffeeScript Assertion Library
- Web Architecture Done Right
- Regions and navigation bar for F# in Visual Studio
- A New Web for .NET: FubuMVC and Chad's response to AR Considered Harmful
- Announcing FsUnit 1.0
- Porting Bryan's Erlang Function to F#
- Announcing an F# Meetup Group in Seattle
- 6 Month Contract Position at MSR Cambridge: Cross-Platform and Web-Delivered Data-Rich Programming with F# 3.0
- Enhancements to FsUnit (version 0.9.1.1)
- Building an ASP.NET MVC 4 Solution with F# and C#
- Getting Setup for JavaScript Testing with Pavlov
- New York City F# Meetup Group: High Performance F#, in .NET and on the GPU with Jack Pappas, Tuesday, November 29, 2011, 6:30 PM
- F# Math (IV.) - Writing generic numeric code
- Building F# Solutions in Visual Studio 11
- F# Math (III.) - Defining custom numeric types
- F# agents with timeouts
- Updates to the August 2011 F# 2.0 Compiler Code Drop
- StatFactory: FCore maths & statistics library, designed for use with F#
- Tonight at F#unctional Londoners: Byron Cook: Proving program termination with F#
- A Pinch of CoffeeScript Sugar - Part 1
- F# Silverlight Library Template in Visual Studio 11
- New F# Windows Phone Library Project Template
- F# Math (II.) - Using matrices for graph algorithms
- MonoDevelop User Voice: Vote for Full F# Support
- A Coder Interview with Dan Mohl
- The Combinator Approach to Programming Domain Specific Languages with F#
- Job at MSR Cambridge: Infer.NET
- F# Math (I.) - Numeric types in PowerPack
- F# Math - Numerical computing and F# PowerPack
- Progressive F# Tutorials at SkillsMatter, London, Thu-Fri This Week
- Type Systems are Asserts
- Type Systems are Asserts
- Calling F# Libraries from Metro Style Apps
- Potential Post-PhD and Internship Positions in Web-Delivered, Data-Rich Cloud Programming
- How to let other teams at Microsoft know how they can support F# better
- Two New F# 3.0 Type Provider Related NuGet Packages
- Please submit, vote on and discuss F# and Visual Studio features
- Some thoughts about Google’s new Dart programming language
- OOP to me means only messaging local retention...
- OOP to me means only messaging local retention…
- Iteratee in F# - Part 1
- Iteratee in F# – Part 1
- Planning for Functional.net 2012
- Authoring Type Providers with the TypeProviderDSL from FSharpx
- Today's the day to say it.... I'm an Apple II kid
- F# 2-Year Contract Position for Biological Modelling Language Development
- The MSR Cambridge Research Games Team invite you to play Blotto
- F# presentation - F# Eye for the C# Guy
- MSDN Magazine Article: Authoring an F#/C# VSIX Project Template
- New F#/C# ASP.NET MVC 3 Template
- A Simple AppSettings Type Provider
- WebSharper 2.3 Q3 released
- Presentation: Dialing Up with F# and Windows Phone 7
- First example of a very simple type provider
- F# Type Providers - Querying StackOverflow
- WebSharper at CUFP 2011
- Advantages of CoffeeScript When Working with jQuery Templates
- A few thoughts on build and Windows 8
- WP7 AccelerometerProxy in F#
- Functional Programming eXchange 2012: Call for abstracts
- F#, RavenDB and PicoMvc – Creating an Autocomplete – The ETL
- See My Stack Overflow Dev Days Talk
- Getting Started with the F# PowerPack - Part 4
- F#, RavenDB and PicoMvc – Creating an Autocomplete – Scenario and Project Setup
- Interested in presenting at a conference on functional…
- Organizing Code Files
- Calculating when the 1000th XKCD will appear
- The iteratee is continuing to hang me up…
- Unit Testing a jQuery Plugin with CoffeeScript and Pavlov
- Upcoming September 2011 course has been moved to 1 November 2011
- F# courses and talks (Autumn 2011)
- Adding NuGet Support to F# Interactive
- Getting Started with the F# PowerPack - Part 3
- DevLink: Getting Started with F# Web Development
- Getting Started with the F# PowerPack - Part 2
- Chinese Chess: An Exercise in Upgrading
- Another CoffeeScript and jQuery UI Example
- Small Revamp to strangelights.com and Free Chapter of Beginning F#
- Programming with F# asynchronous sequences
- Real-World F# Articles on MSDN
- Speaking at Stack Overflow Dev Days and Progressive F# in autumn 2011
- The Frank application signature is curre...
- The Frank application signature is curre…
- http://www.wave-vs.net/
- http://www.wave-vs.net/
- Announcing Four New F# Projects For Creating Web Apps
- Here's another question related to Fran...
- Here’s another question related to Fran…
- I've been working on the Frank syntax lately...
- I’ve been working on the Frank syntax lately…
- Safer asynchronous workflows for GUI programming
- Writing non-blocking user-interfaces in F#
- Which do you prefer for Frank routing gist...
- State Machines...
- HTTP Parsing...
- Asynchronous I/O...
- Which do you prefer for Frank routing gist…
- State Machines…
- Jon Skeet's LINQ to Objects...
- Separate team quoted one man year. Took ...
- More WebSharper talks in February
- Come and see me and other F# guru’s in Seattle!
- Sencha Touch for WebSharper available
- Windows Phone 7 Accelerometer and F#
- New WebSharper extensions available
- WebSharper 2.1 Beta 5 available
Event tags for this group
- f# × 20
- web × 5
- teaching × 3
- testing × 2
- functional × 2
- templates × 2
- websharper × 2
- http × 2
- open source × 1
- compiler × 1
- koans × 1
- bistro × 1
- introduction × 1
- metaprogramming × 1
- enterprise × 1
- tickspec × 1
- server × 1
- agents × 1
- tcp × 1
- monorail × 1
- fpish × 1
- pit fw × 1
- servicestack × 1
- single page application × 1
Group tags
- f# × 15
- clojure × 6
- functional × 6
- haskell × 5
- scala × 4
- websharper × 3
- c# × 2
- erlang × 2
- javascript × 2
- skillsmatter × 2
- .net × 1
- alt.net × 1
- async × 1
- blazehtml × 1
- coffeescript × 1
- continuous delivery × 1
- fractureio × 1
- html × 1
- html5 × 1
- jquery × 1
- lisp × 1
- load testing × 1
- ocaml × 1
- package management × 1
- reactive extensions × 1
- restful × 1
- rx × 1
- scalability × 1
- scheme × 1
- tdd × 1
- unit testing × 1
- user group × 1
- web × 1
- web services × 1
![]() |
Copyright (c) 2004-2011 IntelliFactory. All rights reserved. Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Built with WebSharper |

