all repos — h3rald @ 9187a4dd3e01b0592103432a479da5892ef80ec8

The sources of https://h3rald.com

contents/concatenative.html

 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
-----
title: Concatenative
content-type: project
github: concatenative
status: On Hold
version: 0.2.0
-----
<nav class="project-links">
<p><a href="http://concatenative.rubyforge.org"><span class="caps">DOCUMENTATION</span></a> | <a href="http://rubyforge.org/projects/concatenative"><span class="caps">DOWNLOAD</span></a> | <a href="http://github.com/h3rald/concatenative/tree/master"><span class="caps">SOURCE</span></a> | <a href="http://github.com/h3rald/concatenative/issues"><span class="caps">TRACKING</span></a></p>
</nav>
<ul class="project-data">
	<li>Project Status: <strong>On Hold</strong></li>
	<li>Version: <strong>0.2.0</strong></li>
</ul>
<p>Concatenative is a Ruby <acronym title="Domain-specific Language"><span class="caps">DSL</span></acronym> for concatenative programming. It is heavily inspired by <a href="http://www.latrobe.edu.au/philosophy/phimvt/joy.html">Joy</a>, a minimalist programming language by Manfred von Thun. Like Joy, Concatenative features:</p>
<ul>
	<li>function composition, instead of function application</li>
	<li>quotation, instead of abstraction</li>
	<li>no formal parameters</li>
	<li>no variable assignments</li>
</ul>
<p>Plus, it allows you to use Ruby objects and methods in a concatenative fashion.</p>
<div class="spacer-50"></div>
<h3>Installation</h3>
<p>The simplest method to install Concatenative is to install the gem:</p>
<p><code>gem install concatenative</code></p>
<h3>Usage</h3>
<p>Initialization:</p>
<div class='ruby'><pre><code>require 'concatentive'</code></pre></div><p>Execute a Concatenative program:</p>
<div class='ruby'><pre><code>concatenate(
                10,
                [0, :==],
                [1, :+],
                [:dup, 1, :-],
                [:*],
                :linrec
        )</code></pre></div><p>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.</p>
<div class='ruby'><pre><code>:factorial &lt;&lt; [[0, :==], [:pop, 1], [:dup, 1, :- , :factorial, :*], :if]
                [5, :factorial].execute</code></pre></div><p>The program above calculates the factorial of 5, using explicit recursion.</p>
<p>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:</p>
<ul>
	<li>All operators have an arity of 1</li>
	<li>All other method have an arity of 0</li>
	<li>If a method has a different arity, you must specify it explicitly using the pipe (|) operator.</li>
</ul>
<p>Example:</p>
<div class='ruby'><pre><code>concatenate(
                "Goodbye, World!", /Goodbye/, "Hello", :sub|2
        )</code></pre></div><p>The program above is equivalent to <code>"Goodbye, World!".sub(/Goodbye/, "Hello")</code>.</p>
<h3>Latest Updates</h3>
<ul><li><a href="/articles/concatenative-020/">Concatenative 0.2.0 released</a> &mdash; <time class="timeago" datetime="2009-04-19T07:42:00Z">Sunday, 19 April 2009</time></li>
<li><a href="/articles/concatenative-programming-in-ruby/">Concatenative programming in Ruby</a> &mdash; <time class="timeago" datetime="2009-03-28T06:24:00Z">Saturday, 28 March 2009</time></li>
</ul>