This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module RouteTester | |
| #r "FSharp.PowerPack" | |
| #r "System.Xml" | |
| #r "System.Xml.Linq" | |
| open System | |
| open System.Diagnostics | |
| open System.IO | |
| open System.Net |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Func<IRequest, IResponse> | |
| public interface IApplication { | |
| IAsyncResult BeginInvoke(IRequest request, AsyncCallback callback, object state); | |
| IResponse EndInvoke(IAsyncResult result); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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<_> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace Owin | |
| { | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Text; | |
| using System.IO; | |
| public interface IResponseHandler | |
| { | |
| Type TypeToHandle { get; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // [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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 } } |