lazing 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/LICENSE.txt +21 -0
- data/README.md +63 -0
- data/Rakefile +16 -0
- data/VERSION +1 -0
- data/lazing.gemspec +54 -0
- data/lib/lazing.rb +2 -0
- data/lib/lazing/filters.rb +31 -0
- data/lib/lazing/transformers.rb +19 -0
- data/spec/lazing/filters_spec.rb +37 -0
- data/spec/lazing/transformers_spec.rb +25 -0
- data/spec/spec_helper.rb +1 -0
- metadata +78 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.idea
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2010 Greg Spurrier
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Lazing
|
2
|
+
Lazing provides lazy equivalents for many of the methods in Ruby's standard
|
3
|
+
`Enumerable` module. The lazy variants all end in 'ing', hence the name.
|
4
|
+
|
5
|
+
For example:
|
6
|
+
|
7
|
+
>> (1..20).selecting {|x| raise 'boom' if x == 6; x.odd?}.first(3)
|
8
|
+
=> [1, 3, 5]
|
9
|
+
|
10
|
+
Compare the above with the behavior of the non-lazy standard version:
|
11
|
+
|
12
|
+
>> (1..20).select {|x| raise 'boom' if x == 6; x.odd?}.first(3)
|
13
|
+
RuntimeError: boom
|
14
|
+
from (irb):4:in `block in irb_binding'
|
15
|
+
from (irb):4:in `each'
|
16
|
+
from (irb):4:in `select'
|
17
|
+
from (irb):4
|
18
|
+
from irb:17:in `<main>'
|
19
|
+
|
20
|
+
## Lazy Methods
|
21
|
+
Lazing defines the following lazy filter methods:
|
22
|
+
|
23
|
+
* `finding_all`
|
24
|
+
* `rejecting`
|
25
|
+
* `selecting`
|
26
|
+
|
27
|
+
Lazing defines the following lazy transformation methods:
|
28
|
+
|
29
|
+
* `collecting`
|
30
|
+
* `mapping`
|
31
|
+
|
32
|
+
## Supported Rubies
|
33
|
+
Lazing requires Ruby 1.9. It has been tested with Ruby 1.9.2-p0.
|
34
|
+
|
35
|
+
## Installation
|
36
|
+
Lazing is distributed as a gem. To install, use the command:
|
37
|
+
|
38
|
+
gem install lazing
|
39
|
+
|
40
|
+
## Obtaining the Latest Version
|
41
|
+
Lazing is hosted on github at
|
42
|
+
[http://github.com/gregspurrier/lazing](http://github.com/gregspurrier/lazing).
|
43
|
+
|
44
|
+
A local clone of the repository can be obtained via:
|
45
|
+
|
46
|
+
git clone git://github.com/gregspurrier/lazing.git
|
47
|
+
|
48
|
+
## Related work
|
49
|
+
After implementing [`select_first`](http://blog.rujubu.com/articles/selecting-only-what-you-need)
|
50
|
+
for a project I was working on, I searched for lazy enumeration support in Ruby.
|
51
|
+
I came across this [blog post](http://www.michaelharrison.ws/weblog/?p=163) and
|
52
|
+
it became the inspiration for Lazing.
|
53
|
+
|
54
|
+
I later discovered that my implementation of `selecting` is almost identical
|
55
|
+
to the `infinite_select` example given in Programming Ruby: The Pragmatic
|
56
|
+
Programmers' Guide by Dave Thomas.
|
57
|
+
|
58
|
+
## License
|
59
|
+
Lazing is Copyright (c) 2010 by Greg Spurrier and released under the terms of
|
60
|
+
the MIT License. Please see LICENSE.txt for details.
|
61
|
+
|
62
|
+
|
63
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |gemspec|
|
6
|
+
gemspec.name = "lazing"
|
7
|
+
gemspec.summary = "Making Enumerable a little lazy"
|
8
|
+
gemspec.description = "Lazy equivalents for many of the methods in Enumerable"
|
9
|
+
gemspec.email = "greg@rujubu.com"
|
10
|
+
gemspec.homepage = "http://github.com/gregspurrier/lazing"
|
11
|
+
gemspec.author = "Greg Spurrier"
|
12
|
+
end
|
13
|
+
Jeweler::GemcutterTasks.new
|
14
|
+
rescue LoadError
|
15
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
16
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lazing.gemspec
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{lazing}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Greg Spurrier"]
|
12
|
+
s.date = %q{2010-10-22}
|
13
|
+
s.description = %q{Lazy equivalents for many of the methods in Enumerable}
|
14
|
+
s.email = %q{greg@rujubu.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE.txt",
|
22
|
+
"README.md",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"lazing.gemspec",
|
26
|
+
"lib/lazing.rb",
|
27
|
+
"lib/lazing/filters.rb",
|
28
|
+
"lib/lazing/transformers.rb",
|
29
|
+
"spec/lazing/filters_spec.rb",
|
30
|
+
"spec/lazing/transformers_spec.rb",
|
31
|
+
"spec/spec_helper.rb"
|
32
|
+
]
|
33
|
+
s.homepage = %q{http://github.com/gregspurrier/lazing}
|
34
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
35
|
+
s.require_paths = ["lib"]
|
36
|
+
s.rubygems_version = %q{1.3.7}
|
37
|
+
s.summary = %q{Making Enumerable a little lazy}
|
38
|
+
s.test_files = [
|
39
|
+
"spec/lazing/filters_spec.rb",
|
40
|
+
"spec/lazing/transformers_spec.rb",
|
41
|
+
"spec/spec_helper.rb"
|
42
|
+
]
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
46
|
+
s.specification_version = 3
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
else
|
50
|
+
end
|
51
|
+
else
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
data/lib/lazing.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module Enumerable
|
2
|
+
# Lazy version of Enumerable#reject.
|
3
|
+
#
|
4
|
+
# Returns a lazy collection containing all elements of the collection
|
5
|
+
# for which the provided block returns false.
|
6
|
+
def rejecting(&blk)
|
7
|
+
Enumerator.new do |yielder|
|
8
|
+
each do |item|
|
9
|
+
yielder.yield item unless blk.call(item)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# Lazy version of Enumerable#select
|
15
|
+
#
|
16
|
+
# Returns a lazy collection containing all elements of the collection
|
17
|
+
# for which the provided block returns true.
|
18
|
+
def selecting(&blk)
|
19
|
+
Enumerator.new do |yielder|
|
20
|
+
each do |item|
|
21
|
+
yielder.yield item if blk.call(item)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Lazy version of Enumerable#find_all
|
27
|
+
#
|
28
|
+
# Returns a lazy collection containing all elements of the collection
|
29
|
+
# for which the provided block returns true.
|
30
|
+
alias finding_all selecting
|
31
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Enumerable
|
2
|
+
# Lazy version of Enumerable#map
|
3
|
+
#
|
4
|
+
# Returns the lazy collection formed by applying the given block to each
|
5
|
+
# member of the collection.
|
6
|
+
def mapping(&blk)
|
7
|
+
Enumerator.new do |yielder|
|
8
|
+
each do |item|
|
9
|
+
yielder.yield blk.call(item)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# Lazy version of Enumerable#collect
|
15
|
+
#
|
16
|
+
# Returns the lazy collection formed by applying the given block to each
|
17
|
+
# member of the collection.
|
18
|
+
alias collecting mapping
|
19
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe Enumerable, '#finding_all' do
|
4
|
+
it 'returns a lazy collection matching the results of Enumerable#find_all' do
|
5
|
+
range = (1..20)
|
6
|
+
range.finding_all {|x| x.odd?}.to_a.should == range.find_all {|x| x.odd?}
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'processes items on demand' do
|
10
|
+
(1..20).finding_all {|x| raise 'boom' if x == 6; x.odd?}.first(3).
|
11
|
+
should == [1,3,5]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe Enumerable, '#rejecting' do
|
16
|
+
it 'returns a lazy collection matching the results of Enumerable#reject' do
|
17
|
+
range = (1..20)
|
18
|
+
range.rejecting {|x| x.odd?}.to_a.should == range.reject {|x| x.odd?}
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'processes items on demand' do
|
22
|
+
(1..20).rejecting {|x| raise 'boom' if x == 6; x.even?}.first(3).
|
23
|
+
should == [1,3,5]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe Enumerable, '#selecting' do
|
28
|
+
it 'returns a lazy collection matching the results of Enumerable#select' do
|
29
|
+
range = (1..20)
|
30
|
+
range.selecting {|x| x.odd?}.to_a.should == range.select {|x| x.odd?}
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'processes items on demand' do
|
34
|
+
(1..20).selecting {|x| raise 'boom' if x == 6; x.odd?}.first(3).
|
35
|
+
should == [1,3,5]
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe Enumerable, '#collecting' do
|
4
|
+
it 'returns a lazy collection matching the results of Enumerable#collect' do
|
5
|
+
range = (1..20)
|
6
|
+
range.collecting {|x| x * x}.to_a.should == range.collect {|x| x * x}
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'processes items on demand' do
|
10
|
+
(1..20).collecting {|x| raise 'boom' if x == 4; x * x}.first(3).
|
11
|
+
should == [1,4,9]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe Enumerable, '#mapping' do
|
16
|
+
it 'returns a lazy collection matching the results of Enumerable#map' do
|
17
|
+
range = (1..20)
|
18
|
+
range.mapping {|x| x * x}.to_a.should == range.map {|x| x * x}
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'processes items on demand' do
|
22
|
+
(1..20).mapping {|x| raise 'boom' if x == 4; x * x}.first(3).
|
23
|
+
should == [1,4,9]
|
24
|
+
end
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.expand_path('../../lib/lazing', __FILE__)
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lazing
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Greg Spurrier
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-10-22 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Lazy equivalents for many of the methods in Enumerable
|
22
|
+
email: greg@rujubu.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- LICENSE.txt
|
29
|
+
- README.md
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- LICENSE.txt
|
33
|
+
- README.md
|
34
|
+
- Rakefile
|
35
|
+
- VERSION
|
36
|
+
- lazing.gemspec
|
37
|
+
- lib/lazing.rb
|
38
|
+
- lib/lazing/filters.rb
|
39
|
+
- lib/lazing/transformers.rb
|
40
|
+
- spec/lazing/filters_spec.rb
|
41
|
+
- spec/lazing/transformers_spec.rb
|
42
|
+
- spec/spec_helper.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/gregspurrier/lazing
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options:
|
49
|
+
- --charset=UTF-8
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.3.7
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: Making Enumerable a little lazy
|
75
|
+
test_files:
|
76
|
+
- spec/lazing/filters_spec.rb
|
77
|
+
- spec/lazing/transformers_spec.rb
|
78
|
+
- spec/spec_helper.rb
|