SciRuby / daru

Data Analysis in RUby
BSD 2-Clause "Simplified" License
1.03k stars 139 forks source link

Map method. Should Daru::Vector return an Array? #512

Open kojix2 opened 5 years ago

kojix2 commented 5 years ago

Daru's vector returns an array when you call the map method.

v = Daru::Vector[1,2,3,4]
v.map {|i| i}
# => [1, 2, 3, 4]

map returns an array when you include module Enumerable. But, there is no rule that the map method must return an array in Ruby. For example,

require 'numo/narray'
a = Numo::SFloat.new(10).seq
a.map{|i| i}
#=> Numo::SFloat#shape=[10]
#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In Daru, recode return a Vector.

v = Daru::Vector[1,2,3,4]
v.recode {|i| i}
#=> #<Daru::Vector(4)>
#   0   1
#   1   2
#   2   3
#   3   4

This practice is not so common. I think map should return Daru :: Vector.

If there is room to change the API from now, please consider.

him666 commented 5 years ago

looking into this

kojix2 commented 5 years ago

Thank you for your work. @him666

But what I suggested was to change the behavior of the map method.

Currently, the map method returns an Array, but I suggested changing it to return Daru::Vector.

This is not a technical issue but a user consensus issue.

map_to_vector is similar to recode.
So I don't think we need a new method.

ncs1 commented 4 years ago

I too, think that map on Daru::Vector should return Daru::Vector and not Array.