enumerator-comparable 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/enumerator_comparable.gemspec +19 -0
- data/lib/enumerator_comparable.rb +6 -0
- data/lib/enumerator_comparable/initializers.rb +1 -0
- data/lib/enumerator_comparable/initializers/enumerator_comparable.rb +52 -0
- data/lib/enumerator_comparable/version.rb +3 -0
- metadata +53 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: abbde76fde5739bc18a4a14582582a441e305b0e
|
4
|
+
data.tar.gz: 8902f7dfefd7b8b6e820b0bc039362a008bf0d74
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d76d3f4203a93ca9a96fe47662a485d92f1d340a52c19647d8b621c6d7b5d3b8b375f758725b1a05d04b33e84027698a489e3ddfdd6430cd9b7a2609ee67a10c
|
7
|
+
data.tar.gz: c9cbbba2b6cd196478f4e945f8110a706682ead3d90770274c77386b51ba6a3c9b430d9a42d796c207cb8529cb1708cccbaa574a0e7ec8945ea8ec895c16d7b6
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Bill
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# EnumeratorComparable
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'enumerator_comparable'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install enumerator_comparable
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'enumerator_comparable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "enumerator-comparable"
|
8
|
+
gem.version = EnumeratorComparable::VERSION
|
9
|
+
gem.authors = ["Bill"]
|
10
|
+
gem.email = ["bill.burcham@gmail.com"]
|
11
|
+
gem.description = %q{Makes Enumerators Comparables}
|
12
|
+
gem.summary = %q{Defines spaceship (<=>) operator for Enumerator and mixes in Comparable}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'initializers/enumerator_comparable'
|
@@ -0,0 +1,52 @@
|
|
1
|
+
class Enumerator
|
2
|
+
|
3
|
+
# We define the spaceship operator (<=>) below so why not just go ahead
|
4
|
+
# and let sequences be comparable?
|
5
|
+
include Comparable
|
6
|
+
|
7
|
+
# Stream comparison. Works only for finite sequences i.e. if both
|
8
|
+
# sequences are infinite, then this method never returns.
|
9
|
+
# If you understand dictionary order for words then you understand
|
10
|
+
# how this works. Shorter sequences come before longer sequences.
|
11
|
+
# Here's a method that ought to be defined in Enumerator::Lazy
|
12
|
+
# but isn't.
|
13
|
+
def <=>(other)
|
14
|
+
loop do
|
15
|
+
a, a_end = try_iter &:next
|
16
|
+
b, b_end = other.try_iter &:next
|
17
|
+
# shorter stream comes before longer (matching) stream
|
18
|
+
result = if a_end
|
19
|
+
if b_end
|
20
|
+
0 # equal
|
21
|
+
else
|
22
|
+
-1 # I am shorter
|
23
|
+
end
|
24
|
+
elsif b_end
|
25
|
+
1 # other is shorter
|
26
|
+
elsif a < b
|
27
|
+
-1
|
28
|
+
elsif a > b
|
29
|
+
1
|
30
|
+
end
|
31
|
+
next if result.nil?
|
32
|
+
return result
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Sometimes it is inconvenient to rescue StopIteration exceptions.
|
37
|
+
# This method turns that exception into a boolean value and returns
|
38
|
+
# it along with the next value in the sequence (if any).
|
39
|
+
# Useful for wrapping #peek or #next or any method or block that can
|
40
|
+
# raise StopIteration. For example:
|
41
|
+
# val, done = try_iter &:next
|
42
|
+
def try_iter(&block)
|
43
|
+
x, x_end = nil
|
44
|
+
begin
|
45
|
+
x = yield self
|
46
|
+
rescue StopIteration
|
47
|
+
x_end = true
|
48
|
+
end
|
49
|
+
[x,x_end]
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enumerator-comparable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bill
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Makes Enumerators Comparables
|
14
|
+
email:
|
15
|
+
- bill.burcham@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .gitignore
|
21
|
+
- Gemfile
|
22
|
+
- LICENSE.txt
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- enumerator_comparable.gemspec
|
26
|
+
- lib/enumerator_comparable.rb
|
27
|
+
- lib/enumerator_comparable/initializers.rb
|
28
|
+
- lib/enumerator_comparable/initializers/enumerator_comparable.rb
|
29
|
+
- lib/enumerator_comparable/version.rb
|
30
|
+
homepage: ''
|
31
|
+
licenses: []
|
32
|
+
metadata: {}
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 2.0.3
|
50
|
+
signing_key:
|
51
|
+
specification_version: 4
|
52
|
+
summary: Defines spaceship (<=>) operator for Enumerator and mixes in Comparable
|
53
|
+
test_files: []
|