daru
daru copied to clipboard
[API] Vector: adding new values
Here is the list of what feels right for me, ready to discuss!
I assume that Daru::Vector is a bit like Ruby's Array, and a bit like Hash. So:
- like Array:
-
push(index, value)
to add index-value pair to the end of vector (and<<
as a synonym); - (not sure)
<< value
-- pushes value + auto-calculated index (byvector.index.last.succ
, for ex.); -
insert_after(lookup_index, new_index, new_value)
(andinsert_before
for symmetry);
-
- like Hash:
-
vector[:non_existent_index] = value
should work, not raise IndexError (unless there is REALLY good reason for current behavior); - (not sure)
merge(index: value)
(andmerge!
, of course).
-
Well Vector/DataFrame don't function exactly like Array and are not designed to be expanded. It would involve a lot of unnecessary overhead like checking for missing value, updating missing value, size and indexing data structures internally and in general consuming more memory and time than is necessary.
You'd rather just use an Array, expand it as much as you want and then create a Vector from it for performing computations.
Which once again return us to https://github.com/v0dro/daru/issues/137 ;)
What I think about (looking at real R code using dataframes heavily, which I now can access at work) is all-in-all Vector and DataFrame as just a new containers (which I personally would like to have as popular as an Array/Hash), and they should be as intuitive in that role as possible. And if daru's user wants to create dataframe with 100 rows, then add another 100 by parsing some data, and then another 100 using some API -- there is no really good (e.g. conceptual) reason why he/she can't "just do it".
Those containers aren't immutable, they aren't even immutable in some of directions (like "you can compute new columns, but can't add new rows"), so, this limitation (it is not easy/intuitive to ad new row to DF/new value to Vector) seems pretty artificial.
Hmmmm you have a point, but I'd like to discuss this and #137 further before we come to any decisions. It can have far reaching implications for daru as a whole.
vector[:non_existent_index] = value
should work, not raise IndexError (unless there is REALLY good reason for current behavior);
I'm about to work on Daru::Vector#[]=
and was wondering if this feature should be supported. For Data frames, this feature is supported.
(Adding to milestone, needs reconsidering in a common flow of cleanup)