SciRuby / daru

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

Return a Daru::Vector if multiple values present in mode. #272

Closed dylandy closed 7 years ago

dylandy commented 7 years ago

I found that mode function can only return a single value even when the input vector has multiple modes. Is it a better choice for return a list of mode values rather than return a single value ?

v0dro commented 7 years ago

Good idea. It will return a Daru::Vector incase of multiple values.

v0dro commented 7 years ago

Example:

require 'daru'

v = Daru::Vector.new([1,2,3,4,4,5,5])
v.mode #=> Daru::Vector with [4,5]
baarkerlounger commented 7 years ago

I've implemented this to return a Daru::Vector as suggested but would it not be better to return an array of 1 or more values so that the return type is always consistent and doesn't need to be checked for on the calling side?

zverok commented 7 years ago

@db579 Let's postpone this question or put it in a separate GitHub issue for discussion (I believe, we have several places with the same concept). In fact, the idiom is not unusual for Ruby:

array = [1,2,3]
array[1] # => 2 # single value
array[1..2] # => [2, 3] # array of values

Though, in this case user explicitly requires one value or range of values, unlike the #mode.