backports 1.12.3 → 1.13.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  ---
2
- :patch: 3
3
2
  :major: 1
3
+ :minor: 13
4
+ :patch: 0
4
5
  :build:
5
- :minor: 12
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{backports}
8
- s.version = "1.12.3"
8
+ s.version = "1.13.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Marc-Andr\303\251 Lafortune"]
12
- s.date = %q{2010-01-13}
12
+ s.date = %q{2010-01-20}
13
13
  s.description = %q{ Essential backports that enable some of the really nice features of ruby 1.8.7, ruby 1.9 and rails from ruby 1.8.6 and earlier.
14
14
  }
15
15
  s.email = %q{github@marc-andre.ca}
@@ -1,4 +1,3 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + "/backports/tools")
2
2
  Backports.require_relative "backports/1.9"
3
3
  Backports.require_relative "backports/rails"
4
-
@@ -90,6 +90,31 @@ class Array
90
90
  alias_method :find_index, :index
91
91
  end
92
92
 
93
+ # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
94
+ def permutation(num = Backports::Undefined, &block)
95
+ return to_enum(:permutation, num) unless block_given?
96
+ num = num.equal?(Backports::Undefined) ?
97
+ size :
98
+ Backports.coerce_to(num, Fixnum, :to_int)
99
+ return self unless (0..size).include? num
100
+
101
+ final_lambda = lambda do |partial, remain|
102
+ yield partial
103
+ end
104
+
105
+ outer_lambda = num.times.inject(final_lambda) do |proc, ignore|
106
+ lambda do |partial, remain|
107
+ remain.each_with_index do |val, i|
108
+ new_remain = remain.dup
109
+ new_remain.delete_at(i)
110
+ proc.call(partial.dup << val, new_remain)
111
+ end
112
+ end
113
+ end
114
+
115
+ outer_lambda.call([], self)
116
+ end unless method_defined? :permutation
117
+
93
118
  # pop. Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
94
119
  unless ([1].pop(1) rescue false)
95
120
  def pop_with_optional_argument(n = Backports::Undefined)
@@ -105,22 +130,27 @@ class Array
105
130
 
106
131
  # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
107
132
  def product(*arg)
108
- # Implementation notes: We build an enumerator for all the combinations
109
- # by building it up successively using "inject" and starting from a trivial enumerator.
110
- # It would be easy to have "product" yield to a block but the standard
111
- # simply returns an array, so you'll find a simple call to "to_a" at the end.
133
+ # Implementation notes: We build a block that will generate all the combinations
134
+ # by building it up successively using "inject" and starting with one
135
+ # responsible to append the values.
112
136
  #
113
- trivial_enum = Enumerator.new(Backports::Yielder.new{|yielder| yielder.yield [] }) # Enumerator.new{...} is 1.9+ only
114
- [self, *arg].map{|x| Backports.coerce_to(x, Array, :to_ary)}.
115
- inject(trivial_enum) do |enum, array|
116
- Enumerator.new(Backports::Yielder.new do |yielder| # Enumerator.new{...} is 1.9+ only
117
- enum.each do |partial_product|
118
- array.each do |obj|
119
- yielder.yield partial_product + [obj]
120
- end
121
- end
122
- end)
123
- end.to_a
137
+ result = []
138
+
139
+ arg.map!{|x| Type.coerce_to(x, Array, :to_ary)}
140
+ arg.reverse! # to get the results in the same order as in MRI, vary the last argument first
141
+ arg.push self
142
+
143
+ outer_lambda = arg.inject(result.method(:push)) do |proc, values|
144
+ lambda do |partial|
145
+ values.each do |val|
146
+ proc.call(partial.dup << val)
147
+ end
148
+ end
149
+ end
150
+
151
+ outer_lambda.call([])
152
+
153
+ result
124
154
  end unless method_defined? :product
125
155
 
126
156
  # rindex. Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backports
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.12.3
4
+ version: 1.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Marc-Andr\xC3\xA9 Lafortune"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-13 00:00:00 -05:00
12
+ date: 2010-01-20 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15