This is how an Erlang-like actor could look in Scala.
This is how an Erlang-like actor could look in Scala: case class ActorState(val s: State)(body: State => OptionState) { // immutable @tailrec def loop(s1: State) { body(s1) match { case Some(s2) => loop(s2) case None => () } } def act = loop(s) } def Bot(controller: Actor) = Actor(controller) { s => val res = // do the calculations controller! (this, res) None // finish work } val Controller = Actor(MapBot, ResultType()) {s => // start bots, perhaps using results already stored in s if ( // time to stop, e.g. All bots already finished ) None else receive { case (bot, res) => Some(s + (bot -> res)) // a bot has reported result } } Controller.act.
Thanks for this answer. I played around with your example and altered it to my needs, and it seems to be the perfect solution to my specific problem! – Jens K.
Aug 30 '10 at 18:37.
Actors usually have internal state, being, themselves, mutable beasts. Note that actors are not a FP thing. The setup you describe seems to rely on a mutable controller, and it is difficult to get around it in a language that is not non-strict by default.
Depending on what you are doing, though, you could rely on futures. For example: case Msg(info) => val v1 = new Bot! Fn1(info) val v2 = new Bot!
Fn2(info) val v3 = new Bot! Fn3(info) val v4 = new Bot! Fn4(v1(), v2(), v3()) reply(v4()) In this case -- because!
Returns a Future -- v1, v2 and v3 will be computed in parallel. The message Fn4 is receiving as parameters the futures applied, meaning it will wait until all values are computed before it starts computing. Likewise, the reply will only be sent after v4 has been computed, as the future has been applied for as well.
A really functional way of doing these things is the functional reactive programming, or FRP for short. It is a different model than actors. The beauty of Scala, though, is that you can combine such paradigms to the extent that better fits your problem.
Thanks first of all, the "Future" thing is new to me and I will look into it, although I think that it might not really help me.. But, I will first of all have a look into FRP which may be the answer to all my problems ( since I want to learn FP, not the usage of Actors :) ) – Jens K. Aug 29 '10 at 18:21 "and it is difficult to get around it in a language that is not non-strict by default" You can just do what Erlang does: pass the state as a parameter to a tail-recursive function. – Alexey Romanov Aug 29 '10 at 21:40 @Alexey I'm not sure exactly how that would work.
Why not provide an answer with an example? – Daniel C. Sobral Aug 30 '10 at 14:21 I gave an example.
– Alexey Romanov Aug 30 '10 at 16:16.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.