Page 1 of 1
Add to int array with ruby; how?
Posted: Sun Mar 04, 2018 2:40 pm
by kortezzzz
Hi all. A ruby question:
Seems like we have to use ruby to add each int value in a int array with a fixed number, because there is no "add to int array" primitive. lets say I have A given array like
3
4
2
3
and I would like to add 2 to each value and end up with
5
6
4
5
How it done with ruby, by using 2 inputs (one input for the array and one for the int value that added to each value in the array) and one int array output for the results?
Re: Add to int array with ruby; how?
Posted: Sun Mar 04, 2018 2:45 pm
by KG_is_back
assuming first input is the array and second input is the integer
Code: Select all
output @ins[0].map!{|v| v+@ins[1]}
Re: Add to int array with ruby; how?
Posted: Sun Mar 04, 2018 2:55 pm
by tulamide
I wouldn't use the altering version. Just "map" without "!" will do. You're outputting an array anyway so why bothering with the slower alteration of the original array?
Re: Add to int array with ruby; how?
Posted: Sun Mar 04, 2018 3:21 pm
by KG_is_back
tulamide wrote:I wouldn't use the altering version. Just "map" without "!" will do. You're outputting an array anyway so why bothering with the slower alteration of the original array?
the ! is in fact faster, because it does not allocate a second array - it just modifies the existing one element by element. The inputs and outputs inside ruby actually aren't directly usable by flowstone - flowstone converts them to green connectors on the way out and vice versa for the inputs. Using the ! version ensures that internally, the same array is in the @ins array and @outs array, saving memory and CPU. Although very marginally...
Re: Add to int array with ruby; how?
Posted: Sun Mar 04, 2018 3:42 pm
by tulamide
KG_is_back wrote:tulamide wrote:I wouldn't use the altering version. Just "map" without "!" will do. You're outputting an array anyway so why bothering with the slower alteration of the original array?
the ! is in fact faster, because it does not allocate a second array - it just modifies the existing one element by element. The inputs and outputs inside ruby actually aren't directly usable by flowstone - flowstone converts them to green connectors on the way out and vice versa for the inputs. Using the ! version ensures that internally, the same array is in the @ins array and @outs array, saving memory and CPU. Although very marginally...
You're right! Since Flowstone doesn't allow for long iterations, I looked on the internet instead. Here is one example, where "merge" is used in both forms (I just learned the ! ones are called the bang! versions). Time is in seconds, array length is 10000, ten consecutive runs
Code: Select all
user system total real
merge! 0.010000 0.000000 0.010000 ( 0.011370)
merge 17.710000 0.000000 17.710000 ( 17.840856)
Re: Add to int array with ruby; how?
Posted: Sun Mar 04, 2018 4:15 pm
by kortezzzz
Thanks KJ, but this ime tula's "no !" worked as expected
For some reason, when you add the "!" to the code, it saves older values and then add them with the new ones.