all repos — h3rald @ 82c8911701524e43ff149d2297e3d62ccd3cf07e

The sources of https://h3rald.com

content/concatenative.textile

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
----- 
permalink: concatenative
filters_pre: 
- erb
- redcloth
title: Concatenative
comments: []

date: 2009-03-28 07:21:00 +01:00
tags: []

type: page
toc: true
-----
Concatenative is a Ruby DSL(Domain-specific Language) for concatenative programming. It is heavily inspired by "Joy":http://www.latrobe.edu.au/philosophy/phimvt/joy.html, 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.

h3. Installation

The simplest method to install Concatenative is to install the gem:

@gem install concatenative@

h3. Usage

Initialization:

<% highlight :ruby do %>
require 'concatentive'
<% end %>

Execute a Concatenative program:

<% highlight :ruby do %>
        concatenate(
                10,
                [0, :==],
                [1, :+],
                [:dup, 1, :-],
                [:*],
                :linrec
        )
<% end %>

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.

<% highlight :ruby do %>
                :factorial << [[0, :==], [:pop, 1], [:dup, 1, :- , :factorial, :*], :if]
                [5, :factorial].execute
<% end %>

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:

<% highlight :ruby do %>
        concatenate(
                "Goodbye, World!", /Goodbye/, "Hello", :sub|2
        )
<% end %>

The program above is equivalent to "Goodbye, World!".sub(/Goodbye/, "Hello").


h3. Resources

* "News and Articles":http://www.h3rald.com/tags/concatenative
* "RDoc Documentation":http://concatenative.rubyforge.org [on RubyForge]
* "Download Files":http://rubyforge.org/projects/concatenative [on RubyForge]
* "Source Code and Issue Tracking":http://github.com/h3rald/concatenative [on GitHub]