Bounded Buffer Process

LA home
Computing
FP
 PFL
  Examples
  Syntax
  Interpreter
   Buffer

The bounded buffer function, bbuffer, takes as parameters a bound, an input channel, inch, and an output channel, opch, and produces a buffer process. The buffer process sits between the input and output channels and smooths out variations in production and consumption rates in the usual way. When the buffer is empty (size=0) it will only accept from the input channel an item (inch?x) to be stored. When the buffer is full (size=bound) it will only offer to pass on to the output channel (opch!hd buff) the oldest item in the buffer. When the buffer process is neither full nor empty it will either accept another item or ("|") pass one on.

bbuffer = lambda bound. lambda inch. lambda opch.
  let rec
    add = lambda x. lambda L. {add element x to list L}
      if null L then x::nil else (hd L)::(add x (tl L)),

    bb = lambda size. lambda buff.
      if size=0 then                           {empty}
        inch?x       -> bb 1 (x::nil)
      else if size=bound then                   {full}
        opch!hd buff -> bb (size-1) (tl buff)
      else                    {neither full nor empty}
        opch!hd buff -> bb (size-1) (tl buff)   |
        inch?x       -> bb (size+1) (add x buff)

  in  bb 0 nil

{\fB Bounded Buffer. (incomplete program) \fP}

e.g. c1993
www #ad:

pfl...
|   choice
|| parallel
-> sequence
? input act
! output act
chan new channel

↑ © L. Allison, www.allisons.org/ll/   (or as otherwise indicated).
Created with "vi (Linux)",  charset=iso-8859-1,   fetched Wednesday, 24-Apr-2024 06:21:51 UTC.

Free: Linux, Ubuntu operating-sys, OpenOffice office-suite, The GIMP ~photoshop, Firefox web-browser, FlashBlock flash on/off.