this post was submitted on 20 Jan 2024
4 points (100.0% liked)

Concatenative Programming

144 readers
4 users here now

Hello!

This space is for sharing news, experiences, announcements, questions, showcases, etc. regarding concatenative programming concepts and tools.

We'll also take any programming described as:


From Wikipedia:

A concatenative programming language is a point-free computer programming language in which all expressions denote functions, and the juxtaposition of expressions denotes function composition. Concatenative programming replaces function application, which is common in other programming styles, with function composition as the default way to build subroutines.

For example, a sequence of operations in an applicative language like the following:

y = foo(x)
z = bar(y)
w = baz(z)

...is written in a concatenative language as a sequence of functions:

x foo bar baz


Active Languages

Let me know if I've got any of these misplaced!

Primarily Concatenative

Concatenative-ish, Chain-y, Pipe-y, Uniform Function Call Syntax, etc.


Cheat Sheets & Tutorials

Discord

IRC

Wikis

Wikipedia Topics

Subreddits

GitHub Topics

Blogs

Practice

founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] Andy@programming.dev 1 points 9 months ago

Examples from the slides, but in Factor:

USING: kernel math ;
IN: pipeline-demo.calc

: add1 ( x -- x' ) 1 + ;
: square ( x -- x' ) dup * ;
: double ( x -- x' ) 2 * ;

: demo1 ( -- n )
  5
  add1
  square
  double
;

: add ( x y -- n ) + ;
: times ( x y -- n ) * ;

: demo2 ( -- n )
  5
  1 add
  2 times
;
USING: math sequences sequences.extras ;
IN: pipeline-demo.collection

: pipeline ( seq -- n )
  [ 2 + ]
  [ 3 > ] map-filter
  length
;
USING: io kernel math.functions math.parser ranges sequences ;
IN: pipeline-demo.fizzbuzz

: (fizz-buzz) ( i -- str )
  dup [ 3 divisor? ] [ 5 divisor? ] bi
  2dup or [
    [ "Fizz" "" ? ] [ "Buzz" "" ? ] bi*
    append
    nip
  ] [ 2drop number>string ] if
;

: fizz-buzz ( -- )
  35 [1..b]
  [ (fizz-buzz) ] map
  "," join
  print
;
USING: accessors kernel math strings ;
IN: pipeline-demo.person

TUPLE: Person
  { Name string read-only }
  { Email string read-only }
  { Age integer read-only }
;

: person-with-name ( person name -- person' )
  swap [ Email>> ] [ Age>> ] bi Person boa
;

: person-with-email ( person email -- person' )
  swap [ Name>> ] [ Age>> ] bi swapd Person boa
;

: person-with-age ( person age -- person' )
  swap [ Name>> ] [ Email>> ] bi rot Person boa
;

: demo ( -- person )
  "Scott" "s@example.com" 21 Person boa
  "Tom" person-with-name
  "tom@example.com" person-with-email
  42 person-with-age
;
USING: sequences splitting strings ;
IN: pipeline-demo.roman

: >roman-numerals ( n -- str )
  CHAR: I <string>
  "IIIII" "V" replace
  "VV" "X" replace
  "XXXXX" "L" replace
  "LL" "C" replace
  ! special cases:
  "VIIII" "IX" replace
  "IIII" "IV" replace
  "LXXXX" "XC" replace
  "XXXX" "XL" replace
;

: >roman-numerals-alt ( n -- str )
  CHAR: I <string>
  {
    { "IIIII" "V" }
    { "VV" "X" }
    { "XXXXX" "L" }
    { "LL" "C" }
    ! special cases:
    { "VIIII" "IX" }
    { "IIII" "IV" }
    { "LXXXX" "XC" }
    { "XXXX" "XL" }
  } [ first2 replace ] each
;