Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/ludy/array/pad.rb,
lib/ludy/array/body.rb,
lib/ludy/array/head.rb,
lib/ludy/array/tail.rb,
lib/ludy/array/count.rb,
lib/ludy/array/foldl.rb,
lib/ludy/array/foldr.rb,
lib/ludy/array/choice.rb,
lib/ludy/array/combos.rb,
lib/ludy/array/filter.rb,
lib/ludy/array/rotate.rb,
lib/ludy/array/combine.rb,
lib/ludy/array/product.rb,
lib/ludy/deprecated/unzip.rb,
lib/ludy/array/map_with_index.rb,
lib/ludy/deprecated/untranspose.rb
Instance Method Summary collapse
-
#body ⇒ Object
strip the last element [1,2,3].body => [1,2].
-
#choice ⇒ Object
it would be defined if RUBY_VERSION < ‘1.9.0’, see rdoc in ruby 1.9.
-
#choice! ⇒ Object
the choosen element would be deleted.
-
#combine(*target) ⇒ Object
example: [1,2,3].combine [2,4,6] => [3,6,9].
-
#combine!(*target) ⇒ Object
inplace version of combine.
- #combos ⇒ Object
- #count(t) ⇒ Object
-
#foldl(func, init) ⇒ Object
fold from left to right (it just like inject).
-
#foldr(func, init) ⇒ Object
fold from right to left.
-
#map_with_index ⇒ Object
just like each_with_index.
-
#pad(n, t = nil) ⇒ Object
ensure the size is at least n, pad with n, default nil.
-
#pad!(n, t = nil) ⇒ Object
inplace version of pad.
-
#product(*args) ⇒ Object
it would be defined if RUBY_VERSION < ‘1.9.0’, see rdoc in ruby 1.9.
-
#rotate(n = 1) ⇒ Object
rotate right with size.
-
#rotate! ⇒ Object
inplace version of rotate.
-
#tail ⇒ Object
simply call array.
- #untranspose ⇒ Object
- #untranspose! ⇒ Object
- #unzip ⇒ Object
- #unzip! ⇒ Object
Instance Method Details
#body ⇒ Object
strip the last element
[1,2,3].body
=> [1,2]
6 7 8 |
# File 'lib/ludy/array/body.rb', line 6 def body self[0..-2] end |
#choice ⇒ Object
it would be defined if RUBY_VERSION < ‘1.9.0’, see rdoc in ruby 1.9
6 7 8 |
# File 'lib/ludy/array/choice.rb', line 6 def choice self[rand(size)] end |
#choice! ⇒ Object
the choosen element would be deleted. return the choosen
10 11 12 13 14 15 |
# File 'lib/ludy/array/choice.rb', line 10 def choice! i = rand size r = self[i] self.delete_at i r end |
#combine(*target) ⇒ Object
example:
[1,2,3].combine [2,4,6]
=> [3,6,9]
[1,2].combine [1,2], [1,2]
=> [3,6]
['a','b'].combine ['b','a']
=> ['ab','ba']
14 |
# File 'lib/ludy/array/combine.rb', line 14 def combine *target; zip(*target).map{|i|i.inject(&:+)}; end |
#combine!(*target) ⇒ Object
inplace version of combine
16 |
# File 'lib/ludy/array/combine.rb', line 16 def combine! *target; replace combine(*target); end |
#combos ⇒ Object
for each combos
[[0,1],[2,3]].combos
=> [[0,2],[0,3],[1,2],[1,3]]
simply:
array.first.product *array.tail
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/ludy/array/combos.rb', line 11 def combos result = [] radixs = reverse.map(&:size) inject(1){|r, i| r * i.size}.times{ |step| result << foldr(lambda{ |i, r| radix = radixs[r.size] r.unshift i[step % radix] step /= radix unless radix.nil? r }, []) } result end |
#count(t) ⇒ Object
6 7 8 |
# File 'lib/ludy/array/count.rb', line 6 def count t inject(0){|r,i| i==t ? r+=1 : r} end |
#foldl(func, init) ⇒ Object
fold from left to right (it just like inject)
4 |
# File 'lib/ludy/array/foldl.rb', line 4 def foldl func, init; inject init, &func; end |
#foldr(func, init) ⇒ Object
fold from right to left
4 5 6 7 |
# File 'lib/ludy/array/foldr.rb', line 4 def foldr func, init reverse_each{ |i| init = func[i, init] } init end |
#map_with_index ⇒ Object
just like each_with_index
4 5 6 7 |
# File 'lib/ludy/array/map_with_index.rb', line 4 def map_with_index i = -1 map{ |e| i+=1; yield e, i; } end |
#pad(n, t = nil) ⇒ Object
ensure the size is at least n, pad with n, default nil
4 5 6 7 |
# File 'lib/ludy/array/pad.rb', line 4 def pad n, t = nil return self if n <= size self + [t]*(n-size) end |
#pad!(n, t = nil) ⇒ Object
inplace version of pad
9 10 11 12 |
# File 'lib/ludy/array/pad.rb', line 9 def pad! n, t = nil return self if n <= size concat([t]*(n-size)) end |
#product(*args) ⇒ Object
it would be defined if RUBY_VERSION < ‘1.9.0’, see rdoc in ruby 1.9
12 13 14 |
# File 'lib/ludy/array/product.rb', line 12 def product *args args.unshift(self).combos end |
#rotate(n = 1) ⇒ Object
rotate right with size. if the size is negative, rotate left.
[1,2,3].rotate
=> [2,3,1]
[1,2,3].rotate -1
=> [3,1,2]
[1,2,3].rotate 2
=> [3,1,2]
13 14 15 16 |
# File 'lib/ludy/array/rotate.rb', line 13 def rotate n = 1 return self if empty? or n == 0 self[n..-1] + self[0...n] end |
#rotate! ⇒ Object
inplace version of rotate
18 19 20 |
# File 'lib/ludy/array/rotate.rb', line 18 def rotate! replace rotate end |
#tail ⇒ Object
simply call
array[1..-1]
5 6 7 |
# File 'lib/ludy/array/tail.rb', line 5 def tail self[1..-1] end |
#untranspose ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/ludy/deprecated/untranspose.rb', line 3 def untranspose result = ([nil]*self.first.size).map{[]} self.each{ |zipped| zipped = zipped.clone result.each{ |r| r << zipped.shift } } result end |
#untranspose! ⇒ Object
11 |
# File 'lib/ludy/deprecated/untranspose.rb', line 11 def untranspose!; replace untranspose; end |
#unzip ⇒ Object
5 |
# File 'lib/ludy/deprecated/unzip.rb', line 5 def unzip; untranspose.first; end |
#unzip! ⇒ Object
6 |
# File 'lib/ludy/deprecated/unzip.rb', line 6 def unzip!; replace unzip; end |