Concatenative is a Ruby DSL for concatenative programming. It is heavily inspired by Joy, a minimalist programming language by Manfred von Thun. Like Joy, Concatenative features:
- function composition, instead of function application
- quotation, instead of abstraction
- no formal parameters
- no variable assignments
Plus, it allows you to use Ruby objects and methods in a concatenative fashion.
Installation ⇈
The simplest method to install Concatenative is to install the gem:
gem install concatenative
Usage ⇈
Initialization:
require 'concatentive'
Execute a Concatenative program:
concatenate(
10,
[0, :==],
[1, :+],
[:dup, 1, :-],
[:*],
:linrec
)
The program above returns the factorial of 10, computed using the linrec combinator. It is also possible to execute arrays directly and define concatenative programs as symbols.
:factorial << [[0, :==], [:pop, 1], [:dup, 1, :- , :factorial, :*], :if]
[5, :factorial].execute
The program above calculates the factorial of 5, using explicit recursion.
You can use all Ruby methods in Concatenative programs as well, making sure that the right number of arguments (and the method’s receiver) are retrieved from the stack correctly. For this to work, Concatenative must know the arity of the method in advance, so the following rules are applied:
- All operators have an arity of 1
- All other method have an arity of 0
- If a method has a different arity, you must specify it explicitly using the pipe (|) operator.
Example:
concatenate("Goodbye, World!", /Goodbye/, "Hello", :sub|2)
The program above is equivalent to "Goodbye, World!".sub(/Goodbye/, "Hello")
.