enumerating 1.2.0 → 1.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 +8 -8
- data/.travis.yml +7 -0
- data/Gemfile +1 -1
- data/README.md +1 -1
- data/benchmarks/pipeline_bench.rb +36 -17
- data/lib/enumerating/filtering.rb +7 -3
- data/lib/enumerating/merging.rb +3 -3
- data/lib/enumerating/threading.rb +6 -43
- data/lib/enumerating/version.rb +1 -1
- data/spec/enumerating/filtering_spec.rb +4 -0
- data/spec/enumerating/merging_spec.rb +3 -3
- data/spec/enumerating/threading_spec.rb +9 -5
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NzY5YTdhMzFkMDM1MzkxMGMyZDAyNTUxNmIxMzg4MzYzMTM5OTczZQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
OGU1MzVhZWViMTM4YWE1MWQ1YTljYmZmZDU0NTAyZWJiMjJkMzViOQ==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NWZkYzRhMmE0NmE1YjFkYWEzNWUyMTc3ODYxNjM1YWVlY2U3YTk0ZWRiMGI0
|
10
|
+
MzA5MTI1N2Q0ZGRlYzMyMzliN2QxNWExOWE4Y2QxOTIzYmIxZGJkYTc1YTY0
|
11
|
+
NTgyMWY4OTQ5MTZiZDU1N2NmYjM4MTI0MGMxZTJiYmRjZjg5YzM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZjRjODJlZjhiZjQzYzVlMGNmZDlmMzUwN2M4NGE5ZDI1YjBlYTkyYTZlMjI2
|
14
|
+
ZWIyNWNjZjFkMGE5M2EwNzY4ZGI5MjMwMzRhNTY5ZjI2YTgwZTg1NDc0ZTkw
|
15
|
+
Y2FlYzFlZmM0YTY4MTZhNzA5Mjg3YmFhYjI0MGM4NzVjYmYwMTQ=
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,18 +2,6 @@ require 'benchmark'
|
|
2
2
|
|
3
3
|
$: << File.expand_path("../../lib", __FILE__)
|
4
4
|
|
5
|
-
if defined?(Fiber)
|
6
|
-
require "lazing"
|
7
|
-
module Enumerable
|
8
|
-
alias :lazing_select :selecting
|
9
|
-
alias :lazing_collect :collecting
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
require "enumerating"
|
14
|
-
|
15
|
-
require 'facets'
|
16
|
-
|
17
5
|
array = (1..100000).to_a
|
18
6
|
|
19
7
|
# Test scenario:
|
@@ -54,22 +42,53 @@ end
|
|
54
42
|
array.select { |x| x.even? }.collect { |x| x*x }
|
55
43
|
end
|
56
44
|
|
57
|
-
|
58
|
-
|
45
|
+
def can_require?(library)
|
46
|
+
require(library)
|
47
|
+
true
|
48
|
+
rescue LoadError
|
49
|
+
printf "%-36s ----------------- N/A -----------------\n", library
|
50
|
+
false
|
59
51
|
end
|
60
52
|
|
61
|
-
if
|
53
|
+
if can_require?("enumerating")
|
54
|
+
|
55
|
+
benchmark "enumerating", @control do
|
56
|
+
array.selecting { |x| x.even? }.collecting { |x| x*x }
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
if defined?(Fiber) && can_require?("lazing")
|
62
|
+
|
63
|
+
module Enumerable
|
64
|
+
alias :lazing_select :selecting
|
65
|
+
alias :lazing_collect :collecting
|
66
|
+
end
|
67
|
+
|
62
68
|
benchmark "lazing", @control do
|
63
69
|
array.lazing_select { |x| x.even? }.lazing_collect { |x| x*x }
|
64
70
|
end
|
71
|
+
|
65
72
|
end
|
66
73
|
|
67
74
|
if array.respond_to?(:lazy)
|
75
|
+
|
68
76
|
benchmark "ruby2 Enumerable#lazy", @control do
|
69
77
|
array.lazy.select { |x| x.even? }.lazy.collect { |x| x*x }
|
70
78
|
end
|
79
|
+
|
80
|
+
elsif can_require?("backports/2.0.0/enumerable")
|
81
|
+
|
82
|
+
benchmark "backports Enumerable#lazy", @control do
|
83
|
+
array.lazy.select { |x| x.even? }.lazy.collect { |x| x*x }
|
84
|
+
end
|
85
|
+
|
71
86
|
end
|
72
87
|
|
73
|
-
|
74
|
-
|
88
|
+
if can_require? "facets"
|
89
|
+
|
90
|
+
benchmark "facets Enumerable#defer", @control do
|
91
|
+
array.defer.select { |x| x.even? }.collect { |x| x*x }
|
92
|
+
end
|
93
|
+
|
75
94
|
end
|
@@ -10,10 +10,14 @@ module Enumerating
|
|
10
10
|
@generator = generator
|
11
11
|
end
|
12
12
|
|
13
|
+
DONE = "done-#{self.object_id}".to_sym
|
14
|
+
|
13
15
|
def each
|
14
16
|
return to_enum unless block_given?
|
15
17
|
yielder = proc { |x| yield x }
|
16
|
-
|
18
|
+
catch DONE do
|
19
|
+
@generator.call(yielder)
|
20
|
+
end
|
17
21
|
end
|
18
22
|
|
19
23
|
end
|
@@ -73,7 +77,7 @@ module Enumerable
|
|
73
77
|
if n > 0
|
74
78
|
each_with_index do |element, index|
|
75
79
|
output.call(element)
|
76
|
-
|
80
|
+
throw Enumerating::Filter::DONE if index + 1 == n
|
77
81
|
end
|
78
82
|
end
|
79
83
|
end
|
@@ -82,7 +86,7 @@ module Enumerable
|
|
82
86
|
def taking_while
|
83
87
|
Enumerating::Filter.new do |output|
|
84
88
|
each do |element|
|
85
|
-
|
89
|
+
throw Enumerating::Filter::DONE unless yield(element)
|
86
90
|
output.call(element)
|
87
91
|
end
|
88
92
|
end
|
data/lib/enumerating/merging.rb
CHANGED
@@ -22,7 +22,7 @@ module Enumerating
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def each
|
25
|
-
|
25
|
+
loop do
|
26
26
|
discard_empty_enumerators
|
27
27
|
break if @enumerators.empty?
|
28
28
|
yield next_enumerator.next
|
@@ -58,7 +58,7 @@ module Enumerating
|
|
58
58
|
end
|
59
59
|
|
60
60
|
class << Enumerating
|
61
|
-
|
61
|
+
|
62
62
|
def merging(*enumerables)
|
63
63
|
Enumerating::Merger.new(enumerables)
|
64
64
|
end
|
@@ -66,5 +66,5 @@ class << Enumerating
|
|
66
66
|
def merging_by(*enumerables, &block)
|
67
67
|
Enumerating::Merger.new(enumerables, &block)
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
end
|
@@ -1,51 +1,14 @@
|
|
1
|
+
require 'enumerating/filtering'
|
1
2
|
require 'enumerating/prefetching'
|
2
3
|
|
3
|
-
module Enumerating
|
4
|
-
|
5
|
-
class ThreadStarter
|
6
|
-
|
7
|
-
include Enumerable
|
8
|
-
|
9
|
-
def initialize(source, block)
|
10
|
-
@source = source
|
11
|
-
@block = block
|
12
|
-
end
|
13
|
-
|
14
|
-
def each
|
15
|
-
@source.each do |source_item|
|
16
|
-
thread = Thread.new do
|
17
|
-
@block.call(source_item)
|
18
|
-
end
|
19
|
-
yield thread
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
class ThreadJoiner
|
26
|
-
|
27
|
-
include Enumerable
|
28
|
-
|
29
|
-
def initialize(threads)
|
30
|
-
@threads = threads
|
31
|
-
end
|
32
|
-
|
33
|
-
def each
|
34
|
-
@threads.each do |thread|
|
35
|
-
thread.join
|
36
|
-
yield thread.value
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
41
|
-
|
42
|
-
end
|
43
|
-
|
44
4
|
module Enumerable
|
45
5
|
|
46
6
|
def threading(max_threads, &block)
|
47
|
-
|
48
|
-
|
7
|
+
collecting do |item|
|
8
|
+
Thread.new { block.call(item) }
|
9
|
+
end.prefetching(max_threads - 1).collecting do |thread|
|
10
|
+
thread.join; thread.value
|
11
|
+
end
|
49
12
|
end
|
50
13
|
|
51
14
|
end
|
data/lib/enumerating/version.rb
CHANGED
@@ -13,8 +13,8 @@ describe Enumerating, :needs_enumerators => true do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
it "is lazy" do
|
16
|
-
@enum1 = [1,3]
|
17
|
-
@enum2 = [2,4].with_time_bomb
|
16
|
+
@enum1 = [1,3,6]
|
17
|
+
@enum2 = [2,4,7].with_time_bomb
|
18
18
|
@merge = Enumerating.merging(@enum1, @enum2)
|
19
19
|
@merge.take(4).should == [1,2,3,4]
|
20
20
|
end
|
@@ -32,4 +32,4 @@ describe Enumerating, :needs_enumerators => true do
|
|
32
32
|
|
33
33
|
end
|
34
34
|
|
35
|
-
end
|
35
|
+
end
|
@@ -16,23 +16,27 @@ describe Enumerable do
|
|
16
16
|
[1,2,3].with_time_bomb.threading(2) { |x| x * 2 }.first.should == 2
|
17
17
|
end
|
18
18
|
|
19
|
+
def round(n, accuracy = 0.02)
|
20
|
+
(n / accuracy).round.to_f * accuracy
|
21
|
+
end
|
22
|
+
|
19
23
|
it "runs the specified number of threads in parallel" do
|
20
|
-
delays = [0.
|
24
|
+
delays = [0.03, 0.03, 0.03]
|
21
25
|
start = Time.now
|
22
26
|
delays.threading(2) do |delay|
|
23
27
|
sleep(delay)
|
24
28
|
end.to_a
|
25
|
-
(Time.now - start).
|
29
|
+
round(Time.now - start).should eq(0.06)
|
26
30
|
end
|
27
31
|
|
28
32
|
it "acts as a sliding window" do
|
29
|
-
delays = [0.
|
33
|
+
delays = [0.1, 0.08, 0.06, 0.04, 0.02]
|
30
34
|
start = Time.now
|
31
35
|
elapsed_times = delays.threading(3) do |delay|
|
32
36
|
sleep(delay)
|
33
|
-
(Time.now - start)
|
37
|
+
round(Time.now - start)
|
34
38
|
end
|
35
|
-
elapsed_times.to_a.should eq([0.
|
39
|
+
elapsed_times.to_a.should eq([0.1, 0.08, 0.06, 0.14, 0.12])
|
36
40
|
end
|
37
41
|
|
38
42
|
it "surfaces exceptions" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerating
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-04-
|
11
|
+
date: 2013-04-18 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Enumerating extends Enumerable with "lazy" versions of various operations,
|
14
14
|
allowing streamed processing of large (or even infinite) collections. Even in Ruby
|
@@ -20,6 +20,7 @@ extra_rdoc_files: []
|
|
20
20
|
files:
|
21
21
|
- .gitignore
|
22
22
|
- .rspec
|
23
|
+
- .travis.yml
|
23
24
|
- CHANGES.md
|
24
25
|
- Gemfile
|
25
26
|
- README.md
|