What is Vector#map2?

Hi,

Can anyone give me one example to understand the functionality of
Vector#map2(http://yard.ruby-doc.org/stdlib-2.0/Vector.html#collect2-instance_method).
I understand the Vector#map.

require ‘matrix’

a = [1, 2, 3, 4]
m = Vector[*a] # => Vector[1, 2, 3, 4]
m.map{|i| i * i } # => Vector[1, 4, 9, 16]

Am 05.07.2013 20:25, schrieb Love U Ruby:

m.map{|i| i * i } # => Vector[1, 4, 9, 16]
Shouldn’t be too difficult to find out by looking at the source,
but anyway:

2.0.0p247 :007 > u = Vector[1, 2, 3]
=> Vector[1, 2, 3]
2.0.0p247 :008 > v = Vector[7, 8, 9]
=> Vector[7, 8, 9]
2.0.0p247 :009 > u.collect2(v) {|e1, e2| e1*e2 }
=> [7, 16, 27]

Regards,
Marcus

On 2013-07-05, at 2:49 PM, [email protected] wrote:

m = Vector[*a] # => Vector[1, 2, 3, 4]
=> [7, 16, 27]
With pry (and pry-plus) it is easy to get to the source and docs for a
method:

[1] pry(main)> require ‘matrix’
=> true
[2] pry(main)> $ Vector#collect2

From: /Users/mike/.rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/matrix.rb @
line 1630:
Owner: Vector
Visibility: public
Number of lines: 8

def collect2(v) # :yield: e1, e2
raise TypeError, “Integer is not like Vector” if v.kind_of?(Integer)
Vector.Raise ErrDimensionMismatch if size != v.size
return to_enum(:collect2, v) unless block_given?
Array.new(size) do |i|
yield @elements[i], v[i]
end
end
[3] pry(main)> ri Vector#collect2
Vector#collect2

(from ruby core)

collect2(v) { |e1, e2| … }


Collects (as in Enumerable#collect) over the elements of this vector and
v in conjunction.

[4] pry(main)>

Hope this helps,

Mike

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

Mike S. wrote in post #1114545:

On 2013-07-05, at 2:49 PM, [email protected] wrote:

m = Vector[*a] # => Vector[1, 2, 3, 4]
=> [7, 16, 27]

I am using sublimeREPL.

1] pry(main)> require “matrix”
=> true
[2] pry(main)> $ Vector#collect2

From: /home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/matrix.rb @
line 1630:
Owner: Vector
Visibility: public
Number of lines: 8

def collect2(v) # :yield: e1, e2
raise TypeError, “Integer is not like Vector” if v.kind_of?(Integer)
Vector.Raise ErrDimensionMismatch if size != v.size
return to_enum(:collect2, v) unless block_given?
Array.new(size) do |i|
yield @elements[i], v[i]
end
end
[3] pry(main)> ri Vector#collect2
error: ‘Vector’ not found
[4] pry(main)>

Getting error…

unknown wrote in post #1114544:

2.0.0p247 :007 > u = Vector[1, 2, 3]
=> Vector[1, 2, 3]
2.0.0p247 :008 > v = Vector[7, 8, 9]
=> Vector[7, 8, 9]
2.0.0p247 :009 > u.collect2(v) {|e1, e2| e1*e2 }
=> [7, 16, 27]

Regards,
Marcus

Thank you very much for giving an example.

Am 05.07.2013 20:59, schrieb Mike S.:

require ‘matrix’

a = [1, 2, 3, 4]
m = Vector[*a] # => Vector[1, 2, 3, 4]
m.map{|i| i * i } # => Vector[1, 4, 9, 16]

Shouldn’t be too difficult to find out by looking at the source,
but anyway:

[…]

With pry (and pry-plus) it is easy to get to the source and docs for a method:

The source is even included in the docs linked to by the OP…

Regards,
Marcus