Class: Ruff::Handler
- Inherits:
-
Object
- Object
- Ruff::Handler
- Defined in:
- lib/ruff/handler.rb
Overview
In algebraic effects, handler is an first-class object.
Instance Method Summary collapse
-
#initialize ⇒ Handler
constructor
A new instance of Handler.
-
#on(eff, &fun) ⇒ Handler<A!{Effect<Arg, Ret>, e}, B!{e}>
sets or updates effec handler
&fun
foreff
. -
#run(&prc) ⇒ B
handles the computation.
-
#to(&fun) ⇒ Handler<A!{e}, B!{e'}>
sets value handler
&fun
.
Constructor Details
#initialize ⇒ Handler
Returns a new instance of Handler.
38 39 40 41 |
# File 'lib/ruff/handler.rb', line 38 def initialize @handlers = Store.new @valh = ->(x) { x } end |
Instance Method Details
#on(eff, &fun) ⇒ Handler<A!{Effect<Arg, Ret>, e}, B!{e}>
sets or updates effec handler &fun
for eff
Note that eff
can be a supertype of an effect to be caught.
102 103 104 105 106 |
# File 'lib/ruff/handler.rb', line 102 def on(eff, &fun) @handlers[eff] = fun self end |
#run(&prc) ⇒ B
handles the computation.
131 132 133 134 135 |
# File 'lib/ruff/handler.rb', line 131 def run(&prc) co = Fiber.new(&prc) continue(co).call(nil) end |
#to(&fun) ⇒ Handler<A!{e}, B!{e'}>
sets value handler &fun
.
Value handler is the handler for the result value of the computation.
For example, Handler.new.to{|_x| 0}.run { value }
results in 0
.
The value handler modifies the result of the call of continuation in effect handlers of the handler.
77 78 79 80 81 |
# File 'lib/ruff/handler.rb', line 77 def to(&fun) @valh = fun self end |