Skip to content

Instantly share code, notes, and snippets.

@bokuo-okubo
Created November 16, 2015 19:07
Show Gist options
  • Save bokuo-okubo/6dde5db7cc0fbe95f908 to your computer and use it in GitHub Desktop.
Save bokuo-okubo/6dde5db7cc0fbe95f908 to your computer and use it in GitHub Desktop.
"use strict";
// count : Number
var count = 0;
function p(obj) {
console.log(obj);
}
// sumcountup5 : Number, Number -> Countup(Number) 副作用なし
function sum_countup5(x, y) {
return {value: x + y, countup: 5}
}
// length_countup: String -> Countup(Number) 副作用なし
function length_countup(s) {
var len = s.length;
return {value: len, countup: len};
}
// countup: Numer -> Countup(Void) 副作用なし
function countup(n) {
return {value: undefined, countup: n};
}
// CountupMain: Countup(T) -> T 副作用あり
function CountupMain(countupRequest) {
count += countupRequest.countup;
return countupRequest.value;
}
p(count);
p(sum_countup5(1, 3));
p(count);
p(length_countup("Hello,World."));
p(count);
/*
* このような型を持つ関数が欲しい
* Countup(Void) countup_ex(Countup(Number))
* Countup(Number) sum_countup5_ex( Countup(Number), Countup(Number) )
*/
// countup_ex: Countup(Number) -> Countup(Void)
function countup_ex(countup_n) {
return {value: undefined, countup: countup_n.countup + countup_n.value};
}
// sum_countup5_ex: Countup(Number), Countup(Number) -> Countup(Number)
function sum_countup5_ex(countup_x, countup_y) {
return {value: countup_x.value + countup_y.value,
countup: countup_x.countup + countup_y.countup + 5};
}
// 整数値からCountup(Number) 型のデータを作る関数を作っておく
// noeffect: Number -> Countup(Number)
function noeffect(n) {
return {value:n, countup:0};
}
var hoge = countup_ex( sum_countup5_ex( noeffect(1), length_countup("Hello, world!") ))
p(hoge);
// ext: ( T -> Countup(S) ) -> ( Countup(T) -> Countup(S) )
function ext(fun) {
return function(/*...*/) {
var valueArgs = [];
var countupTotal = 0;
for ( var i =0; i < arguments.length; i++ ) {
var arg = arguments[i];
valueArgs[i] = arg.value;
countupTotal += arg.countup;
}
var result = fun.apply(null, valueArgs);
result.countup = result.countup + countupTotal;
return result;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment