// Increment all elements by one (array-wise expression)
half[] += 1;
Is this really that useful? Seems like a weirdly specific code smell... // Increment all elements by one (array-wise expression)
half[] += 1;
Is this really that useful? Seems like a weirdly specific code smell...I know Ruby, so here is what I'm used to:
half.map { |i| i + 1 }
I also know Elixir: Enum.map(half, fn i -> i + 1 end)
...and JS, I guess because we have to: half.map(i => i + 1) // It looks just like the Ruby one
...vs whatever the Python version is... I don't know Python very well, but I imagine it involves list comprehensions...Compare all those to the aforementioned D version :
half[] += 1;
I'm really feeling the D version!EDITED to fix little mistakes.
It just seems like a weirdly specific syntactic sugar... which is a "language smell"... to me
e.g.:
arr1[] += arr2[] / 10.0 + 5;
TBH, I don't use this feature much, because I work with ranges more than arrays, which do not have this ability. This feature predates ranges (and the std.algorithm.map function, which can do what you say as well).
You can also mix and match scalar and slices, so for audio code it's quite cool:
a[0..frames] += b[0..frames] * volume;In this case the syntax lowers to a template which handles the iteration for you. It is not special, it gets treated the same by the compiler backend, but it will almost definitely be inlined & vectorized if the compiler is allowed to.
GCC and LLVM are actually clever enough to call memset for you if they deem it profitable.
Looking at the notes, it may have separate syntax to make compiler vectorization and parallelization easier. Plus it's pretty terse.