Created
November 25, 2015 14:44
-
-
Save kevsmith/2af37b3fde5eb6bd7763 to your computer and use it in GitHub Desktop.
Hacked up call/cc in Elixir
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
iex(1)> f = Contex.Target.compute(10, 20) | |
# c is 30 | |
iex(2)> f.(100) | |
3000 |
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
defmodule Contex.Target do | |
use Contex.Scanner | |
defcc compute(a, b) do | |
c = a + b | |
# Wraps the remainder of the function in a arity 1 function | |
# which allows the caller to rebind b | |
cc! b | |
c * b | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment