Last active
August 29, 2015 14:13
-
-
Save ken-okabe/f772809741fe5f8feec5 to your computer and use it in GitHub Desktop.
圏論の考え方でつくられた、JavaScriptで動作する純粋関数型プログラミング言語 spinoza (スピノザ) ref: http://qiita.com/kenokabe/items/61d156005c7831c693cd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
world = $('hello')(out); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
world = $('hello')(out); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var compute = function(fs) | |
{ | |
//........ | |
return compute(fs1); | |
//........ | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
//--------- | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
world = $(1)(out); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var out = function(fs) | |
{ | |
var z = compute(fs); | |
info('world', z); | |
return z; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
//=========================================== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//------------------------------- | |
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 | |
//=========================================== | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> (+ 1 2) | |
3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(1 2 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>(list 1 2 3) | |
(1 2 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
world = $('hello'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> (quote (1 2 3)) | |
(1 2 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> '(1 2 3) | |
(1 2 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(quote (1 2 3)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( | |
"hello World" | |
(map (CONSOLE)) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
("hello World") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hello World |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( | |
"hello World" | |
(map (CONSOLE)) | |
(map (CONSOLE)) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
("hello World") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hello World | |
hello World |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(3 (== (3)) ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fseq = function(s) | |
{ | |
var fs = function() {}; | |
fs.s = s; | |
return fs; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(3 (== (5)) ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(false) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(2 (> (1)) ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(2 (< (1)) ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(false) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( | |
3 (==(3)) | |
( | |
if ( | |
("match") | |
("mismatch") | |
) | |
) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
("match") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( | |
3 (==(5)) | |
( | |
if ( | |
("match") | |
("mismatch") | |
) | |
) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fseq = function(s) | |
{ | |
var fs = function() {}; | |
fs.s = s; | |
return fs; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
("mismatch") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( | |
3 (==(5)) | |
( | |
if ( | |
("case1") | |
( | |
3 (==(3)) | |
( | |
if ( | |
("case2") | |
( | |
3 (==(3)) | |
( | |
if ( | |
("case3") | |
("mismatch") | |
) | |
) | |
) | |
) | |
) | |
) | |
) | |
) | |
) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( | |
3 (==(5)) | |
( | |
if ( | |
("case1") | |
( | |
3 (==(1)) | |
( | |
if ( | |
("case2") | |
( | |
3 (==(3)) | |
( | |
if ( | |
("case3") | |
("mismatch") | |
) | |
) | |
) | |
) | |
) | |
) | |
) | |
) | |
) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
("case3") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
world = $(1)(out); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
world = $(1)(2)(3)(out); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
world = $(1)(out); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
world = $(1)(2)(3)(out); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment