by perfunctory · 6y ago · 0 comments
One way to alleviate the problem is to use naming convention.
normalize(a) # mutates in place. note: no return value.
b = normalized(a) # no mutation. returns a new instance. note the adjective.
Python does this in the standard library a.sort()
b = sorted(a)a = a.sort or a.sort!
I think this is something that is a slightly less obvious when dealing with classes. I see stuff like this in academic code all the time:
class SomeDataObj:
def __init__(self, data):
self.data = data
def normalize(self):
self.data = normalize(self.data)
I'd much prefer for there to be variable `self.data_normalized` which is initialized to None or even better a getter `getNormalizedData()` that computes/caches a normalized variable.The solution was to dictate that the API is copying. But also provide ways to allow the user to manage their own memory.
e.g.
a = tensor.New(tensor.WithShape(2,2), tensor.Of(Float64))
b, err := a.Apply(sigmoid) // copies
c, err := a.Apply(sigmoid, tensor.WithReuse(a)) // mutates
Docs: https://godoc.org/gorgonia.org/tensor#Dense.Apply[1] https://docs.julialang.org/en/v1/base/collections/#Base.sum!
Both Julia and Ruby got this from Scheme and it might be even older than that.
The other two major ones are the number of core ways to the the same thing and the slowness of the interpreter. The first is a matter of taste. The second is a technical issue with the interpreter mostly independent of the language and/or a question of fitness for specific projects.
I spent far too much time yesterday figuring out why nothing was being replaced, when I called replace on a string in Javascript