Created
February 1, 2012 01:49
-
-
Save einblicker/1714469 to your computer and use it in GitHub Desktop.
Rubyでリスト内包表記
This file contains hidden or 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
require 'continuation' | |
def shift | |
callcc {|c1| $ctn.(yield(proc {|v| callcc {|c2| $ctn = c2; c1.(v) } })) } | |
end | |
def reset | |
callcc {|c| $ctn = c; v = yield; $ctn.(v) } | |
end | |
def cmpr | |
reset { [ yield ] } | |
end | |
def range(xs) | |
shift {|k| xs.map{|x| k[x]}.flatten(1) } | |
end | |
def guard(cond) | |
range([]) unless cond | |
end | |
#使用例 | |
cmpr { | |
x = range(1 .. 3) | |
y = range(1 .. 6) | |
guard( x + y == 3 ) | |
[x, y] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment