Skip to content

Instantly share code, notes, and snippets.

@mnzk
Last active August 29, 2015 14:14
Show Gist options
  • Save mnzk/67ab40a73dcd6b7ce054 to your computer and use it in GitHub Desktop.
Save mnzk/67ab40a73dcd6b7ce054 to your computer and use it in GitHub Desktop.
// spinoza for Nashorn shell (jjs)
// original code => https://github.com/kenokabe/spinoza
// [usage example]
//
// > jjs
// jjs> load("spnz.js")
// ===== spinoza =====
// jjs> world = $(1)(2)(3)(out)
// world-> 1,2,3
var console = {
info: print,
log: print
};
(function() {
'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(obj) {
var i = 0;
var s = [obj];
var fs = function(obj) {
s[++i] = obj;
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 }
var length = fs.s.length;
if (length === 1) { return fs.s }
var last_s = fs.s[length - 1];
if (type(last_s) != 'Function') { return fs.s; }
var s = fseq( fs.s.slice(0, length - 1) );
return compute( last_s(s) );
};
var out = function(fs) {
var z = compute(fs);
info('world', z);
return z;
};
Object.defineProperties(
this,
{
world: { set: function(fs) { return compute(fs) } }
});
this.$ = $;
this.out = out;
this.compute = compute;
//module.exports = spinoza;
}.call(this));
// library
var plus10 = function(fs) {
var f = function(x) { return x + 10 };
return fs.s.map(f)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment