coroutines 0.2.0 → 0.2.1
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 +4 -4
- data/README.rdoc +33 -7
- data/lib/coroutines/base.rb +0 -1
- data/tests/suite.rb +9 -4
- metadata +20 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2fa7ac85ca8235cd32795dc01769e4203155e3b
|
4
|
+
data.tar.gz: a3e56d66919ec36730231a369c0ec776732f760c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e26d6f223b0e3f0f6e7336ddedaafcaf38e973c36e805de019ce6c0ad3b22577ab5e5df82bad0f45914a9212ff1749f33e17817d627fd6e13c204e9901761ae
|
7
|
+
data.tar.gz: 155ccb30cdd1c8c96b0a132994c5a2dd1e660ba030c0a1cd8850ec77778923461a6b54d60026eb5e08b61106246edede58ab6988a00902a3b7fa46b2a73a0247
|
data/README.rdoc
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
= Coroutines
|
1
|
+
= Coroutines for Ruby
|
2
2
|
A library for creating and composing producer/transformer/consumer coroutines.
|
3
3
|
Producers are already provided by Ruby's built-in Enumerator class; this
|
4
4
|
library provides Transformer and Consumer classes that work analogously. In
|
@@ -6,6 +6,10 @@ particular, they are also based on Fiber and not on threads (as in some other
|
|
6
6
|
producer/consumer libraries). Also provides a module Sink, which is analogous
|
7
7
|
to Enumerable, and Enumerable/Transformer/Sink composition.
|
8
8
|
|
9
|
+
rdoc API docs:: http://nome.github.io/coroutines
|
10
|
+
source code:: https://github.com/nome/coroutines
|
11
|
+
bug tracker:: https://github.com/nome/coroutines/issues
|
12
|
+
|
9
13
|
== Installing
|
10
14
|
gem install coroutines
|
11
15
|
|
@@ -44,6 +48,27 @@ basic library API. Of course, a counter could just as easily be implemented
|
|
44
48
|
using a closure; the advantage of a consumer is that the implementation could
|
45
49
|
involve arbitrary control structures with multiple calls to await.
|
46
50
|
|
51
|
+
Often, a consumer can be rewritten using an enumerator. In the above example,
|
52
|
+
we could also write
|
53
|
+
|
54
|
+
def counter(start, values)
|
55
|
+
result = start
|
56
|
+
values.each { |x| result += x }
|
57
|
+
"Final value: #{result}"
|
58
|
+
end
|
59
|
+
|
60
|
+
values = [10, 1000, 100000] # could be an enumerator fetching values lazily
|
61
|
+
counter(10, values)
|
62
|
+
|
63
|
+
Depending on the context, either solution may be more readable. But there's one
|
64
|
+
thing you can do with consumers but not with enumerators: Lazily feeding the
|
65
|
+
same values to more than one function. With enumerators, iteration is driven
|
66
|
+
from the consuming side; with consumers, it is driven by the producing side.
|
67
|
+
Distributing a stream to many consumers (or, more generally, sinks) can be done
|
68
|
+
using Sink::Multicast. See the {Apache log parsing
|
69
|
+
example}[https://github.com/nome/coroutines/blob/master/examples/parse_apache.rb]
|
70
|
+
for a practical application of multicasting.
|
71
|
+
|
47
72
|
A simple transformer:
|
48
73
|
|
49
74
|
require 'coroutines'
|
@@ -164,7 +189,7 @@ where a Proc object in a pipeline is interpreted as if it were an argument to En
|
|
164
189
|
(1..9).lazy.filter_map{|x| x.to_s if x.even? }.out_connect("")
|
165
190
|
|
166
191
|
Apart from saving a few keystrokes (d'oh...), this has a the advantage that all
|
167
|
-
elements of a pipeline are lazy
|
192
|
+
elements of a pipeline are lazy _by_default_. When using map, filter and
|
168
193
|
friends, forgetting to drop a "lazy" in the right place causes this part of the
|
169
194
|
pipeline to become strict (but of course it may still produce the intended
|
170
195
|
results!). This type of bug can be hard to catch - unless you're always
|
@@ -200,9 +225,10 @@ Proc:: define Proc#to_trans, Proc#<= and Proc#>=
|
|
200
225
|
Symbol:: define Symbol#to_trans, Symbol#<= and Symbol#>=
|
201
226
|
|
202
227
|
== Contributing
|
203
|
-
1. Fork it
|
204
|
-
2. Create your feature branch (
|
205
|
-
3. Commit your changes (
|
206
|
-
4.
|
207
|
-
5.
|
228
|
+
1. {Fork it on Github}[https://github.com/nome/coroutines/fork]
|
229
|
+
2. Create your feature branch (<code>git checkout -b my-new-feature</code>)
|
230
|
+
3. Commit your changes (<code>git commit -am 'Add some feature'</code>)
|
231
|
+
4. Make sure unit tests pass (<code>gem install --development coroutines; ruby tests/suite.rb</code>)
|
232
|
+
5. Push to the branch (<code>git push origin my-new-feature</code>)
|
233
|
+
6. Create new Pull Request
|
208
234
|
|
data/lib/coroutines/base.rb
CHANGED
data/tests/suite.rb
CHANGED
@@ -1,4 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
base_dir = File.expand_path(File.join(File.dirname(__FILE__), ".."))
|
2
|
+
lib_dir = File.join(base_dir, "lib")
|
3
|
+
test_dir = File.join(base_dir, "tests")
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(lib_dir)
|
6
|
+
|
7
|
+
require 'test/unit'
|
8
|
+
|
9
|
+
exit Test::Unit::AutoRunner.run(true, test_dir)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coroutines
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Knut Franke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: lazy_enumerator
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: test-unit
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
27
41
|
description: |
|
28
42
|
A library for creating and composing producer/transformer/consumer coroutines.
|
29
43
|
Producers are already provided by Ruby's built-in Enumerator class; this
|
@@ -43,11 +57,13 @@ files:
|
|
43
57
|
- lib/coroutines/operators.rb
|
44
58
|
- lib/coroutines/sink.rb
|
45
59
|
- tests/suite.rb
|
46
|
-
homepage:
|
60
|
+
homepage: http://nome.github.io/coroutines
|
47
61
|
licenses:
|
48
62
|
- Ruby
|
49
63
|
- BSD-2-Clause
|
50
|
-
metadata:
|
64
|
+
metadata:
|
65
|
+
issue_tracker: https://github.com/nome/coroutines/issues
|
66
|
+
source_code: https://github.com/nome/coroutines
|
51
67
|
post_install_message:
|
52
68
|
rdoc_options: []
|
53
69
|
require_paths:
|