I am trying to append/ concatenate a global variable to an array.
I have a Pseq as follows:
Pseq([1,2,3], inf);
I am trying to append ‘.scramble’ at the end of the array:
Pseq([1,2,3].scramble, inf);
But instead of using ‘.scramble’ directly, I would like to store it in a global variable called ~scramble.
I have tried the following but it does not work:
~scramble = '.scramble';
Pseq([1,2,3]++~scramble, inf);
Any ideas on how to achieve this? Ultimately, I would like to string together multiple functions, i.e;
'Pseq([1,2,3]++~scramble++~pyramid++rotate, inf)
jordan
2
Having scramble stored in a global var called scramble is pointless, just write scramble directly.
Your code should look like this…
[1,2,3].scramble.pyramid.rotate
… You cannot concatenate (++) a Symbol with an object like that and expect it to work.
… that being said, here is something (after more advanced that you should probably ignore) that actually does something using the idea…
~apply_unary_ops = {|obj, unary_opts|
unary_opts.inject(obj, {|prev, act| prev.perform(act) })
};
~my_operation_seq = ['scramble', 'reverse', 'normalizeSum'];
~a = [1, 2, 3, 4, 5, 6, 7, 8];
~apply_unary_ops.(obj: ~a, unary_opts: ~my_operation_seq)
To use a symbol as a method you need to use .perform
, this can be looped over applying each operation consecutively using inject and a function.
3 Likes
Wow, incredible. Thank you very for for the explanation and workaround!