ruby-stream 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.md +7 -0
- data/README.md +1 -1
- data/lib/ruby-stream.rb +42 -0
- data/ruby-stream.gemspec +3 -2
- data/spec/ruby-stream_spec.rb +64 -2
- metadata +6 -4
data/LICENSE.md
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
The MIT License (MIT) Copyright (c) Callum Stott, http://www.seadowg.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -45,6 +45,6 @@ will need to be prepared to wait, forever. No seriously, I mean for all
|
|
45
45
|
time. Its infinite.
|
46
46
|
|
47
47
|
Streams become most powerful when used with high order functions. At the
|
48
|
-
moment ruby-stream supports `map` and `
|
48
|
+
moment ruby-stream supports `map`, `filter`, `each`, `take` and `scan` operations that operate
|
49
49
|
as they would on normal collections (these operations will only actually be
|
50
50
|
applied to Stream elements on access or iteration however).
|
data/lib/ruby-stream.rb
CHANGED
@@ -78,6 +78,16 @@ class Stream
|
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
81
|
+
def drop(n)
|
82
|
+
if n <= 0
|
83
|
+
Stream.new(head) do
|
84
|
+
tail
|
85
|
+
end
|
86
|
+
else
|
87
|
+
tail.drop(n - 1)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
81
91
|
def map(&block)
|
82
92
|
Stream.new(yield head) do
|
83
93
|
tail.map(&block)
|
@@ -90,6 +100,24 @@ class Stream
|
|
90
100
|
end
|
91
101
|
end
|
92
102
|
|
103
|
+
def fold_left(*args, &block)
|
104
|
+
zero = args[0]
|
105
|
+
symbol = args[1] || args[0]
|
106
|
+
|
107
|
+
scan = if block
|
108
|
+
self.scan(zero, &block)
|
109
|
+
elsif args.length == 2
|
110
|
+
self.scan(zero, &symbol.to_proc)
|
111
|
+
elsif args.length == 1
|
112
|
+
self.drop(1).scan(head, &symbol.to_proc)
|
113
|
+
end
|
114
|
+
|
115
|
+
scan.last
|
116
|
+
end
|
117
|
+
|
118
|
+
alias :inject :fold_left
|
119
|
+
alias :reduce :fold_left
|
120
|
+
|
93
121
|
def filter(&block)
|
94
122
|
if block.call(head)
|
95
123
|
Stream.new(head) do
|
@@ -109,6 +137,10 @@ class Stream
|
|
109
137
|
EmptyStream.new
|
110
138
|
end
|
111
139
|
|
140
|
+
def last
|
141
|
+
nil
|
142
|
+
end
|
143
|
+
|
112
144
|
def [](n)
|
113
145
|
nil
|
114
146
|
end
|
@@ -121,6 +153,10 @@ class Stream
|
|
121
153
|
EmptyStream.new
|
122
154
|
end
|
123
155
|
|
156
|
+
def drop(n)
|
157
|
+
EmptyStream.new
|
158
|
+
end
|
159
|
+
|
124
160
|
def take_while(&block)
|
125
161
|
EmptyStream.new
|
126
162
|
end
|
@@ -132,5 +168,11 @@ class Stream
|
|
132
168
|
def filter(&block)
|
133
169
|
EmptyStream.new
|
134
170
|
end
|
171
|
+
|
172
|
+
def scan(zero, &block)
|
173
|
+
Stream.new(zero) do
|
174
|
+
EmptyStream.new
|
175
|
+
end
|
176
|
+
end
|
135
177
|
end
|
136
178
|
end
|
data/ruby-stream.gemspec
CHANGED
@@ -3,11 +3,12 @@ $:.unshift lib unless $:.include?(lib)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "ruby-stream"
|
6
|
-
s.version = "0.
|
6
|
+
s.version = "0.4.0"
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
8
|
s.authors = ["Callum Stott"]
|
9
|
-
s.email = ["callum
|
9
|
+
s.email = ["callum@seadowg.com"]
|
10
10
|
s.summary = "Lazy stream implementation for Ruby"
|
11
|
+
s.license = 'MIT'
|
11
12
|
|
12
13
|
s.require_paths = ['lib']
|
13
14
|
s.files = `git ls-files`.split("\n")
|
data/spec/ruby-stream_spec.rb
CHANGED
@@ -94,6 +94,24 @@ describe Stream do
|
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
97
|
+
describe "#drop(n)" do
|
98
|
+
it "returns another Stream" do
|
99
|
+
@stream.drop(1).kind_of?(Stream).must_equal true
|
100
|
+
end
|
101
|
+
|
102
|
+
it "returns a Stream that skips the first n elements of the original" do
|
103
|
+
stream = @stream.drop(5)
|
104
|
+
stream[0].must_equal 6
|
105
|
+
stream[1].must_equal 7
|
106
|
+
end
|
107
|
+
|
108
|
+
describe "for a finite Stream" do
|
109
|
+
it "returns a Stream with the correct length" do
|
110
|
+
@stream.take(5).drop(1).length.must_equal 4
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
97
115
|
describe "#each(func)" do
|
98
116
|
describe "for a finite Stream" do
|
99
117
|
it "should return nil" do
|
@@ -180,7 +198,7 @@ describe Stream do
|
|
180
198
|
end
|
181
199
|
end
|
182
200
|
|
183
|
-
describe "#scan(func)" do
|
201
|
+
describe "#scan(zero, func)" do
|
184
202
|
it "returns a new Stream" do
|
185
203
|
@stream.scan(0) { |x, i| i }.kind_of?(Stream).must_equal true
|
186
204
|
end
|
@@ -189,13 +207,57 @@ describe Stream do
|
|
189
207
|
@stream.scan(-1) { |x, i| i }.head.must_equal -1
|
190
208
|
end
|
191
209
|
|
192
|
-
it "returns a Stream
|
210
|
+
it "returns a Stream that is the scan of the receiver" do
|
193
211
|
scan = @stream.scan(0) { |x, i| x + i }
|
194
212
|
scan[1].must_equal(1)
|
195
213
|
scan[2].must_equal(3)
|
196
214
|
scan[3].must_equal(6)
|
197
215
|
scan[100].must_equal(5050)
|
198
216
|
end
|
217
|
+
|
218
|
+
it "works correctly with finite streams" do
|
219
|
+
scan = @stream.take(1).scan(0) { |x, i| x + i }
|
220
|
+
scan[1].must_equal 1
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
describe "#fold_left(zero, func)" do
|
225
|
+
it "returns the left fold for an finite Stream" do
|
226
|
+
sum = @stream.take(5).fold_left(1) { |memo, ele|
|
227
|
+
memo + ele
|
228
|
+
}
|
229
|
+
sum.must_equal 16
|
230
|
+
end
|
231
|
+
|
232
|
+
it "returns the zero value for empty lists" do
|
233
|
+
value = @stream.take(0).fold_left(101) { |i, j| j }
|
234
|
+
value.must_equal 101
|
235
|
+
end
|
236
|
+
|
237
|
+
it "allows the func to be a symbol" do
|
238
|
+
sum = @stream.take(5).fold_left(1, :+)
|
239
|
+
sum.must_equal 16
|
240
|
+
end
|
241
|
+
|
242
|
+
it "is aliased with 'inject'" do
|
243
|
+
@stream.method(:fold_left).must_equal @stream.method(:inject)
|
244
|
+
end
|
245
|
+
|
246
|
+
it "is aliased with 'reduce'" do
|
247
|
+
@stream.method(:fold_left).must_equal @stream.method(:reduce)
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
describe "#fold_left(symbol)" do
|
252
|
+
it "returns the left fold for a finite Stream but skips the zero value step" do
|
253
|
+
sum = @stream.take(5).fold_left(:+)
|
254
|
+
sum.must_equal 15
|
255
|
+
end
|
256
|
+
|
257
|
+
it "returns nil for an empty Stream" do
|
258
|
+
sum = @stream.take(0).fold_left(:+)
|
259
|
+
sum.must_equal nil
|
260
|
+
end
|
199
261
|
end
|
200
262
|
|
201
263
|
describe ".continually(func)" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-stream
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-05-06 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email:
|
16
|
-
- callum
|
16
|
+
- callum@seadowg.com
|
17
17
|
executables: []
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
@@ -21,13 +21,15 @@ files:
|
|
21
21
|
- .ruby-version
|
22
22
|
- Gemfile
|
23
23
|
- Gemfile.lock
|
24
|
+
- LICENSE.md
|
24
25
|
- README.md
|
25
26
|
- Rakefile
|
26
27
|
- lib/ruby-stream.rb
|
27
28
|
- ruby-stream.gemspec
|
28
29
|
- spec/ruby-stream_spec.rb
|
29
30
|
homepage:
|
30
|
-
licenses:
|
31
|
+
licenses:
|
32
|
+
- MIT
|
31
33
|
post_install_message:
|
32
34
|
rdoc_options: []
|
33
35
|
require_paths:
|