1 people like it.

Pigeon/Akka immutable actor

This is a Pigeon/Akka actor based on pure (if you remove printf) function instead of inheriting from a class..

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
#r @"Akka.dll"
#r @"Akka.FSharp.dll"

open Akka.FSharp
open Akka.Actor


type FunActor<'State,'Msg>(f ,startState:'State) =
    inherit Actor()
    let mutable state = startState

    override x.OnReceive(msg) =
        state <- f state ( msg :?> 'Msg)


module Actor =
    let system name =
        ActorSystem.Create(name)

    let spawn (system:ActorSystem) (s:'s) (f: 's -> 'm -> 's)  =
       system.ActorOf(Props(Deploy.Local, typeof<FunActor<'s,'m>>, [f;s])) 

let system = Actor.system "This is Akka !"

type Message =
    | Inc of int
    | Dec of int

let actor = 
    Actor.spawn system 0
    <| fun s ->
        printfn "%d" s
        function
        | Inc n-> s + n
        | Dec n -> s - n

[0..1000] |> List.iter(fun _ -> actor <! Inc 2)
[0..1000] |> List.iter (fun _ -> actor <! Dec 1)
namespace Microsoft.FSharp
Multiple items
type FunActor<'State,'Msg> =
  inherit obj
  new : f:'a * startState:'State -> FunActor<'State,'Msg>
  override OnReceive : msg:'a -> 'b

Full name: Script.FunActor<_,_>

--------------------
new : f:'a * startState:'State -> FunActor<'State,'Msg>
val f : 'a
val startState : 'State
override FunActor.OnReceive : msg:'a -> 'b

Full name: Script.FunActor`2.OnReceive
val system : name:'a -> 'b

Full name: Script.Actor.system
val name : 'a
val spawn : system:'a -> s:'s -> f:('s -> 'm -> 's) -> 'b

Full name: Script.Actor.spawn
val system : 'a
val s : 's
val f : ('s -> 'm -> 's)
val typeof<'T> : System.Type

Full name: Microsoft.FSharp.Core.Operators.typeof
val system : obj

Full name: Script.system
module Actor

from Script
type Message =
  | Inc of int
  | Dec of int

Full name: Script.Message
union case Message.Inc: int -> Message
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
union case Message.Dec: int -> Message
val actor : obj

Full name: Script.actor
val s : int
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val n : int
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val iter : action:('T -> unit) -> list:'T list -> unit

Full name: Microsoft.FSharp.Collections.List.iter
Raw view New version

More information

Link:http://fssnip.net/lU
Posted:5 years ago
Author:thinkbeforecoding
Tags: pigeao , akka