[Closed] Mapped functions for float arrays?
After all these years, I got curious to use mapped functions for simple value transformations, but they seem to be designed for object collections, or am I missing something?
At least this isn’t working as expected:
a = #(1,2,3,4,5)
mapped fn byTwo val = val*2
b = byTwo a
I was hoping for b == #(2,4,6,8,10)
What am I missing?
Yeah, with complex types, it’s quite useful, you can do
a = #([1,2], [3,4], [5,6], [7,8], [9,10])
mapped fn byTwo val = (val.x *= 2; val.y *= 2)
byTwo a
print a #nomap
but on the other hand this won’t work
a = #([1,2], [3,4], [5,6], [7,8], [9,10])
mapped fn byTwo val = val *= 2
byTwo a
print a #nomap
Same with matrices etc. As long as you keep the original and don’t reassign it (which basically means creating a new value and throwing out the old one – only the old one isn’t thrown out and isn’t replaced), you can do quite a lot. That said, I don’t use it even in those cases where it would work as it doesn’t return the value, only modifies it, and the resulting code where the function is used is more often than not more complicated and in many cases there’s little to no performance to gain.