
|
./Ruby/Continuations
---FILE: continuation.rb---
def checknext
$queue.pop.call if $queue.size > 0
puts "No Possible Matches"; exit 0
end
def choose(*args)
$queue = [] if $queue.nil?
callcc {|cont| $queue << cont; return args[rand(args.size)] }
args.each {|x| callcc {|cont| $queue << cont; return x } }
checknext
end
def makesure(condition)
if not condition
checknext
end
end
USE:
require 'continuation.rb'
a = choose(1,2,3,4,5)
b = choose(6,7,8,9)
makesure( a == b/2.0 )
puts "a:#{a} b:#{b}" ==> a:3 b:6 or a:4 b:8
(use different makesure tests, or multiple tests to output different results) |
| Copyright © 2005-2006, Joby Bednar. All Rights Reserved Nothing may be reused without my approval. |
You: 38.107.191.96 Now: 9/8/2010 5:52:35 AM |