cont 0.1.1 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +35 -4
- data/lib/cont/multi_prompt.rb +65 -0
- data/lib/cont/single_prompt.rb +11 -12
- data/lib/cont/version.rb +1 -1
- data/lib/cont.rb +5 -6
- metadata +13 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 765c239f2c9fc352c85314515572a7c4d891e4bc8b450ab82fa485350f945f76
|
4
|
+
data.tar.gz: e920adca4ed74172195f4dc41d70a78f676859e4eadf02b28bd780dee0dc72d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 444664087727c3c9bdbf5a8453ee6e8ac5c2935fe4b1ff0a10c07342248e2b2994cd3a931294194504fb773924fa06a8ef946f7adfe9f2e91a8fc9d4d525efe4
|
7
|
+
data.tar.gz: 3d9c747308320008c8c796a68a13b27243a503d0af07f8b97135f0a7b022955aa17c3edc36b65161830cb2edf8f5272108f29a66a47c7974eea953c3ca04a48d
|
data/README.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
# Cont
|
1
|
+
# Cont
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/cont.svg)](https://badge.fury.io/rb/cont)
|
2
4
|
|
3
5
|
## Overview
|
4
6
|
|
@@ -28,7 +30,6 @@ It takes a block of code and returns the result of that block.
|
|
28
30
|
|
29
31
|
```ruby
|
30
32
|
result = Cont.reset do
|
31
|
-
# Your code here
|
32
33
|
42
|
33
34
|
end
|
34
35
|
puts result # => 42
|
@@ -43,8 +44,38 @@ It takes a block of code, which should call a lambda to resume the continuation.
|
|
43
44
|
|
44
45
|
```ruby
|
45
46
|
result = Cont.reset do
|
46
|
-
Cont.shift do |
|
47
|
-
|
47
|
+
Cont.shift do |k|
|
48
|
+
k.call(42) + 1
|
49
|
+
end
|
50
|
+
end
|
51
|
+
puts result # => 43
|
52
|
+
```
|
53
|
+
|
54
|
+
### `Cont.reset_at`
|
55
|
+
|
56
|
+
`Cont.reset_at` limits the continuation to the current block and assigns it a tag.
|
57
|
+
It takes a tag and a block of code, allowing you to manage multiple continuation points.
|
58
|
+
|
59
|
+
#### Example
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
result = Cont.reset_at(:x) do
|
63
|
+
1 + Cont.shift_at(:x) { |cont| cont.call(2) }
|
64
|
+
end
|
65
|
+
puts result # => 3
|
66
|
+
```
|
67
|
+
|
68
|
+
### `Cont.shift_at`
|
69
|
+
|
70
|
+
`Cont.shift_at` captures the current continuation associated with a specific tag and allows you to resume it.
|
71
|
+
It takes a tag and a block of code, enabling more complex control flow by using multiple continuations.
|
72
|
+
|
73
|
+
#### Example
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
result = Cont.reset_at(:x) do
|
77
|
+
Cont.shift_at(:x) do |k|
|
78
|
+
k.call(42) + 1
|
48
79
|
end
|
49
80
|
end
|
50
81
|
puts result # => 43
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module MultiPrompt
|
2
|
+
|
3
|
+
# The 'fiber' library is required for the implementation of continuations.
|
4
|
+
require 'fiber'
|
5
|
+
|
6
|
+
# Exception for handling dead continuation
|
7
|
+
class DeadContinuationError < StandardError; end
|
8
|
+
|
9
|
+
# Exception for handling unexpected statuses
|
10
|
+
class UnexpectedStatusError < StandardError; end
|
11
|
+
|
12
|
+
# Limit the continuation to the current block.
|
13
|
+
#
|
14
|
+
# @yield [block] The block of code to be run
|
15
|
+
# @return [Object] The result of the block.
|
16
|
+
def reset_at(tag, &block)
|
17
|
+
prompt0_at(tag, &block)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Capture the current continuation.
|
21
|
+
#
|
22
|
+
# @yield [block] The block of code to be run
|
23
|
+
# @return [Object] The result of the block.
|
24
|
+
def shift_at(tag, &block)
|
25
|
+
control0_at(tag) do |fiber|
|
26
|
+
prompt0_at(tag) do
|
27
|
+
block.call lambda { |value|
|
28
|
+
prompt0_at(tag) do
|
29
|
+
raise DeadContinuationError.new unless fiber.alive?
|
30
|
+
run_at(nil, fiber, :resume, lambda { value })
|
31
|
+
end
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def run_at(tag, fiber, *args)
|
38
|
+
case fiber.resume(*args)
|
39
|
+
in :return, value
|
40
|
+
value
|
41
|
+
in :capture, ^tag, value
|
42
|
+
value.call(fiber)
|
43
|
+
in :capture, other_tag, value
|
44
|
+
run_at(tag, fiber, Fiber.yield(:capture, other_tag, value))
|
45
|
+
else
|
46
|
+
raise UnexpectedStatusError.new("unexpected status")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def prompt0_at(tag, &block)
|
51
|
+
fiber = Fiber.new do
|
52
|
+
Fiber.yield(:return, block.call())
|
53
|
+
end
|
54
|
+
run_at(tag, fiber)
|
55
|
+
end
|
56
|
+
|
57
|
+
def control0_at(tag, &block)
|
58
|
+
status, f = Fiber.yield(:capture, tag, block)
|
59
|
+
raise UnexpectedStatusError.new("unexpected status: #{status}") \
|
60
|
+
unless status == :resume
|
61
|
+
f.call()
|
62
|
+
end
|
63
|
+
|
64
|
+
module_function :reset_at, :shift_at, :run_at, :prompt0_at, :control0_at
|
65
|
+
end
|
data/lib/cont/single_prompt.rb
CHANGED
@@ -13,7 +13,7 @@ module SinglePrompt
|
|
13
13
|
#
|
14
14
|
# @yield [block] The block of code to be run
|
15
15
|
# @return [Object] The result of the block.
|
16
|
-
def
|
16
|
+
def reset(&block)
|
17
17
|
prompt0(&block)
|
18
18
|
end
|
19
19
|
|
@@ -21,7 +21,7 @@ module SinglePrompt
|
|
21
21
|
#
|
22
22
|
# @yield [block] The block of code to be run
|
23
23
|
# @return [Object] The result of the block.
|
24
|
-
def
|
24
|
+
def shift(&block)
|
25
25
|
control0 do |fiber|
|
26
26
|
prompt0 do
|
27
27
|
block.call lambda { |value|
|
@@ -34,31 +34,30 @@ module SinglePrompt
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
status, value = fiber.resume(*args)
|
41
|
-
case status
|
42
|
-
when :return
|
37
|
+
def run(fiber, *args)
|
38
|
+
case fiber.resume(*args)
|
39
|
+
in :return, value
|
43
40
|
value
|
44
|
-
|
41
|
+
in :capture, value
|
45
42
|
value.call(fiber)
|
46
43
|
else
|
47
|
-
raise UnexpectedStatusError.new("unexpected status
|
44
|
+
raise UnexpectedStatusError.new("unexpected status")
|
48
45
|
end
|
49
46
|
end
|
50
47
|
|
51
|
-
def
|
48
|
+
def prompt0(&block)
|
52
49
|
fiber = Fiber.new do
|
53
50
|
Fiber.yield(:return, block.call())
|
54
51
|
end
|
55
52
|
run(fiber)
|
56
53
|
end
|
57
54
|
|
58
|
-
def
|
55
|
+
def control0(&block)
|
59
56
|
status, f = Fiber.yield(:capture, block)
|
60
57
|
raise UnexpectedStatusError.new("unexpected status: #{status}") \
|
61
58
|
unless status == :resume
|
62
59
|
f.call()
|
63
60
|
end
|
61
|
+
|
62
|
+
module_function :reset, :shift, :run, :prompt0, :control0
|
64
63
|
end
|
data/lib/cont/version.rb
CHANGED
data/lib/cont.rb
CHANGED
@@ -20,12 +20,11 @@
|
|
20
20
|
# This software is released under the MIT License.
|
21
21
|
|
22
22
|
require_relative 'cont/single_prompt'
|
23
|
+
require_relative 'cont/multi_prompt'
|
23
24
|
|
24
25
|
module Cont
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
SinglePrompt.shift(&block)
|
30
|
-
end
|
26
|
+
include SinglePrompt
|
27
|
+
include MultiPrompt
|
28
|
+
module_function :reset, :shift, :run, :prompt0, :control0
|
29
|
+
module_function :reset_at, :shift_at, :run_at, :prompt0_at, :control0_at
|
31
30
|
end
|
metadata
CHANGED
@@ -1,29 +1,35 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cont
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Masaya Taniguchi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-06-
|
11
|
+
date: 2024-06-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: minitest
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5.23'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 5.23.1
|
20
23
|
type: :development
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
27
|
- - "~>"
|
25
28
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
29
|
+
version: '5.23'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 5.23.1
|
27
33
|
description: |
|
28
34
|
The Cont module provides methods for working with continuations.
|
29
35
|
Continuations are a way to save the execution state of a program
|
@@ -47,6 +53,7 @@ files:
|
|
47
53
|
- LICENSE.txt
|
48
54
|
- README.md
|
49
55
|
- lib/cont.rb
|
56
|
+
- lib/cont/multi_prompt.rb
|
50
57
|
- lib/cont/single_prompt.rb
|
51
58
|
- lib/cont/version.rb
|
52
59
|
homepage: https://github.com/tani/ruby-cont
|
@@ -68,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
75
|
- !ruby/object:Gem::Version
|
69
76
|
version: '0'
|
70
77
|
requirements: []
|
71
|
-
rubygems_version: 3.
|
78
|
+
rubygems_version: 3.5.9
|
72
79
|
signing_key:
|
73
80
|
specification_version: 4
|
74
81
|
summary: Cont provides methods for working with continuation
|