SciRuby / daru

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

allow to call count method without parameter for category type vector #389

Closed rainchen closed 6 years ago

rainchen commented 6 years ago

@zverok updated.

About the nil value issue, I'm not quite sure how to deal with. I wish to keep using the same style with count method after the vector converted to category type, e.g.:

vc.count  # => 5
vc2 = vc.to_category
vc2.count  # => 5, excepted

how about using a special :nil key for counting nil values? .e.g: vc.count(:nil)

zverok commented 6 years ago

About the nil value issue, I'm not quite sure how to deal with.

There is "undefined object" pattern for such cases:

UNDEFINED = Object.new.freeze # anonymous object, equal only to itself
def count category=UNDEFINED
  return .... if category == UNDEFINED

With this idiom, we know for sure that UNDEFINED would not be one of the categories in the vector, so, everything works always as expected, even for nil category.

rainchen commented 6 years ago

@zverok good idea

rainchen commented 6 years ago

@zverok All checks have passed, plz review