Module: Ruff::Standard::DelimCtrl

Defined in:
lib/ruff/standard/delim_ctrl.rb

Overview

DelimCtrl provides one-shot delimited control operators, shift and reset.

Examples:

DelimCtrl.reset {
  puts "hello"
  DelimCtrl.shift { |k|
    k.call
    puts "!"
  }

  puts "world"
}
# ==>
#   hello
#   world
#   !

Class Method Summary collapse

Class Method Details

.reset(&th) ⇒ Object

delimits a continuation

Parameters:

  • th (Proc<(), A>)

    is a thunk. In this thunk shift captures a continuation delimited with the thunk.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruff/standard/delim_ctrl.rb', line 25

def reset(&th)
  eff = Ruff::Effect.new
  @stack.push eff

  ret = Ruff.handler
            .on(eff) do |k, f|
              f.call(k)
            end
            .run(&th)

  @stack.pop
  ret
end

.shift(&k) ⇒ Object

captures a continuation.

Parameters:

  • k (Proc<Proc<C, A>, A/B>)

    is a continuation.



42
43
44
45
46
# File 'lib/ruff/standard/delim_ctrl.rb', line 42

def shift(&k)
  # fetch nearmost prompt
  top = @stack.last
  top.perform(k)
end