miyamuko #2161(2007/08/14 12:54 GMT) [ xtal ] Rating1/1=1.00
not を何回も書けるようにしました。 and とか or みたいに1行メソッドがシンプルに書けるのがいいですね。 あと、xtal は callee があるので無名関数で再帰できるのもいいところ。
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 54 55 56 57
// xtal 0.9.7.1 の inject はバグっているので自作。 Iterator::reduce: method(init, fn){ result: init; this { result = fn(result, it); } return result; } Solver: class { - _expr; initialize: method(_expr) {} and: |a,b| a && b; or: |a,b| a || b; apply_not: method(values) { r: values.clone(); i: 0; _expr.each { if (it == "not") { r[i] = ! r[i]; } else { i++; } } return r[0], r.slice(1, r.length); } eval: method(values) { first, rest: apply_not(values); expr: _expr.select(|e| e != "not"); return expr.zip(rest.each).reduce(first, fun(r, pair) { // this.("and") で and メソッドが取得できる return this.(pair[0])(r, pair[1]); }); } } prodloop: method(n) fiber { (fun(n, xs) { if (n == 0) { yield xs; } else { [true, false] { callee(n - 1, xs ~ [it]); } } })(n, []); } solver: Solver(["and", "or", "not", "not", "not", "and"]); prodloop(4).select(|v| solver.eval(v) ) { it.p; }
Rating1/1=1.00-0+
[ reply ]
miyamuko
#2161()
[
xtal
]
Rating1/1=1.00
// xtal 0.9.7.1 の inject はバグっているので自作。 Iterator::reduce: method(init, fn){ result: init; this { result = fn(result, it); } return result; } Solver: class { - _expr; initialize: method(_expr) {} and: |a,b| a && b; or: |a,b| a || b; apply_not: method(values) { r: values.clone(); i: 0; _expr.each { if (it == "not") { r[i] = ! r[i]; } else { i++; } } return r[0], r.slice(1, r.length); } eval: method(values) { first, rest: apply_not(values); expr: _expr.select(|e| e != "not"); return expr.zip(rest.each).reduce(first, fun(r, pair) { // this.("and") で and メソッドが取得できる return this.(pair[0])(r, pair[1]); }); } } prodloop: method(n) fiber { (fun(n, xs) { if (n == 0) { yield xs; } else { [true, false] { callee(n - 1, xs ~ [it]); } } })(n, []); } solver: Solver(["and", "or", "not", "not", "not", "and"]); prodloop(4).select(|v| solver.eval(v) ) { it.p; }Rating1/1=1.00-0+
[ reply ]