Skip to content

Instantly share code, notes, and snippets.

@ken-okabe
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ken-okabe/f772809741fe5f8feec5 to your computer and use it in GitHub Desktop.
Save ken-okabe/f772809741fe5f8feec5 to your computer and use it in GitHub Desktop.
圏論の考え方でつくられた、JavaScriptで動作する純粋関数型プログラミング言語 spinoza (スピノザ) ref: http://qiita.com/kenokabe/items/61d156005c7831c693cd
world = $('hello')(out);
world = $('hello')(out);
var compute = function(fs)
{
//........
return compute(fs1);
//........
};
var compute = function(fs)
{
//------------
if (type(fs) != 'Function')
{
return fs;
}
else
{
var length = fs.s.length;
var result;
if (length === 1)
{
return fs.s;
}
else
{
var ff = fs.s[length - 1]; //the last fs
var fs0 = fseq(fs.s.slice(0, length - 1)); //fseq the rest
if (type(ff) != 'Function')
{
return fs.s;
}
else
{
var fs1 = ff(fs0);
return compute(fs1);
}
}
}
//---------
};
world = $(1)(out);
var out = function(fs)
{
var z = compute(fs);
info('world', z);
return z;
};
'use strict';
console.log('===== spinoza =====');
var info = function(type, info)
{
console.info(type + '->', info);
};
var type = function(obj)
{
return Object
.prototype
.toString
.call(obj)
.slice(8, -1);
};
var $ = function(a)
{
var i = 0;
var s = [];
s[i] = a;
var fs = function(a)
{
i++;
s[i] = a;
fs.s = s;
return fs;
};
fs.s = s;
return fs;
};
var fseq = function(s)
{
var fs = function() {};
fs.s = s;
return fs;
};
var compute = function(fs)
{
//------------
if (type(fs) != 'Function')
{
return fs;
}
else
{
var length = fs.s.length;
var result;
if (length === 1)
{
return fs.s;
}
else
{
var ff = fs.s[length - 1]; //the last fs
var fs0 = fseq(fs.s.slice(0, length - 1)); //fseq the rest
if (type(ff) != 'Function')
{
return fs.s;
}
else
{
var fs1 = ff(fs0);
return compute(fs1);
}
}
}
//---------
};
var out = function(fs)
{
var z = compute(fs);
info('world', z);
return z;
};
if (typeof(window) === 'undefined')
{
console.log('in node/iojs');
global.window = global;
}
else
{
console.log('in browser');
}
Object.defineProperties(window,
{
world: //our physical world
{
set: function(fs)
{
return compute(fs); //compute fs
}
}
});
var plus10 = function(fs)
{
var _ = require('lodash'); //take advantage of any npm great heritage!
var f = function(x)
{
return x + 10;
};
var z = _.map(fs.s, f);
return z;
};
//===========================================
world = $('hello')(out);
//[ 'hello' ]
world = $(1)(out);
// [ 1 ]
world = $(1)(2)(3)(out);
// [ 1, 2, 3 ]
world = $(1)(plus10)(out);
// [ 11 ]
world = $(1)(2)(3)(plus10)(out);
// [ 11, 12, 13 ]
world = $('hello');
// nothing happens
world = $('hello')(out)(out);
// hello twice
//===========================================
//-------------------------------
var spinoza = require('./app');
var $ = spinoza.$;
var compute = spinoza.compute;
var out = spinoza.out;
var plus10 = function(fs) {
var _ = require('lodash');
var f = function(x) {
return x + 10;
};
var z = _.map(fs.s, f);
return z;
};
var f = function() {
world = $('delay hello')(out);
};
var delay = setTimeout(f, 2000);
//-------------
var input = (function() {
var std = process.stdin
.resume()
.setEncoding('utf8')
.on('data', function(chunk) {
chunk.trim().split('\n').forEach(function(line) { 
world = $(line)(out);
});
})
.on('end', function() {});
})();--------
//===========================================
world = $('hello')(out);
//[ 'hello' ]
world = $(1)(out);
// [ 1 ]
world = $(1)(2)(3)(out);
// [ 1, 2, 3 ]
world = $(1)(plus10)(out);
// [ 11 ]
world = $(1)(2)(3)(plus10)(out);
// [ 11, 12, 13 ]
world = $('hello');
// nothing happens
world = $('hello')(out)(out);
// hello twice
//===========================================
>(list 1 2 3)
(1 2 3)
world = $('hello');
> (quote (1 2 3))
(1 2 3)
> '(1 2 3)
(1 2 3)
(quote (1 2 3))
(
"hello World"
(map (CONSOLE))
)
("hello World")
(
"hello World"
(map (CONSOLE))
(map (CONSOLE))
)
("hello World")
hello World
hello World
(3 (== (3)) )
var fseq = function(s)
{
var fs = function() {};
fs.s = s;
return fs;
};
(3 (== (5)) )
(2 (> (1)) )
(2 (< (1)) )
(
  3 (==(3)) 
(
if (
("match")
("mismatch")
)
)
)
(
  3 (==(5)) 
(
if (
("match")
("mismatch")
)
)
)
var fseq = function(s)
{
var fs = function() {};
fs.s = s;
return fs;
};
("mismatch")
(
  3 (==(5)) 
(
if (
("case1")
(
  3 (==(3)) 
(
if (
("case2")
(
  3 (==(3)) 
(
if (
("case3")
("mismatch")
)
)
)
)
)
)
)
)
)
(
  3 (==(5)) 
(
if (
("case1")
(
  3 (==(1)) 
(
if (
("case2")
(
  3 (==(3)) 
(
if (
("case3")
("mismatch")
)
)
)
)
)
)
)
)
)
world = $(1)(out);
world = $(1)(2)(3)(out);
var $ = function(a)
{
var i = 0;
var s = [];
s[i] = a;
var fs = function(a)
{
i++;
s[i] = a;
fs.s = s;
return fs;
};
fs.s = s;
return fs;
};
world = $(1)(out);
world = $(1)(2)(3)(out);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment