Stateful FIFO process

Mandatory assignment

In the fifo.erl module you’ve implemented a functional FIFO queue. After each push or pop operation on the queue a new version of the queue is returned.

Stateful process

A common pattern in Erlang is to provide services as separate processes. If we keep the FIFO queue as a separate process, we can use the PID to the FIFO process as a reference to the FIFO. When we update the state of the FIFO, the PID remains the same.

File to use

Open the moudle-8/src/sfifo.erl module. This module exports a number of functions. The first of these exported functions is sfifo:new/0 which spawns a new process running the loop/1 function. The state of the loop/1 function is a functional FIFO queue implemented in the module-8/src/fifo.erl module.

EDoc

Look at the generated html documentation for the sfifo.erl module.

Messages and functional interface

To inspect and modify the state of the FIFO queue, messages can be sent to the FIFO process and received in the loop/1 function. It is good practice to hide the message passing protocol inside a functional interface. The following functions are already implemented:

  • sfifo:new/0
  • sfifo:size/1
  • sfifo:empty/1

Todo

Your task is to provide working implementations of the following interface functions:

  • sfifo:pop/1
  • sfifo:push/2

You will also need to make changes to the loop/1 function.

Test driven development

There are a number of EUnit test cases at the end of sfifo.erl file. Repeat the following cycle.

  1. Compile
    1. $ make
    2. If error, make changes and goto 1.
  2. Runt the test:
    1. $ make test_sfifo
    2. If all tests passes, your are done :-)
    3. If one ore more tests fails, make a few changes and goto 1.