b-lazy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/b-lazy.rb +192 -0
  2. metadata +66 -0
data/lib/b-lazy.rb ADDED
@@ -0,0 +1,192 @@
1
+
2
+
3
+ # We really don't need to make any significant changes
4
+ # to the Enumerator class itself. We're just adding a
5
+ # few convenient methods that can simplify looping constructs.
6
+ class Enumerator
7
+
8
+ def has_next?
9
+ peek
10
+ true
11
+ rescue StopIteration
12
+ false
13
+ end
14
+
15
+ def empty?
16
+ peek
17
+ false
18
+ rescue StopIteration
19
+ true
20
+ end
21
+
22
+ end
23
+
24
+
25
+
26
+
27
+ module Enumerable
28
+
29
+
30
+ def lmap(&blk)
31
+ Enumerator.new do |out|
32
+ self.each do |i|
33
+ out.yield blk.call(i)
34
+ end
35
+ end
36
+ end
37
+
38
+
39
+ def lselect(&blk)
40
+ Enumerator.new do |out|
41
+ self.each do |i|
42
+ out.yield i if blk.call(i)
43
+ end
44
+ end
45
+ end
46
+
47
+ def lreject(&blk)
48
+ Enumerator.new do |out|
49
+ self.each do |i|
50
+ out.yield i unless blk.call(i)
51
+ end
52
+ end
53
+ end
54
+
55
+
56
+ # Start as soon as the condition becomes true
57
+ def start_when(&blk)
58
+ Enumerator.new do |out|
59
+ s = self.to_enum
60
+ loop do
61
+ break if blk.call(s.peek)
62
+ s.next
63
+ end
64
+
65
+ loop do
66
+ out.yield s.next
67
+ end
68
+ end
69
+ end
70
+
71
+
72
+ # Start one element after the condition becomes true
73
+ def start_after(&blk)
74
+ Enumerator.new do |out|
75
+ s = self.to_enum
76
+ loop do
77
+ break if blk.call(s.next)
78
+ end
79
+
80
+ loop do
81
+ out.yield s.next
82
+ end
83
+ end
84
+ end
85
+
86
+
87
+ # Continue as long as the condition is true
88
+ def do_while(&blk)
89
+ s = self.to_enum
90
+ Enumerator.new do |out|
91
+ while s.has_next?
92
+ break unless blk.call(s.peek)
93
+ out.yield s.next
94
+ end
95
+ end
96
+ end
97
+
98
+
99
+ # Keep iterating until the condition becomes true
100
+ def do_until(&blk)
101
+ s = self.to_enum
102
+ Enumerator.new do |out|
103
+ until s.empty?
104
+ break if blk.call(s.peek)
105
+ out.yield s.next
106
+ end
107
+ end
108
+ end
109
+ alias :stop_before :do_until
110
+
111
+
112
+ # Returns all elements up to and including the element
113
+ # the causes the condition to become true.
114
+ def stop_when(&blk)
115
+ s = self.to_enum
116
+ Enumerator.new do |out|
117
+ while s.has_next?
118
+ out.yield s.peek
119
+ break if blk.call(s.next)
120
+ end
121
+ end
122
+ end
123
+
124
+
125
+ def skip(n = 1)
126
+ Enumerator.new do |out|
127
+ s = self.to_enum
128
+ begin
129
+ n.times {s.next}
130
+ loop do
131
+ out.yield s.next
132
+ end
133
+ end
134
+ end
135
+ end
136
+
137
+
138
+
139
+
140
+ def cons()
141
+ Enumerator.new do |out|
142
+ s = self.to_enum
143
+ loop do
144
+ items = s.next.to_enum
145
+ loop do
146
+ out.yield items.next
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+
153
+ # NOTE: This method should only be called on Enumerators
154
+ # of Enumerators! This is similar to cons, but it
155
+ # instead takes the first item from each enumerator,
156
+ # then the second item, etc. This can be handy when
157
+ # you have a finite number of enumerators, but each
158
+ # one may hold an infinite number of items
159
+ def weave()
160
+ Enumerator.new do |out|
161
+ s = self.to_enum
162
+ enums = []
163
+
164
+
165
+ # The first time through, we will gradually collect all
166
+ # of self's enumerators into an Array.
167
+ self.each do |e|
168
+ if e.has_next?
169
+ enums << e
170
+ out.yield e.next
171
+ end
172
+ end
173
+
174
+
175
+
176
+ # Now all the enumerators have been collected into an Array.
177
+ # Henceforth, we'll iterate through this Array
178
+ until enums.empty?
179
+ next_enums = []
180
+ enums.each do |e|
181
+ if e.has_next?
182
+ next_enums << e
183
+ out.yield e.next
184
+ end
185
+ end
186
+ enums = next_enums
187
+ end
188
+
189
+ end
190
+ end
191
+
192
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: b-lazy
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Brian Lauber
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-04 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Extends core Ruby objects to provide inherent support for lazy-evaluation.
22
+ email: constructible.truth@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/b-lazy.rb
31
+ has_rdoc: true
32
+ homepage: http://rubygems.org/gems/hola
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ segments:
46
+ - 1
47
+ - 9
48
+ - 1
49
+ version: 1.9.1
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project: b-lazy
61
+ rubygems_version: 1.3.7
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Why work hard for lazy-evaluation?
65
+ test_files: []
66
+