Skip to content

Instantly share code, notes, and snippets.

@panesofglass
panesofglass / AsyncEx.fs
Created September 13, 2010 00:30
Create an Observable from an F# Async.
module AsyncEx
open System
type Async<'a> with
member this.ToObservable() =
{ new IObservable<_> with
member x.Subscribe(o) =
if o = null then nullArg "observer"
let cts = new System.Threading.CancellationTokenSource()
let invoked = ref 0
@panesofglass
panesofglass / RouteTester.fsx
Created October 8, 2010 16:32
A sitemap-based url tester that runs in parallel (F#).
module RouteTester
#r "FSharp.PowerPack"
#r "System.Xml"
#r "System.Xml.Linq"
open System
open System.Diagnostics
open System.IO
open System.Net
@panesofglass
panesofglass / ContinuationMonad.fs
Created October 15, 2010 21:59 — forked from mattpodwysocki/ContinuationMonad.fs
Continuation monad with callCC
type Cont<'a,'r> =
abstract Call : ('a -> 'r) * (exn -> 'r) -> 'r
let private protect f x cont econt =
let res = try Choice1Of2 (f x) with err -> Choice2Of2 err
match res with
| Choice1Of2 v -> cont v
| Choice2Of2 v -> econt v
let runCont (c:Cont<_,_>) cont econt = c.Call(cont, econt)
@panesofglass
panesofglass / TestModule.fs
Created November 27, 2010 23:18
Expressive testing with NUnit in F#
module TestModule
open NUnit.Framework
let inline (==) (actual:#obj) (expected:#obj) = Assert.AreEqual(expected, actual)
let inline (!=) (actual:#obj) (expected:#obj) = Assert.AreNotEqual(expected, actual)
let inline (<->) (actual:#obj) expected = Assert.IsInstanceOf(expected, actual)
let inline (<!>) (actual:#obj) expected = Assert.IsNotInstanceOf(expected, actual)
let ``is null`` anObject = Assert.IsNull(anObject)
let ``is not null`` anObject = Assert.NotNull(anObject)
@panesofglass
panesofglass / IApplication.cs
Created November 30, 2010 16:59
.NET Web Abstractions
// Func<IRequest, IResponse>
public interface IApplication {
IAsyncResult BeginInvoke(IRequest request, AsyncCallback callback, object state);
IResponse EndInvoke(IAsyncResult result);
}
@panesofglass
panesofglass / OwinChat.fsx
Created December 2, 2010 20:18
An example of writing a chat server using OWIN and F# Agents. See http://tomasp.net/blog/education-and-agents-talks.aspx
let root = @"C:\demos\FSharp.Agents\Demo.ChatServer\"
let cts = new CancellationTokenSource()
Owin.Extensions.Application.Start
("http://localhost:8082/", (fun request -> async {
match request.Path with // Path is an extension on IRequest to retrieve the path without the query string.
| "/post" ->
// Send message to the chat room
let! body = request.AsyncReadBody() // Extension that asynchronously reads the request body.
room.SendMessage(body) // room is an instance wrapping a F# MailboxProcessor<_>
namespace Owin
{
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
public interface IResponseHandler
{
Type TypeToHandle { get; }
@panesofglass
panesofglass / OwinDraft2.fs
Created December 28, 2010 04:50 — forked from bvanderveen/OwinDraft2.cs
An alternate definition of the OWIN specification based on RFC3875
namespace Owin
open System
open System.Collections.Generic
open System.Threading.Tasks
type Request = IDictionary<string, obj>
type Response = string * IDictionary<string, seq<string>> * seq<obj>
type Application = Func<Request, Task<Response>>
@panesofglass
panesofglass / TcpServer.fsx
Created January 4, 2011 17:43
Asynchronous TCP server in F#
// [snippet: Async socket server using F# async computations.]
open System
open System.IO
open System.Net
open System.Net.Sockets
open System.Threading
type Socket with
member socket.AsyncAccept() = Async.FromBeginEnd(socket.BeginAccept, socket.EndAccept)
@panesofglass
panesofglass / Parser.fs
Created January 12, 2011 07:04
Parser monad using Async computations
type Parser<'r, 's> = 's -> Async<('r * 's) option>
let returnF v : Parser<'r,'s> = fun input -> async { return Some(v, input) }
let bind (p: Parser<'a,'s>) (f: 'a -> Parser<'b,'s>) : Parser<'b,'s> =
fun input -> async {
let! comp = p input
return! match comp with
| Some(value, input) -> f value input
| None -> async { return None } }