Front Page All Articles Recent Changes Random Article

Contents

Concatenative language

  • ACL
  • Ait
  • Aocla
  • Breeze
  • Callisto
  • Cat
  • Cognate
  • colorForth
  • Concata
  • CoSy
  • Deque
  • DSSP
  • dt
  • Elymas
  • Enchilada
  • ETAC
  • F
  • Factor
  • Fiveth
  • Forth
  • Fourth
  • Freelang
  • Gershwin
  • hex
  • iNet
  • Joy
  • Joy of Postfix App
  • kcats
  • Kitten
  • lang5
  • Listack
  • LSE64
  • Lviv
  • Meow5
  • min
  • Mirth
  • mjoy
  • Mlatu
  • Ode
  • OForth
  • Om
  • Onyx
  • Plorth
  • Popr
  • Porth
  • PostScript
  • Prowl
  • Quest32
  • Quackery
  • r3
  • Raven
  • Retro
  • RPL
  • SPL
  • Staapl
  • Stabel
  • Tal
  • Titan
  • Trith
  • Uiua
  • Worst
  • xs
  • XY
  • 5th
  • 8th

Concatenative topics

  • Compilers
  • Interpreters
  • Type systems
  • Object systems
  • Quotations
  • Variables
  • Garbage collection
  • Example programs

Concatenative meta

  • People
  • Communities

Other languages

  • APL
  • C++
  • Erlang
  • FP trivia
  • Haskell
  • Io
  • Java
  • JavaScript
  • Lisp
  • ML
  • Oberon
  • RPL
  • Self
  • Slate
  • Smalltalk

Meta

  • Search
  • Farkup wiki format
  • Etiquette
  • Sandbox

Pipeline style

Suppose we wish to compose a sequence of functions A, B, C, D. In a concatenative language, you just write

A B C D

In an applicative language, you'd write something like the following:

(D (C (B (A x))))

This looks "backwards", because one must read it from right to left; A is the first function to execute, then B, then C, then D. Haskell has an infix operator for function composition, so the excessive nesting is gone but the code still looks "backwards":

D . C . B . A

In the Factor community, we call such code "pipeline code".

Pipeline code is one of the simplest idioms in a stack language; you have a sequence of words, 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/:.*//'

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:

USING: ui.gadgets.packs ui.gadgets.labels ui.gadgets.borders
ui.gadgets ui.gadgets.buttons ui.backend ui.render ui colors ;

<pile>
    "Hello" <label> add-gadget
    "Click me" [ drop beep ] <bevel-button> add-gadget
    <shelf>
        "A" <label>
            red <solid> >>interior
        add-gadget
        "B" <label>
            green <solid> >>interior
        add-gadget
        "C" <label>
            blue <solid> >>interior
        add-gadget
    add-gadget
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 Sun, 4 Jan 2009 15:09:15 by jkleiser (fixed typo)

Latest Revisions Edit

All content is © 2008-2024 by its respective authors. By adding content to this wiki, you agree to release it under the BSD license.