Skip to content

Transient and the Actor Model (Erlang OTP, Cloud Haskell, Scala Akka)

Alberto edited this page Apr 14, 2016 · 6 revisions

Good question. The actor model communicate via mailboxes, which execute callbacks sort to say. Essentially this produces the callback hell. Taking away the initializations, this is the ping pong example in Erlang: https://gist.github.com/torgeir/3978785

    ping(Receiver) ->
      { pong, Receiver } ! { ping, self() },
      receive
        pong -> io:format("Got pong!~n")
    end.

    pong() ->
      receive

        finished ->
          io:format("Pong finished~n");

        { ping, Sender } ->
          io:format("Got ping!~n"),
          Sender ! pong,
          pong()
      end.

This is the same in Transient: A message is sent to the receiver print "ping" in his console, then send a message to the sender, that print "pong" in his console

    pingPong receiver=  
      wormhole receiver $ do
          teleport
          lliftIO $ print "ping"
          teleport
          lliftIO $ print "pong"

You see that the ping and the pong are composed "monadically" in a single piece of code. The flow is quite understandable. Just start the program in both nodes (initialization code is not shown) This is a distributed program but I can compose this program with any other. This other program stream integers from a remote node from 1 in increasing order:

    streamFrom node= 
       wormhole  node $ do
                  teleport
                  r <- threads 1 $ choose[1..]
                  threadDelay 1000000
                  teleport
                  return r

I can compose both distributed programs:

    composed node =  do
         r <-  (pingPong node >> return 0) <|>  streamFrom node
         lliftIO $ case r of
              0 -> putStrLn "pong was received"
              n -> print n

composed print either the number received or "pong" followed by "pong was received" in the console of the caller.

Clone this wiki locally