cont 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +67 -0
- data/lib/cont/single_prompt.rb +64 -0
- data/lib/cont/version.rb +3 -0
- data/lib/cont.rb +26 -0
- metadata +75 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 493e03fe958a5776f33b1bbd144830a66893bd02e75a6b93fc2dc3b836817a51
|
4
|
+
data.tar.gz: 250d5cdad63e5cafd2b90e34ff90af5d6d3b903706a4edec0e6b4667f7ff0031
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e5cee2b1b33182f7935345da4efadbd4567fd4f89e75f6f07b4400b8dc8292102508500caba9d56504e1ceeb1c1d43badcded4004e524541ac5daf0eaa1e1d3e
|
7
|
+
data.tar.gz: 68b9aec29911124c3e5e2a28932512ace12bdace81ac6ff5302b575f1bdef30634e2203657418dfd35d2f9403237281ee57befbded131c4a9a153f29e811cbe7
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2024 Masaya Taniguchi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Cont Library
|
2
|
+
|
3
|
+
## Overview
|
4
|
+
|
5
|
+
The `Cont` module is a Ruby library that provides an implementation of
|
6
|
+
continuations using the `fiber` library. Continuations are advanced control flow
|
7
|
+
constructs that allow you to save the state of a computation at a certain point
|
8
|
+
and then resume it later. This library is useful for managing complex control
|
9
|
+
flows and implementing advanced features like coroutines, generators, and
|
10
|
+
cooperative multitasking.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
To use the `Cont` library, simply require it in your Ruby project:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
require 'cont'
|
18
|
+
```
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
### `Cont.reset`
|
23
|
+
|
24
|
+
`Cont.reset` limits the continuation to the current block.
|
25
|
+
It takes a block of code and returns the result of that block.
|
26
|
+
|
27
|
+
#### Example
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
result = Cont.reset do
|
31
|
+
# Your code here
|
32
|
+
42
|
33
|
+
end
|
34
|
+
puts result # => 42
|
35
|
+
```
|
36
|
+
|
37
|
+
### `Cont.shift`
|
38
|
+
|
39
|
+
`Cont.shift` captures the current continuation and allows you to resume it.
|
40
|
+
It takes a block of code, which should call a lambda to resume the continuation.
|
41
|
+
|
42
|
+
#### Example
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
result = Cont.reset do
|
46
|
+
Cont.shift do |cont|
|
47
|
+
cont.call(42) + 1
|
48
|
+
end
|
49
|
+
end
|
50
|
+
puts result # => 42
|
51
|
+
```
|
52
|
+
|
53
|
+
## Exceptions
|
54
|
+
|
55
|
+
### `Cont::DeadContinuationError`
|
56
|
+
|
57
|
+
This exception is raised when an attempt is made to resume a dead continuation.
|
58
|
+
|
59
|
+
### `Cont::UnexpectedStatusError`
|
60
|
+
|
61
|
+
This exception is raised when an unexpected status is encountered.
|
62
|
+
|
63
|
+
## License
|
64
|
+
|
65
|
+
MIT License
|
66
|
+
|
67
|
+
Copyright (c) 2024 Masaya Taniguchi
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module SinglePrompt
|
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 self.reset(&block)
|
17
|
+
prompt0(&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 self.shift(&block)
|
25
|
+
control0 do |fiber|
|
26
|
+
prompt0 do
|
27
|
+
block.call lambda { |value|
|
28
|
+
prompt0 do
|
29
|
+
raise DeadContinuationError.new unless fiber.alive?
|
30
|
+
run(fiber, :resume, lambda { value })
|
31
|
+
end
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def self.run(fiber, *args)
|
40
|
+
status, value = fiber.resume(*args)
|
41
|
+
case status
|
42
|
+
when :return
|
43
|
+
value
|
44
|
+
when :capture
|
45
|
+
value.call(fiber)
|
46
|
+
else
|
47
|
+
raise UnexpectedStatusError.new("unexpected status: #{status}")
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.prompt0(&block)
|
52
|
+
fiber = Fiber.new do
|
53
|
+
Fiber.yield(:return, block.call())
|
54
|
+
end
|
55
|
+
run(fiber)
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.control0(&block)
|
59
|
+
status, f = Fiber.yield(:capture, block)
|
60
|
+
raise UnexpectedStatusError.new("unexpected status: #{status}") \
|
61
|
+
unless status == :resume
|
62
|
+
f.call()
|
63
|
+
end
|
64
|
+
end
|
data/lib/cont/version.rb
ADDED
data/lib/cont.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# The Cont module provides methods for working with continuations.
|
2
|
+
# Continuations are a way to save the execution state of a program
|
3
|
+
# so that it can be resumed later. They are used for advanced control
|
4
|
+
# flow structures such as coroutines, generators, and so on.
|
5
|
+
#
|
6
|
+
# Ruby have a built-in support for continuations, but it is deprecated
|
7
|
+
# and should not be used. This implementation uses the 'fiber' library
|
8
|
+
# based on https://github.com/minoki/delimited-continuations-in-lua .
|
9
|
+
# That library is released under the MIT license:
|
10
|
+
# https://github.com/minoki/delimited-continuations-in-lua/blob/master/LICENSE .
|
11
|
+
#
|
12
|
+
# Caution 1: The continuations of this implementation are 'one-shot',
|
13
|
+
# So they can only be resumed once. If you try to resume a dead
|
14
|
+
# continuation, an exception will be raised.
|
15
|
+
#
|
16
|
+
# Caution 2: This implementation is based on the 'fiber' library,
|
17
|
+
# so you should not crate a new fiber in a continuation block.
|
18
|
+
#
|
19
|
+
# Copyright (c) 2024 Masaya Taniguchi
|
20
|
+
# This software is released under the MIT License.
|
21
|
+
|
22
|
+
require_relative 'cont/single_prompt'
|
23
|
+
|
24
|
+
module Cont
|
25
|
+
include SinglePrompt
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cont
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Masaya Taniguchi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-06-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.10'
|
27
|
+
description: |
|
28
|
+
The Cont module provides methods for working with continuations.
|
29
|
+
Continuations are a way to save the execution state of a program
|
30
|
+
so that it can be resumed later. They are used for advanced control
|
31
|
+
flow structures such as coroutines, generators, and so on.
|
32
|
+
|
33
|
+
Ruby have a built-in support for continuations, but it is deprecated
|
34
|
+
and should not be used. This implementation uses the 'fiber' library
|
35
|
+
based on https://github.com/minoki/delimited-continuations-in-lua .
|
36
|
+
That library is released under the MIT license.
|
37
|
+
|
38
|
+
Caution: The continuations of this implementation are 'one-shot',
|
39
|
+
So they can only be resumed once. If you try to resume a dead
|
40
|
+
continuation, an exception will be raised.
|
41
|
+
email:
|
42
|
+
- ta2ghc@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- LICENSE.txt
|
48
|
+
- README.md
|
49
|
+
- lib/cont.rb
|
50
|
+
- lib/cont/single_prompt.rb
|
51
|
+
- lib/cont/version.rb
|
52
|
+
homepage: https://github.com/tani/ruby-cont
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata: {}
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubygems_version: 3.3.25
|
72
|
+
signing_key:
|
73
|
+
specification_version: 4
|
74
|
+
summary: Cont provides methods for working with continuation
|
75
|
+
test_files: []
|