val urlList : string list

Full name: index.urlList
val fetchHtmlAsync : url:string -> Async<string>

Full name: index.fetchHtmlAsync
val url : string
val async : AsyncBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
val uri : System.Uri
namespace System
Multiple items
type Uri =
  new : uriString:string -> Uri + 5 overloads
  member AbsolutePath : string
  member AbsoluteUri : string
  member Authority : string
  member DnsSafeHost : string
  member Equals : comparand:obj -> bool
  member Fragment : string
  member GetComponents : components:UriComponents * format:UriFormat -> string
  member GetHashCode : unit -> int
  member GetLeftPart : part:UriPartial -> string
  ...

Full name: System.Uri

--------------------
System.Uri(uriString: string) : unit
System.Uri(uriString: string, uriKind: System.UriKind) : unit
System.Uri(baseUri: System.Uri, relativeUri: string) : unit
System.Uri(baseUri: System.Uri, relativeUri: System.Uri) : unit
val webClient : System.Net.WebClient
namespace System.Net
Multiple items
type WebClient =
  inherit Component
  new : unit -> WebClient
  member BaseAddress : string with get, set
  member CachePolicy : RequestCachePolicy with get, set
  member CancelAsync : unit -> unit
  member Credentials : ICredentials with get, set
  member DownloadData : address:string -> byte[] + 1 overload
  member DownloadDataAsync : address:Uri -> unit + 1 overload
  member DownloadFile : address:string * fileName:string -> unit + 1 overload
  member DownloadFileAsync : address:Uri * fileName:string -> unit + 1 overload
  member DownloadString : address:string -> string + 1 overload
  ...

Full name: System.Net.WebClient

--------------------
System.Net.WebClient() : unit
val html : string
member System.Net.WebClient.AsyncDownloadString : address:System.Uri -> Async<string>
val getHtmlList : (string list -> string [])

