Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/ludy/array/body.rb,
lib/ludy/array/tail.rb,
lib/ludy/array/foldl.rb,
lib/ludy/array/foldr.rb,
lib/ludy/array/combos.rb,
lib/ludy/array/filter.rb,
lib/ludy/array/rotate.rb,
lib/ludy/array/combine.rb,
lib/ludy/deprecated/unzip.rb,
lib/ludy/array/reverse_map.rb,
lib/ludy/array/map_with_index.rb,
lib/ludy/deprecated/untranspose.rb

Instance Method Summary collapse

Instance Method Details

#bodyObject

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

#combine(*target) ⇒ Object

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’]



13
# File 'lib/ludy/array/combine.rb', line 13

def combine *target; zip(*target).map{|i|i.inject(&:+)}; end

#combine!(*target) ⇒ Object

in-place version of combine



15
# File 'lib/ludy/array/combine.rb', line 15

def combine! *target; replace combine(*target); end

#combosObject

for each combos

[[0,1],[2,3]].combos
=> [[0,2],[0,3],[1,2],[1,3]]


10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ludy/array/combos.rb', line 10

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

#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_indexObject



3
4
5
6
# File 'lib/ludy/array/map_with_index.rb', line 3

def map_with_index
  i = -1
  map{ |e| i+=1; yield e, i; }
end

#reverse_map(&block) ⇒ Object

synonymy for reverse.map



4
5
6
# File 'lib/ludy/array/reverse_map.rb', line 4

def reverse_map &block
  reverse.map(&block)
end

#rotate(size = 1) ⇒ Object

rotate right with size. if the size is negative, rotate left.

[1,2,3].rotate
=> [3,1,2]

[1,2,3].rotate -1
=> [2,3,1]

[1,2,3].rotate 2
=> [2,3,1]


13
14
15
16
17
# File 'lib/ludy/array/rotate.rb', line 13

def rotate size = 1
  head = size > 0 ? last(size) : self[(-size)..-1]
  tail = self - head
  head + tail
end

#rotate!Object

in-place version of rotate



19
20
21
# File 'lib/ludy/array/rotate.rb', line 19

def rotate!
  replace rotate
end

#tailObject



3
4
5
# File 'lib/ludy/array/tail.rb', line 3

def tail
  self[1..-1]
end

#untransposeObject



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

#unzipObject



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