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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MGI2YTQ2Y2Y0ZmFjOTljNmEzMjU2ZDc4YTFkNWE3ZjY0MWY4Mjg1Ng==
4
+ NzY5YTdhMzFkMDM1MzkxMGMyZDAyNTUxNmIxMzg4MzYzMTM5OTczZQ==
5
5
  data.tar.gz: !binary |-
6
- ODUwMjlhZmYzN2M1ZTgxNTkwNmI4N2E4YTg5YzI5ZmY2NTJiNTgyZQ==
6
+ OGU1MzVhZWViMTM4YWE1MWQ1YTljYmZmZDU0NTAyZWJiMjJkMzViOQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MWYzMjdhYTNlYjE3ZjRkODI0NDQzMmQ0YTA3NDIwNjFjMDViMWYwNjZhMmVk
10
- OTBiZWEwMTJjODY2NjFiYzhlYzMwMGI4ODUwNTU3MWViNjYyYzNhNzIwNTdj
11
- ZWEwNGRkMWZjY2ZkYjM1YzUwZjJhNzc2YzZmNDcxNDBjYTg0MTE=
9
+ NWZkYzRhMmE0NmE1YjFkYWEzNWUyMTc3ODYxNjM1YWVlY2U3YTk0ZWRiMGI0
10
+ MzA5MTI1N2Q0ZGRlYzMyMzliN2QxNWExOWE4Y2QxOTIzYmIxZGJkYTc1YTY0
11
+ NTgyMWY4OTQ5MTZiZDU1N2NmYjM4MTI0MGMxZTJiYmRjZjg5YzM=
12
12
  data.tar.gz: !binary |-
13
- M2U3ZWJjYjY0NjlkOGQyOTg2MjVhOTYzMTk1NjgzYjg2OGFkM2E5MzBiMGYz
14
- ZjU2ZmZmNTJjMjIwYmRiNGNjYTk4Yzg3YTBkNjVmMTQzYjNhMjUxMzRlNDE1
15
- OGQyNjUxMTg2NjE1ZGEyODJhZDczNDA1Nzc4OTEyY2ZmNjBiOGE=
13
+ ZjRjODJlZjhiZjQzYzVlMGNmZDlmMzUwN2M4NGE5ZDI1YjBlYTkyYTZlMjI2
14
+ ZWIyNWNjZjFkMGE5M2EwNzY4ZGI5MjMwMzRhNTY5ZjI2YTgwZTg1NDc0ZTkw
15
+ Y2FlYzFlZmM0YTY4MTZhNzA5Mjg3YmFhYjI0MGM4NzVjYmYwMTQ=
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - jruby-18mode
7
+ - jruby-19mode
data/Gemfile CHANGED
@@ -2,5 +2,5 @@ source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- gem "rake", "~> 0.8.7"
5
+ gem "rake", "~> 10.0.0"
6
6
  gem "rspec", ">= 2"
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- Enumerating
1
+ Enumerating [![Build Status](https://secure.travis-ci.org/mdub/enumerating.png?branch=master)](http://travis-ci.org/mdub/enumerating)
2
2
  ===========
3
3
 
4
4
  Lazy "filtering" and transforming
@@ -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
- benchmark "enumerating", @control do
58
- array.selecting { |x| x.even? }.collecting { |x| x*x }
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 defined?(Fiber)
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
- benchmark "facets Enumerable#defer", @control do
74
- array.defer.select { |x| x.even? }.collect { |x| x*x }
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
- @generator.call(yielder)
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
- break if index + 1 == n
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
- break unless yield(element)
89
+ throw Enumerating::Filter::DONE unless yield(element)
86
90
  output.call(element)
87
91
  end
88
92
  end
@@ -22,7 +22,7 @@ module Enumerating
22
22
  end
23
23
 
24
24
  def each
25
- while true do
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
- threads = Enumerating::ThreadStarter.new(self, block)
48
- Enumerating::ThreadJoiner.new(threads.prefetching(max_threads - 1))
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
@@ -1,3 +1,3 @@
1
1
  module Enumerating
2
- VERSION = "1.2.0".freeze
2
+ VERSION = "1.2.1".freeze
3
3
  end
@@ -82,6 +82,10 @@ describe Enumerable do
82
82
  [1,2].with_time_bomb.taking(2).to_a.should == [1,2]
83
83
  end
84
84
 
85
+ it "is stackable" do
86
+ [1,2].taking(2).taking(2).to_a.should == [1,2]
87
+ end
88
+
85
89
  it "copes with 0" do
86
90
  [].with_time_bomb.taking(0).to_a.should == []
87
91
  end
@@ -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.01, 0.01, 0.01]
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).round(2).should eq(0.01 * 2)
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.05, 0.04, 0.03, 0.02, 0.01]
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).round(2)
37
+ round(Time.now - start)
34
38
  end
35
- elapsed_times.to_a.should eq([0.05, 0.04, 0.03, 0.07, 0.06])
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.0
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-15 00:00:00.000000000 Z
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