Snippets in category Concurrency
Asynchronous sequences
An asynchronous sequence is similar to the seq<T> type, but the elements of the sequence are generated asynchronously without blocking the caller as in Async<T>. This snippet declares asynchronous sequence and uses it to compare two files in 1k blocks.
36 people like this
Posted: 1 years ago by Tomas PetricekClojure's Atoms
Clojure's Atoms are ref like structures, with the addition of (Compare And Swap) update semantics
17 people like this
Posted: 1 years ago by Nick PalladinosAsync hashing
Async memory or file hashing
8 people like this
Posted: 1 years ago by Mauricio SchefferAsync SMTP
Async wrapper for SmtpClient (which is event-based)
9 people like this
Posted: 1 years ago by Mauricio SchefferAsync TCP Server
A basic, asynchronous TCP server
15 people like this
Posted: 1 years ago by Ryan RileyAsync to IObservable
Wraps an Async as an IObservable to allow easier consumption by other .NET languages. Many thanks to desco for his help: http://cs.hubfs.net/forums/thread/16545.aspx
3 people like this
Posted: 1 years ago by Ryan RileyEasy Wrapper for thread pool work
An easy wrapper for the TPL that works nicely with (|>)
8 people like this
Posted: 1 years ago by Paul GreeneClojure's Atoms
Clojure's Atoms are ref like structures, with the addition of (Compare And Swap) update semantics
16 people like this
Posted: 1 years ago by Nick PalladinosSimple HTTP server with Async workflow
Simple HTTP server with Async workflow and Http Listener
1 people like this
Posted: 1 years ago by Ankur DhamaPlaying with async sequences
Attempt to reimplement functions AsyncRead/AsyncReadLines from 'Rx on the server ' articles (by Jeffrey van Gogh) using idea of AsyncSequence (by Tomas Petricek)
18 people like this
Posted: 1 years ago by Vladimir MatveevComposable WCF Web API using Async
A functional wrapper around the new WCF Web APIs (http://wcf.codeplex.com/). Composition is achieved through the use of the HttpRequestMessage -> Async<HttpResponseMessage> signature. Pushing the app calls in the MessageHandler intercepts all requests and allows you to take control at the earliest point possible before operation selection occurs. Extending this slightly to call the innerChannel's SendAsync would allow you to create a middleware layer that would work both with this and other, normal Web API services.
39 people like this
Posted: 1 years ago by Ryan RileyA little esoteric os
Petrovich is more than just a programming language, it is a complete computer operating system and program development environment named after Ivan Petrovich Pavlov. Design Principles: * Provide an operating system and computer language that can learn and improve its performance in a natural manner. * Adapt to user feedback in an intelligent manner.
9 people like this
Posted: 1 years ago by Natallie BaikevichObject oriented as it was supposed to be ?
The snippet shows implementing object orientation using mail box processor. In this context object orientation have this simple definition: "Objects acts on message passing". The objects created this way are thread safe too :). Not sure how much practical this would be in todays context where object oriented has gone the wrong way.
19 people like this
Posted: 1 years ago by Ankur DhamaImplementing active objects with a MailboxProcessor
Mailbox processors can easily be used to implement active objects. This example shows how to do that with a reusable wrapper type and minimal boilerplate code in the actual class definitions. Supports both asynchronous calls and synchronous calls. For the latter case, exceptions are automatically propagated back to the caller.
47 people like this
Posted: 1 years ago by Wolfgang MeyerSimple asynchronous functions
The snippet demonstrates how to compose simple asynchronous functions and how to use try .. with to handle exceptions in asynchronous workflows.
28 people like this
Posted: 1 years ago by Tomas PetricekTCP/IP Proxy
A minimal TCP/IP proxy implementation with F# asynchronous workflows
11 people like this
Posted: 1 years ago by Ademar GonzalezWeb Crawler
This snippet features an F# Web crawler that i'm already using in 2 applications (slightly modified). It's based on a scalable network of communicating agents that follow URLs extracted from HTML pages until reaching the specified limit.
20 people like this
Posted: 1 years ago by Taha HachanaActors acting as Lambdas
The Untyped Lambda Calculus encoded as actors (F#'s MailboxProcessors)
2 people like this
Posted: 1 years ago by Nick PalladinosAsynchronous Controller Helper
The snippet declares a helper for creating asynchronous controllers for ASP.NET MVC 3. It declares a new base class for asynchronous actions that exposes a computation builder for writing actions using F# asynchronous workflows.
7 people like this
Posted: 1 years ago by Tomas PetricekAsync SNTP client
An asynchronous SNTP client that can retrieve the current time from an internet time server (such as time-a.nist.gov) and optionally update the local system clock to match. Demonstrates async UDP communication, bit-shifting, and native interop/PInvoke.
8 people like this
Posted: 1 years ago by Joel MuellerAsync File Crawl
Async File Crawl
6 people like this
Posted: 11 months ago by fholmSequence Generator from Async
Generates a sequence using a sequence generator and Async.StartWithContinuations. This is an attempt at modeling the OWIN delegate structure in F#
4 people like this
Posted: 11 months ago by Ryan RileyThrottling agent
Agent that can be used for controlling the number of concurrently executing asynchronous workflows. The agent runs a specified number of operations concurrently and queues remaining pending requests. The queued work items are started as soon as one of the previous items completes.
9 people like this
Posted: 11 months ago by Tomas PetricekAsynchronous Workflow Controller
The snippet overrides default AsyncControllerActionInvoker so F# async workflows can be used for ASP.NET MVC 3. It declares a new base class for asynchronous controller. Controller method has to have return type Async<ActionResult>.
3 people like this
Posted: 11 months ago by Dmitry MorozovSilverlight asynchronous WebService call with UI-thread syncronization dispatcher
You can use this code to make a async WebRequest from Silverlight to update ViewModel.
4 people like this
Posted: 11 months ago by Tuomas HietanenAgent based ObjectPool
This is a simple implementation of an object pool using an agent (MailboxProcessor). The pool is created with an initial number of object using the specified generator. The ObjectPool has three functions: Put: An item can be 'Put' into the pool. Get: An item can be taken from the pool ToListAndClear: A list of all the items in the pool is returned and the pool is cleared.
7 people like this
Posted: 11 months ago by 7sharp9F# Future
Similar to an async { } block but captures the result for future consumption. This structure can be very useful for performing multiple result-returning operations in parallel when the results aren't needed immediately. For example, performing several read/transform operations or pre-populating a cache with yet-to-be computed values. Microsoft's Task Parallel Library in .NET 4.0 includes a Future implementation so this version is only needed on earlier .NET versions. Comments, suggestions, and improvements are always welcome.
3 people like this
Posted: 11 months ago by Jason McCampbellF# Future using lazy and a threading event
F# Future using lazy and a threading event. Supports creating futures from functions or asyncs. Eager evaluation of can be specified.
3 people like this
Posted: 11 months ago by Ankur DhamaTetris
Playable Tetris mini-game. Use arrow keys to move left and right and up to rotate, down to drop. Try it out in the browser on TryFSharp.org
10 people like this
Posted: 10 months ago by Phillip TrelfordStill Mouse Click Event
Detects a mouse down then up event without a move.
3 people like this
Posted: 10 months ago by Phillip TrelfordCancellable agent
The snippet implements a wrapper for standard F# agent that can be cancelled using the IDisposable interface. This makes it possible to use the agent locally (e.g. inside asynchronous workflow). When it is no longer needed, the agent's body is cancelled.
8 people like this
Posted: 10 months ago by Tomas Petricek
Web Crawler extensions
The snippet extends a web crawler from snippet http://fssnip.net/3K. It synchronizes all printing using an additional agent (so printed text does not interleave) and the crawling function returns an asynchronous workflow that returns when crawling completes.
6 people like this
Posted: 10 months ago by Tomas PetricekAgent Based Scheduler
An agent based scheduler, can be sent a single schedule message (ScheduleOnce) and multiple schedule message (Schedule). The schedule messages comprise of a function to receive the message, the message, an initial TimeSpan before the message is scheduled, and another timespan for the schedule repeat. Check out my blog below for more details: http://bit.ly/mK4prb
4 people like this
Posted: 10 months ago by 7sharp9Extensions for HTTP servers
This snippet extends several types from the System.Net namespace. It provides an easy to use API for creating asynchronous (as well as synchronous) HTTP servers using F# asynchronous workflows.
3 people like this
Posted: 10 months ago by Tomas PetricekSynchronous, event-based and asynchronous HTTP proxy
This snippet shows the implementation of three HTTP proxy servers in F#. The first is written using simple synchronous style (that isn't scalable). The second version uses event-based approach in the Node.js style, but is difficult to write. The third version uses F# async workflows and is both scalable and easy to write.
5 people like this
Posted: 10 months ago by Tomas PetricekAsynchronous HTTP proxy with chunking and caching
This snippet shows two improvements to asynchronous HTTP proxy from: http://fssnip.net/6e. First extension is to process page in chunks (instead of downloading the entire content first). The second extension is to use simple agent-based in-memory cache for previously visited pages.
5 people like this
Posted: 10 months ago by Tomas PetricekErlang Ring problem
Here's an attempt at the Erlang ring problem in F#.
1 people like this
Posted: 10 months ago by David GrenierReturn the first result using Async.Choice
The snippet implements Async.Choice method that takes several workflows and creates a workflow, which returns the first result that was computed. After a workflow completes, remaining workflows are cancelled using the F# async cancellation mechanism. (The method doesn't handle exceptions.)
6 people like this
Posted: 9 months ago by Tomas PetricekAsync based MapReduce
Async is a very versatile structure, which has been used to compose CPU/IO bound computations. So it is very tempting to implement a MapReduce function based on Async and borrowing ideas from the theory of list homomorphisms.
7 people like this
Posted: 9 months ago by Nick PalladinosSimple job processor
Generic batch job processor using Mail box processor. Sending quit message using PostAndReply will ensure that all jobs are completed before returning. (Exception handling is responsibility of the job)
4 people like this
Posted: 9 months ago by Ankur DhamaAsyncSeq - Introduction and Crawler
This snippet demonstrates programming using asynchronous sequences. It contains (hidden) implementation of AsyncSeq type and combinators for working with it. More importantly, it demonstrates how to use asynchronous sequences to implement a simple sequential on-demand crawler.
8 people like this
Posted: 9 months ago by Tomas PetricekGrowing Tree Algorithm for Maze Generation
There are several maze creation algorithms (http://www.astrolog.org/labyrnth/algrithm.htm). The interesting point about Growing Tree one is that it turns into the others (for example, Recursive Backtracker and Prim's algo) when we choose the next step in different ways. Check it with tryfsharp.org.
8 people like this
Posted: 8 months ago by Natallie BaikevichCall/CC for Async
An implementation of call-with-current-continuation for Async.
2 people like this
Posted: 8 months ago by Ryan RileyDownload stock prices as async sequence
The snippet uses asynchronous sequences (from F# AsyncExtensions) to download historical stock data from Yahoo. Data is downloaded in a buffered way on demand (as needed) and returned line by line. The sample then prints OHLC values for first 30 items.
5 people like this
Posted: 8 months ago by Tomas PetricekCreating observable using Async.StartDisposable
Implements a simple Async.StartDisposable extension that can be used to easily create IObservable values from F# asynchronous workflows. The method starts an asynchronous workflow and returns IDisposable that cancels the workflow when disposed.
4 people like this
Posted: 8 months ago by Tomas PetricekSliding window for Observable
Implements the Observable.windowed function that creates an observable returning a sliding window. The function is an observable version of Seq.observable. The implementation uses a simple F# agent that keeps partial windows and sends them to an observer.
3 people like this
Posted: 8 months ago by Tomas PetricekAsync.Choose operation
Non-deterministic choice operation for F# asynchronous workflows - creates a workflow that returns the result of one of two asynchronous workflows, depending on which completes first. (The other workflow is not cancelled.)
3 people like this
Posted: 7 months ago by Tomas PetricekSimple timed-expiry cache
Basic thread-safe timed-expiry cache, implemented as a MailboxProcessor.
3 people like this
Posted: 6 months ago by Yusuf MotaraF#-friendly SocketAsyncEventArgs
The name is a bit trying, but the overall callback approach greatly simplifies the mechanism for calling and handling the System.Net.Sockets.Socket Async methods.
0 people like this
Posted: 6 months ago by Ryan RileyCaching agent
Agent that keeps a cache of web pages that were downloaded previously. The agent handles messages to add and get data as well as message to clear the cache.
2 people like this
Posted: 6 months ago by Tomas PetricekThe dining philosophers
The dining philosophers problem implemented using a waiter.
4 people like this
Posted: 6 months ago by Alex MuscarF# counter agent
The snippet shows a simple F# agent that calculates average from the received values. It supports one message for adding numbers to the statistics and one for resetting the state. Moreover, the agent limits the processing rate to 1 message per second.
3 people like this
Posted: 4 months ago by Tomas PetricekPong
Pong video game runnable inside TryFSharp.org. Player 1 keys 'Q' - up, 'A' - down. Player 2 keys 'P' - up, 'L' - down.
5 people like this
Posted: 3 months ago by Phillip TrelfordAsync.Parallel2 and Async.Parallel3
Async.Parallel2 and Async.Parallel3, for running three Async's in parallel as thread pool tasks. Alternative versions given which use Async.Parallel under the hood.
13 people like this
Posted: 3 months ago by Don SymeAsync.Sleep with immediate cancellation
Implementation of Async.Sleep in f# 2.0 doesn't allow break it execution until the time elapsed. Here is the alternate implementation which support immediate cancellation. According to discussion http://stackoverflow.com/questions/9041491/is-there-any-reason-why-async-sleep-can-not-be-canceled-immediately .
2 people like this
Posted: 3 months ago by Andrei KolomentsevTicTacToe(Joinads Example)
TicTacToe game simulator implemented by using Joinads(http://tomasp.net/blog/joinads-async-prog.aspx). Game logic in this snippet was simplified so nicely by using Joinads. You can run this snippet on Try Joinads (http://tryjoinads.org/).
3 people like this
Posted: 3 months ago by nagat01Async HTTP server
Creating an asynchronous HTTP Server in F#.
6 people like this
Posted: 1 months ago by Julian KayMonadic transactions for Clojure-style atoms
This is a simple implementation of a monadic transaction builder for Clojure-style atoms. Based on original code by Nick Palladinos.
10 people like this
Posted: 1 months ago by Eirik TsarpalisFun with Ping and Pong
Yet another ping pong sample with agents.
3 people like this
Posted: 22 days ago by Ryan RileyPing and Pong go Chunking Along
Parameterizing pong allows us to do even more fun things. Here we use a few message types to allow stateful consumption of data sent by ping to its pongs.
1 people like this
Posted: 21 days ago by Ryan RileyOminaisuusStorage-testi
Korjasin modulen P:n olemaan isolla... :-)
Async Primitives
A set of Async primitives as described by Dave Thomas [1] (and derived from Stephen Toub [2]). [1] http://moiraesoftware.com/blog/2012/04/22/back-to-the-primitive-ii/ [2] http://blogs.msdn.com/b/pfxteam/archive/2012/02/11/10266923.aspx
1 people like this
Posted: 9 days ago by Ryan Riley