Skip to content

Instantly share code, notes, and snippets.

@einblicker
Created February 1, 2012 01:49
Show Gist options
  • Save einblicker/1714469 to your computer and use it in GitHub Desktop.
Save einblicker/1714469 to your computer and use it in GitHub Desktop.
Rubyでリスト内包表記
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