Add to int array with ruby; how?

For general discussion related FlowStone
Post Reply
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Add to int array with ruby; how?

Post 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?
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: Add to int array with ruby; how?

Post 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]}
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Add to int array with ruby; how?

Post 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?
"There lies the dog buried" (German saying translated literally)
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: Add to int array with ruby; how?

Post 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...
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Add to int array with ruby; how?

Post 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)
"There lies the dog buried" (German saying translated literally)
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Add to int array with ruby; how?

Post 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.
Post Reply