Created
January 26, 2012 03:48
-
-
Save sdeming/1680865 to your computer and use it in GitHub Desktop.
Use callcc for non-recursive factorials.
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' | |
| class FactorialsInCC | |
| def fact(n) | |
| factorial = 1 | |
| callcc { |fact_jmp| @fact_jmp = fact_jmp } | |
| factorial *= n | |
| n -= 1 | |
| @fact_jmp.call if n > 1 | |
| factorial | |
| end | |
| end | |
| puts FactorialsInCC.new.fact(ARGV[0].to_i) |
Author
Cool stuff. Neat way to queue up some logic.
Careful though; Its kinda like setting a trap. Thing is, set enough traps and you will eventually catch yourself in one.
Author
It gets really cool when you start passing the continuations around.
Haha, i'm just busting your balls. It really is cool stuff. Have you done anything useful with it yet?
Author
Hah, not yet. At least not in Ruby. In Ruby, I think callcc is a solution looking for a problem. A good use case may not actually exist.
That's exactly what I said!!
Author
Did not! Er, where?
I told you it was a trap!
On Thu, Jan 26, 2012 at 10:41 PM, Scott Deming < ***@***.*** > wrote:
Did not! Er, where?
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1680865
##
Justin Mazzi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Been sitting on this a while. Non-recursive using callcc for quick jump back. Obviously you don't need to be recursive to do factorials anyway, but it's a neat alternative to a loop.
@fact_jmp.call = goto
I love goto. It's so efficient.