Re:Copying data is wasteful, mutating data is dangerous
by nnq on Friday January 10, 2020 @06:08AM
(#22010148)
Better pattern is to follow the style of Numpy or Pandas and other libs (that OP also mentioned, but then failed to apply it in his own examples), letting the caller/user of your functions choose, if/when they want better performance, to opt-in for in-place modification, like have either:
or:
Please, do follow this pattern for any libraries you release, having value-semantics-by-default to prevent shooting yourself in the foot + opt-in in-place mutation option for better memory performance is the right thing, and it's quite easy too with Python, Numpy and Pandas!
[ Parent
| on HN ]
Re:Copying data is wasteful, mutating data is dangerous
by ourlordcaffeine on Friday January 10, 2020 @06:27AM
(#22010232)
Julia has this as well, functions with a '!' at the end mutate the data, functions without make a copy.
So you have the functions 'filter' and 'filter!', 'sort' and 'sort!' etc.