scaleex-scale.html
doubleCode_samples.html
mergeex-merge.html
sum
dxex-dx.html
wiring functionsex-msfl.html
histoex-histo.html
RLEex-rle.html
convolex-conv.html
higher order
actorex-fmap.html
 

actor sum

  in (i:unsigned<8> dc)

  out (o:unsigned<16>)

var state: {Wait, Summing} = Wait

var sum: unsigned<16>

rules

| (state:Wait, i:’<) -> (state:Summing, sum:0)

| (state:Summing, i:’>) -> (state:Wait, o:sum)

| (state:Summing, i:’v) -> (sum:sum+v)

;

This actor operates on a structured stream composed of a sequence of lists, each list starting with a « start of structure » (’<) token and ending with a « end of structure » (’>) token. For each list, it computes the sum of the elements.

For example, if the stream on input i is : 

    < 1 2 3 > < 4 5 > < 6 7 8 >...

then the stream on output o will be :

    6 9 21 ...

For this, it uses pattern matching on the input to detect the start and end of each list and two local variables : a variable state, indicating whether the actor is waiting for a new list or computing the sum, and a accumulator sum keeping track of the running sum.

The three transition rules can be read as follows :

  1. if we are in state Wait and input token is ``’<'', then initialize sum to 0 and go to state Summing

  2. if we are in state Summing and input token is ``’>'', then writes the accumulated sum to output and  go back to state Wait

  3. if we are in state Summing and input token is a data value, then add the corresponding value to the accumulator.