Functional Programming
&
Actors

by @holsee

with examples in Elixir

What I will Cover

1. Actor Theory
2. Theory Applied in Elixir
3. Code Examples
4. Coding Challenge

So what are Actors?

the Theory...

Actors are...
A Fundamental

Unit of

Computation

They embody the
3 essential
units of computation

  1. Processing

  2. Storage

  3. Communication

"One Ant is no Ant"

E. O. Wilson

"One Actor is no Actor"

Carl Hewitt

"...they come in Systems."

Carl Hewitt

Actors are nothing alone but everything together

In order to be together, they must be able to communicate

In order to communicate
Actors must have an Address which allows an Actor to send Messages to another Actor

If the fundamental unit of computation is an Actor then a Mailbox must be an Actor that has its own Mailbox

In order to bring sanity to
"everything is an Actor, and Actors have Mailboxes, which are also Actors which then must have Mailboxes of their own..."

There are the following truths:
 

Mailbox Axoims

 

  1. Mailboxes Can Create more Actors
  2. Send Message to Actors that it has addresses for
  3. Designates what it will do with the next message it receives


i.e. Mailboxes are Special Actors

What happens if an Actors sends a Message to itself?

Will it deadlock?

In walks Futures which provides a reference to a result which has not yet been computed.

 

And in the context of Actors that boils down to an Actor which has an address, that will hold the result, when it is available.

Futures IRL

 

A Future for a Bushell of Wheat (not now but in the Future),
when the harvest takes it will resolve to a Bushell of Wheat!

 


But if there is a drought and there will be no Wheat,
the Future is Broken.
 

A computational Future is a reference to
 a result that is not available yet.
If the result fails to be produced you get an error instead

but you didn't have to block waiting for the result... but you may use the result before it becomes available.

So by passing the reference to the result we can continue processing until we actually need the value held within the Future.

So in theory if you needed a result from yourself, you could use a Future to wrap the request to self and not deadlock waiting for the response.

Remember Actors are the fundamental unit of computation... so we model Futures as Actors too

So in order to implement a Future in the Actor Model, you have an Actor which performs the Processing and Stores the result and Communicates when it is done.

Processing

Processes
&
Functions!

defmodule Greeter do

  def hello(name) do
    "hi #{name}"
  end

end

f

input

output

iex(1)> Greeter.hello("world")

"hi world"

Storage

Processes

+
Recursion

=

Storage!

Other Actors by Reference
(e.g. Futures) and
other "Special" Processes
e.g. ETS tables

Communication

Message Passing!

Recieve Blocks!

GenServers!

Addresses

Process References!

Code Demo!


Processing
Storage
Communication

Futures

Values held at
Process References

Will resolve to a Value in the Future or Fail

Code Demo!

Futures in Elixir

References

 

♡ thanks

 


 

Using a Functional Language and an Actor Model
Implement a "Greeter" which:

 

1. Can say hello to you.

 

2. Remembers who it said Hello to and can tell you when asked.

 

3. Can be told to forget who they have said Hello to.

4. Can run concurrently with other Greeters.

Coding Challenge!

FP with Actors in Elixir

By Steven Holdsworth

FP with Actors in Elixir

  • 241