Pipeline style

A very common idiom is "pipeline code". The simplest code in a stack language is where each word takes a single value from the stack and leaves a new value there; such words can be chained together much like commands in the Unix shell. For example, here is some Factor code:

"/etc/passwd" ascii file-lines
[ "#" head? not ] filter
[ ":" split first ] map

This is very similar to the following Unix shell script:

cat /etc/passwd | grep -v '^#' | sed -e 's/:.*//'

However of course Factor is more expressive and has a simpler syntax.

A variation on pipeline code can be found in the UI framework. Here, we are building a tree of gadgets, so it is not quite a pipeline: we create child gadgets, build them up, then add them to the parent using add-gadget ( parent child -- parent ). Here is an elaborate example:

    "Hello" <label> add-gadget
    "Click me" [ drop beep ] <bevel-button> add-gadget
        "A" <label>
            red solid >>interior
        "B" <label>
            green solid >>interior
        "C" <label>
            green solid >>interior
5 <border>
    black solid >>boundary
"Test" open-window

Notice it reads almost like a declarative description, but really it is just stack code.

This revision created on Fri, 2 Jan 2009 22:06:30 by slava