Full name: index.getHtmlList
module Seq

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
Multiple items
type Async
static member AsBeginEnd : computation:('Arg -> Async<'T>) -> ('Arg * AsyncCallback * obj -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit)
static member AwaitEvent : event:IEvent<'Del,'T> * ?cancelAction:(unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate)
static member AwaitIAsyncResult : iar:IAsyncResult * ?millisecondsTimeout:int -> Async<bool>
static member AwaitTask : task:Task -> Async<unit>
static member AwaitTask : task:Task<'T> -> Async<'T>
static member AwaitWaitHandle : waitHandle:WaitHandle * ?millisecondsTimeout:int -> Async<bool>
static member CancelDefaultToken : unit -> unit
static member Catch : computation:Async<'T> -> Async<Choice<'T,exn>>
static member FromBeginEnd : beginAction:(AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg:'Arg1 * beginAction:('Arg1 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * beginAction:('Arg1 * 'Arg2 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * arg3:'Arg3 * beginAction:('Arg1 * 'Arg2 * 'Arg3 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromContinuations : callback:(('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T>
static member Ignore : computation:Async<'T> -> Async<unit>
static member OnCancel : interruption:(unit -> unit) -> Async<IDisposable>
static member Parallel : computations:seq<Async<'T>> -> Async<'T []>
static member RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:CancellationToken -> 'T
static member Sleep : millisecondsDueTime:int -> Async<unit>
static member Start : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions * ?cancellationToken:CancellationToken -> Task<'T>
static member StartChild : computation:Async<'T> * ?millisecondsTimeout:int -> Async<Async<'T>>
static member StartChildAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions -> Async<Task<'T>>
static member StartImmediate : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartWithContinuations : computation:Async<'T> * continuation:('T -> unit) * exceptionContinuation:(exn -> unit) * cancellationContinuation:(OperationCanceledException -> unit) * ?cancellationToken:CancellationToken -> unit
static member SwitchToContext : syncContext:SynchronizationContext -> Async<unit>
static member SwitchToNewThread : unit -> Async<unit>
static member SwitchToThreadPool : unit -> Async<unit>
static member TryCancelled : computation:Async<'T> * compensation:(OperationCanceledException -> unit) -> Async<'T>
static member CancellationToken : Async<CancellationToken>
static member DefaultCancellationToken : CancellationToken

Full name: Microsoft.FSharp.Control.Async

--------------------
type Async<'T>

Full name: Microsoft.FSharp.Control.Async<_>
static member Async.Parallel : computations:seq<Async<'T>> -> Async<'T []>
static member Async.RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:System.Threading.CancellationToken -> 'T
val htmlList : string []

Full name: index.htmlList
module Observable

from Microsoft.FSharp.Control
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
Multiple items
val single : value:'T -> single (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.single

--------------------
type single = System.Single

Full name: Microsoft.FSharp.Core.single
Multiple items
module Map

from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> =
  interface IEnumerable
  interface IComparable
  interface IEnumerable<KeyValuePair<'Key,'Value>>
  interface ICollection<KeyValuePair<'Key,'Value>>
  interface IDictionary<'Key,'Value>
  new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
  member Add : key:'Key * value:'Value -> Map<'Key,'Value>
  member ContainsKey : key:'Key -> bool
  override Equals : obj -> bool
  member Remove : key:'Key -> Map<'Key,'Value>
  ...

Full name: Microsoft.FSharp.Collections.Map<_,_>

--------------------
new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
val set : elements:seq<'T> -> Set<'T> (requires comparison)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.set

Async programming

First about different approaches

  • WWW - world wide waiting
  • APM - IAsyncResult
  • TPL - Task Parallell Library
  • async/await
  • Rx
  • Message queues
  • Actor - Erlang/Akka/Akka.NET

Traditional synchronous approach

1: 
2: 
3: 
4: 
5: 
6: 
public class MyReader
{
    var fs = File.OpenRead(@"c:\somefile.txt");
    byte[] buffer = new byte[fs.Length];
    public int Read(byte [] buffer, int offset, int count);
}

APM

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
static void Main(string[] args)
{
    byte[] readBuffer;
    var fs = File.OpenRead(@"c:\somefile.txt");
    readBuffer = new byte[fs.Length];
    var result = fs.BeginRead(readBuffer, 0, (int)fs.Length,
      OnReadComplete, fs);

    //do other work here while file is read...

    Console.ReadLine();
}
private static void OnReadComplete(IAsyncResult result)
{
    var stream = (FileStream)result.AsyncState;
    var bytesRead = stream.EndRead(result);
    Console.WriteLine("Read {0} bytes successfully.", bytesRead);
    stream.Dispose();
}

APM

  • Asynchronous Programming Model
  • Separate XYBegin und XYComplete methods - readability
  • No way to cancel
  • No error handling
    • No using statement for resources possible - create and dispose in different methods
  • No synchronization between callback and caller thread
  • No coordination for multiple async operations

APM

APM with lambda indstead of separate method

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
static void Main(string[] args)
{
  byte[] readBuffer;
  var fs = File.OpenRead(@"c:\somefile.txt");
  readBuffer = new byte[fs.Length];
  var result = fs.BeginRead(readBuffer, 0, (int)fs.Length,
      asyncResult =>
      {
          var bytesRead = fs.EndRead(asyncResult);
          Console.WriteLine("Read {0} bytes successfully.", bytesRead);
          fs.Dispose();
      }, null);

  //do other work here while file is read...

  Console.ReadLine();
}

TAP

TAP - Task-based Async Programming

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
private static Task<int> ReadFileAsync(string filePath)
{
    var fs = File.OpenRead(filePath);
    var readBuffer = new byte[fs.Length];
    var readTask = fs.ReadAsync(readBuffer, 0, (int)fs.Length);
    readTask.ContinueWith(task =>
    {
        if (task.Status == TaskStatus.RanToCompletion)
            Console.WriteLine("Read {0} bytes from file {1}",
              task.Result, filePath);
        else
            Console.WriteLine("Error with file {0}.", filePath);
        fs.Dispose();
    });
    return readTask;
}

TAP - Task-based Async Programming

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
static void Main(string[] args)
{
    var read1 = ReadFileAsync(@"c:\somefile.txt");
    var read2 = ReadFileAsync(@"c:\someotherfile.txt");
    Task.WhenAll(read1, read2)
        .ContinueWith(task => Console.WriteLine("All done."));   
    //do other work here while files are read...
}

TAP - Promise in JavaScript

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
asyncThing1().then(function() {
  return asyncThing2();
}).then(function() {
  return asyncThing3();
}).catch(function(err) {
  return asyncRecovery1();
}).then(function() {
  return asyncThing4();
}, function(err) {
  return asyncRecovery2();
}).catch(function(err) {
  console.log("Don't worry about it");
}).then(function() {
  console.log("All done!");
});

TAP - Future in scala

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
var rateQuote = Future {
  exchange.getCurrentValue(USD)
}

val purchase = rateQuote map { quote =>
  if (isProfitable(quote)) exchange.buy(amount, quote)
  else throw new Exception("not profitable")
}

purchase onSuccess {
  case _ => println("Purchased " + amount + " USD")
}

Async/Await - sugar for TAP

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
private static async Task<int> ReadFileAsync(string filePath)
{
    var bytesRead = 0;
    try
    {
        using (var fs = File.OpenRead(filePath))
        {
            var readBuffer = new Byte[fs.Length];
            bytesRead = await fileStream.ReadAsync(readBuffer, 0, (int)fs.Length);             
            Console.WriteLine("{0} bytes from file {1}", bytesRead, filePath);
            return bytesRead;
        }
    }
    catch(Exception)
    {
        Console.WriteLine("Error while reading file {0}.", filePath);
        return bytesRead;
    }
}

whats wrong with asyn/await in c#

  • C# async gotchas
  • Avoid a void
  • Simple deadlocks by newbies
  • Issues with nesting
  • Not obvious where runs the code - Await.Configure()
    • Which part runs synchronously

Compare to originals - async in F#

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
let urlList = [
    "http://www.microsoft.com"
    "http://www.google.com"
    "http://www.amazon.com" ]

let fetchHtmlAsync url = async {
    let uri = new System.Uri(url)
    let webClient = new System.Net.WebClient()
    let! html = webClient.AsyncDownloadString(uri)
    return html
}

let getHtmlList =
    Seq.map fetchHtmlAsync    // Build an Async<'a> for each site
    >> Async.Parallel         // each Async<'a> across different threads
    >> Async.RunSynchronously // Run each Async<'a>, non-blocking wait

let htmlList = urlList |> getHtmlList

Reactive Extensions - another approach to asynchronity

sync/pullasync/push
one
1: 
TResult
1: 
Task<TResult>
multiple
1: 
IEnumerable<TResult>
1: 
IObservable<TResult>
  • The Observer pattern done right
  • Invented in C# by Erik Meyer
  • Choose your plattform:

Rx - Your Mouse is a Database

1: 
2: 
3: 
4: 
5: 
6: 
TextChanges(input)
  .DistinctUntilChanged()
  .Throttle(TimeSpan.FromMilliSeconds(10))
  .Select(word=>Completions(word))
  .Switch()
  .Subscribe(ObserveChanges(output));

Rx - reactive Paint in WPF

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
var mouseDown = Observable
  .FromEvent<MouseButtonEventArgs>(image, "MouseDown")
  .Select(evt => evt.EventArgs.GetPosition(image));
var mouseUp = Observable
  .FromEvent<MouseButtonEventArgs>(image, "MouseUp");
var mouseMove = Observable
  .FromEvent<MouseEventArgs>(image, "MouseMove")
  .Select(evt => evt.EventArgs.GetPosition(this));
var q = mouseDown
  .SelectMany(imageOffset => mouseMove.TakeUntil(mouseUp),
    (imageOffset, pos) => new Point
    {
      X = pos.X - imageOffset.X,
      Y = pos.Y - imageOffset.Y
    })
    .Subscribe(Observer.Create<Point>(p =>
    {
      Canvas.SetLeft(image, p.X);
      Canvas.SetTop(image, p.Y);
    }));

Message queues

Implementations:

Advantages:

  • Fast - some benchmarks
  • Asynchronous - Queue it now, run later
  • Clients in any language,
  • AMPQ - common protocol
  • Decoupling - Separates app logic
  • Resilience: Won't crash whole app
  • Redundancy: Retry failed jobs
  • Guarantees: message survives restart
  • Scalable: workers per job
  • Free monitoring tools

RAM footprint per unit of concurrency (approx)

1.3KB
Haskell ThreadId + MVar (GHC 7.6.3, 64-bit)
2.6 KB
Erlang process (64-bit)
4.0 KB
Go goroutine
9.0 KB
C pthread (minimum, 64-bit Mac OS X)
64.0 KB
Java thread stack (minimum)

513 KB
C pthread (default, 64-bit Mac OS X)
1024 KB
Java thread stack (default)

Akka

  • Inspired from Erlang processes
  • Originally not type safe. Erlang is dynamic
  • Async OOP over messaging - "share nothing"
  • In-process usable too - spray
  • Failure safe
  • Supervision trees
  • Back-pressure - Publisher/Subscriber
  • Low level - extra programming effort
  • Own communication protocol

spray/akka-http

  • Both
    • Scala-Frameworks
    • Actor-Based - Akka
    • Performant enough - at least for us
    • Routing DSL
    • Test-Libraries - akka-testkit, spray-testkit, akka-http-testkit

spray

  • Production-Ready
  • Well documented
  • spray-testkit
  • No more features - only support
  • No WebSockets - most requested feature ever

akka-http

  • Successor development of Spray
    • 1.0 (Jul 15 2015)
      • Much slower than spray
    • 2.0-M1 - (Nov 05 2015)
      • API unification - routing, marshalling, http
    • 2.0 - soon?
  • Not yet complete docs - fallback to spray docs
  • Additional features:
    • WebSockets
    • Streams

akka-http - WebSockets

  • No long polling
  • Persistent Client-Server connection
  • Full-Duplex
  • Much smaller header - efficient communication
1: 
2: 
3: 
4: 
5: 
6: 
7: 
path("ws") {
  val handler = Flow.fromSinkAndSource(
      Sink.ignore,
      Source.single(TextMessage("Hello World!"))
    )
  handleWebsocketMessages(handler)
}

akka-streams

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
//types:
Source[Out,Mat]
  Flow[In, Out, Mat]
  Sink[In, Mat]

val ready = Source(someSource)
    .via(flow).map(_ * 2)
    .to(sink)

val mat: Mat = ready.run()

val f: Future[String] = Source.single(1)
    .map (_ .toString)
    .runWith(Sink.head)

akka-streams

1: 
2: 
3: 
4: 
//types:
Source[Int,Unit]
  Flow[Int, String, Unit]
       Sink[String, Future[String]]

Scala Vor/Nachteile

  • PRO
    • FP + OOP
    • Reach type system
    • JVM
    • big community -> a lot of libs
      • Hadoop, Spark, etc.
    • Type classes

Scala - Powerfull type system

1: 
2: 
// a fictional idealized version of the genuine method
def map[B](f: (A) => B) : Map[B]
1: 
2: 
3: 
// the laughably labeled "full" signature
def map[B, That](f: ((A,B)) => B)
  (implicit bf: CanBuildFrom[Map[A,B], B, That]) : That
has FOUR distinct identifiers
neither has any basis in reality!

Scala - predictable

1: 
2: 
3: 
4: 
(Stream from 1) zip (Stream from 1)
  map { case (x,y) => x + y } head
(Stream from 1, Stream from 1).zipped
  map { case (x,y) => x + y } head

F# compared to Scala

  • Simple, conciser syntax - less {}(); more |> >> >>= >=>
  • Async and Actor included
  • Powerful Hindley-Milner type inference
  • No circular dependencies
  • No implicit conversions
  • More FP than OOP
  • Good, fast, free IDEs with intellisense
    • VS Community Edition, VS-Code, Atom, Emacs, Vim, ...
  • Full stack libraries
  • Scripting and REPL

CLR - better JVM

It's a moot point, but:

  • Real generics
  • Real tile recursion optimization
  • Value types
  • Easier native calls - c/c++
  • Runs on linux too +-

F#-only features

  • Computation expressions:
    • async, query, seq, cloud, log, mongo, ...
  • Type providers - for any structured data
    • sql, csv, xml, json, wmi, odata, wsdl, hadoop/hive, typescript, excel, swagger, registry, chess...
  • Measure-types - currency, physical units
  • Discriminated unions - compare to inheritance hell
  • Active patterns - compare to scala unapply

F# Mongo driver

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
let collection : IMongoCollection<Item> = ...

mongo {
    for x in collection do
    update
    set x.Price 0.99
    inc x.Quantity 1
}

Akka.NET

  • Akka for .NET
  • Ported from Scala
  • Written in C#
  • API for F#
  • Portable - mono on Unix
  • Production ready