riffle 0.0.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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +66 -0
- data/Rakefile +1 -0
- data/lib/riffle/core_ext/array/riffle.rb +35 -0
- data/lib/riffle/version.rb +3 -0
- data/lib/riffle.rb +2 -0
- data/riffle.gemspec +24 -0
- data/spec/array_spec.rb +81 -0
- data/spec/spec_helper.rb +2 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f0a462eefd0ef7b1494eec7847a0518a34dd3876
|
4
|
+
data.tar.gz: 47e2520de2f35d39da9eabf321e333c2281ad5e4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 84210dd35055df948f24c8c34c8754458da764227f0cee4c58b0ade7a8b91679dd009f4209596bff8dc1f78695f514381102502b583101d3129a933868cea7ec
|
7
|
+
data.tar.gz: ad6d5c63e120fb904e81489895d4cb0ed9d6b32729166c4aeb2f12e56d06a43443f5ad5ab7ac05c1a180cef76d7e77ad4c50b9b1f7cf9cbdb2420930f300dd74
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jordan Stephens
|
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,66 @@
|
|
1
|
+
# riffle
|
2
|
+
|
3
|
+
Extends `Array` by defining `Array#riffle` to merge multiple arrays as if
|
4
|
+
[riffling a deck of cards](http://en.wikipedia.org/wiki/Shuffling#Riffle).
|
5
|
+
|
6
|
+
**Algorithm Description**
|
7
|
+
|
8
|
+
`Array#riffle` iterates over argument arrays and selects a random number
|
9
|
+
of items from each to remove from the front (this default subsequence
|
10
|
+
length, or *group size* is in `(1..3)`). The removed items are then
|
11
|
+
appended to a new array which is returned as the result after all items
|
12
|
+
from each argument array have been merged. This iteration continues
|
13
|
+
until all argument arrays are empty and all items have been merged.
|
14
|
+
|
15
|
+
The order of items in each argument array is preserved in the resulting
|
16
|
+
array relative to other items originating from the same argument array.
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
### Basic Usage
|
21
|
+
|
22
|
+
numbers = [1, 2, 3, 4, 5, 6]
|
23
|
+
letters = %w(a b c d e f)
|
24
|
+
|
25
|
+
numbers.riffle(letters) # => [1, 2, "a", "b", 3, 4, "c", 5, 6, "d", "e", "f"]
|
26
|
+
|
27
|
+
### Modifying the Possible *Group Size* Range
|
28
|
+
You may also pass an options `Hash` as the last argument to define the
|
29
|
+
possible range of the randomly determined *group size* at each iteration:
|
30
|
+
|
31
|
+
numbers.riffle(letters, { min_group_size: 2, max_group_size: 8 })
|
32
|
+
|
33
|
+
**Note on Random Subsequence Lengths**
|
34
|
+
|
35
|
+
The resulting subsequence length from any given argument is *random*,
|
36
|
+
so it is very likely that consecutive runs will produce
|
37
|
+
different results.
|
38
|
+
|
39
|
+
numbers.riffle(letters) # => [1, "a", "b", 2, 3, "c", 4, 5, 6, "d", "e", "f"]
|
40
|
+
numbers.riffle(letters) # => [1, 2, 3, "a", "b", 4, 5, "c", "d", "e", 6, "f"]
|
41
|
+
|
42
|
+
## Installation
|
43
|
+
|
44
|
+
Add this line to your application's Gemfile:
|
45
|
+
|
46
|
+
gem 'riffle'
|
47
|
+
|
48
|
+
And then execute:
|
49
|
+
|
50
|
+
$ bundle
|
51
|
+
|
52
|
+
Or install it yourself as:
|
53
|
+
|
54
|
+
$ gem install riffle
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
1. Fork it
|
59
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
60
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
61
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
62
|
+
5. Create new Pull Request
|
63
|
+
|
64
|
+
## Running Tests
|
65
|
+
|
66
|
+
$ rspec spec
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,35 @@
|
|
1
|
+
|
2
|
+
class Array
|
3
|
+
def riffle(*args)
|
4
|
+
result = []
|
5
|
+
|
6
|
+
return self if args.empty?
|
7
|
+
|
8
|
+
opts = args.pop if args.length > 1 && args.last.is_a?(Hash)
|
9
|
+
|
10
|
+
min_group_size = (opts && opts[:min_group_size]) || 1
|
11
|
+
max_group_size = (opts && opts[:max_group_size]) || 3
|
12
|
+
|
13
|
+
# insert self at front of args list
|
14
|
+
args.unshift self
|
15
|
+
|
16
|
+
unless args.select { |a| !a.is_a? Array }.empty?
|
17
|
+
raise ArgumentError, "All arguments must be of type `Array`"
|
18
|
+
end
|
19
|
+
|
20
|
+
until args.flatten.empty?
|
21
|
+
args.each_with_index do |arg, i|
|
22
|
+
group_size = rand(min_group_size..max_group_size)
|
23
|
+
if arg.length < group_size
|
24
|
+
result.concat arg
|
25
|
+
args[i] = []
|
26
|
+
else
|
27
|
+
result.concat arg.take(group_size)
|
28
|
+
args[i] = arg.drop group_size
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
result
|
34
|
+
end
|
35
|
+
end
|
data/lib/riffle.rb
ADDED
data/riffle.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'riffle/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "riffle"
|
8
|
+
spec.version = Riffle::VERSION
|
9
|
+
spec.authors = ["Jordan Stephens"]
|
10
|
+
spec.email = ["iam@jordanstephens.net"]
|
11
|
+
spec.description = %q{defines Array#riffle to merge multiple arrays as if riffling a deck of cards. }
|
12
|
+
spec.summary = %q{riffle multiple Arrays by selecting a random number of items from each and appending to a new result Array until all arugment Arrays are empty and all elements have been merged. }
|
13
|
+
spec.homepage = "http://github.com/jordanstephens/riffle"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^spec/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
data/spec/array_spec.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Array do
|
4
|
+
it "Array#riffle should be defined" do
|
5
|
+
[].respond_to?(:riffle).should be_true
|
6
|
+
end
|
7
|
+
|
8
|
+
it "returns self if no args are passed" do
|
9
|
+
a = [1, 2, 3]
|
10
|
+
a.riffle.should == a
|
11
|
+
end
|
12
|
+
|
13
|
+
it "requires all arguments to be of type `Array`, except when the last arg is an opts Hash" do
|
14
|
+
expect { [].riffle 1 }.to raise_error(ArgumentError)
|
15
|
+
expect { [].riffle [1], :foo }.to raise_error(ArgumentError)
|
16
|
+
expect { [].riffle({}, [1]) }.to raise_error(ArgumentError)
|
17
|
+
|
18
|
+
# allow an opts Hash to be passed as the last arg
|
19
|
+
expect { [].riffle [1], { min_group_size: 1 } }.to_not raise_error
|
20
|
+
end
|
21
|
+
|
22
|
+
it "does not raise exceptions when empty args are passed" do
|
23
|
+
expect { [].riffle [], [] }.to_not raise_error
|
24
|
+
[].riffle([], []).should be_empty
|
25
|
+
end
|
26
|
+
|
27
|
+
it "supports more than two arguments" do
|
28
|
+
numbers = [1, 2, 3, 4, 5, 6]
|
29
|
+
letters = %w(a b c d e f)
|
30
|
+
symbols = [:foo, :bar, :baz, :qux]
|
31
|
+
result = numbers.riffle letters, symbols
|
32
|
+
|
33
|
+
result.length.should == numbers.length +
|
34
|
+
letters.length +
|
35
|
+
symbols.length
|
36
|
+
|
37
|
+
# filtering out integers from the result should be exactly equal to the
|
38
|
+
# original `numbers` array. particularly the order should be the same
|
39
|
+
result.select { |r| r.is_a? Integer }.should.eql? numbers
|
40
|
+
|
41
|
+
# filtering out strings from the result should be exactly equal to the
|
42
|
+
# original `letters` array. particularly the order should be the same
|
43
|
+
result.select { |r| r.is_a? String }.should.eql? letters
|
44
|
+
|
45
|
+
# filtering out strings from the result should be exactly equal to the
|
46
|
+
# original `letters` array. particularly the order should be the same
|
47
|
+
result.select { |r| r.is_a? Symbol }.should.eql? symbols
|
48
|
+
end
|
49
|
+
|
50
|
+
it "merges all argument arrays while maintaining the order of the original arrays in relation to themselves" do
|
51
|
+
numbers = [1, 2, 3, 4, 5, 6]
|
52
|
+
letters = %w(a b c d e f)
|
53
|
+
result = numbers.riffle letters
|
54
|
+
|
55
|
+
result.length.should == numbers.length + letters.length
|
56
|
+
|
57
|
+
# filtering out integers from the result should be exactly equal to the
|
58
|
+
# original `numbers` array. particularly the order should be the same
|
59
|
+
result.select { |r| r.is_a? Integer }.should.eql? numbers
|
60
|
+
|
61
|
+
# filtering out strings from the result should be exactly equal to the
|
62
|
+
# original `letters` array. particularly the order should be the same
|
63
|
+
result.select { |r| r.is_a? String }.should.eql? letters
|
64
|
+
end
|
65
|
+
|
66
|
+
it "maintains the first element in the array on which `riffle` was called" do
|
67
|
+
a = [1, 2, 3]
|
68
|
+
b = %w(a b c)
|
69
|
+
a.riffle(b).first.should == a.first
|
70
|
+
end
|
71
|
+
|
72
|
+
it "supports defining a min and max group size through an opts Hash passed as the last arg" do
|
73
|
+
a = [1, 2, 3, 4, 5, 6, 7, 8]
|
74
|
+
b = %w(a b c d e f g h)
|
75
|
+
r = a.riffle(b, { min_group_size: 2, max_group_size: 2 })
|
76
|
+
r.slice(0..1).each { |x| x.is_a?(Integer).should be_true }
|
77
|
+
r.slice(2..3).each { |x| x.is_a?(String).should be_true }
|
78
|
+
r.slice(6..7).each { |x| x.is_a?(String).should be_true }
|
79
|
+
r.slice(8..9).each { |x| x.is_a?(Integer).should be_true }
|
80
|
+
end
|
81
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: riffle
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jordan Stephens
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: 'defines Array#riffle to merge multiple arrays as if riffling a deck
|
56
|
+
of cards. '
|
57
|
+
email:
|
58
|
+
- iam@jordanstephens.net
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- .gitignore
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/riffle.rb
|
69
|
+
- lib/riffle/core_ext/array/riffle.rb
|
70
|
+
- lib/riffle/version.rb
|
71
|
+
- riffle.gemspec
|
72
|
+
- spec/array_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
homepage: http://github.com/jordanstephens/riffle
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.0.0
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: riffle multiple Arrays by selecting a random number of items from each and
|
98
|
+
appending to a new result Array until all arugment Arrays are empty and all elements
|
99
|
+
have been merged.
|
100
|
+
test_files:
|
101
|
+
- spec/array_spec.rb
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
has_rdoc:
|