Function order of arrays

Hello, I have this function that leads to the right numbers, but the wrong order of arrays:

(~cr={|start,end|

	(start..end).collect{|in|
	var one,two;
		one = Array.series(4,in,1);
		two = Array.series(4,in,-1);
		[one] ++ [two];
}
});

Now, ~cr.(1,3) results in:

-> [ [ [ 1, 2, 3, 4 ], [ 1, 0, -1, -2 ] ], [ [ 2, 3, 4, 5 ], [ 2, 1, 0, -1 ] ], [ [ 3, 4, 5, 6 ], [ 3, 2, 1, 0 ] ] ]

How can I change the function to result in the following order, listing all three [one] first and then all three [two]:

-> [ [ [ 1, 2, 3, 4 ], [ 2, 3, 4, 5 ] ], [ [ 3, 4, 5, 6 ], [ 1, 0, -1, -2 ] ] , [ [2, 1, 0, -1 ], [ 3, 2, 1, 0 ] ] ]

Thank you for you help!

You can use the flop method for this.

(
~cr = {|start,end|
    (start..end).collect{|in|
        var one,two;
        one = Array.series(4,in,1);
        two = Array.series(4,in,-1);
        [one] ++ [two];
    }.flop;
};

~cr.(1, 3);
)

Or you could change the function so that the step size is in the outer loop and the (start…end) is the inner loop:

(
~cr1 = {|start,end|
    [1, -1].collect{|step|
        (start..end).collect{|in|
            Array.series(4, in, step)
        }
    };
};

~cr1.(1, 3) == ~cr.(1, 3); // true
)
1 Like

And again I learnt something new, that’s exciting. Thank you so much for taking the time to help me! Much appreciated.