prepend to each item in a list?
How would I [prepend] the same thing to each item in a list? The simplest solution I've thought of is using [unpack] and n different [prepend]s inline with the different outlets, but this doesn't seem like it'd scale well. I tried some solutions with [zl slice] / [zl iter] but I can't seem to figure it out.
I come from javascript and I'd do this really simply there likemyList.map(elem => ['extra', elem])
Javascript is what would come easily to me too. So have you considered doing this in Javascript via Max's js object?
zl iter 1
|
prepend
?
I don't know. If you would like me to provide a simple js example, please let me know.
@schlam in this case there's only one outlet now, out of the prepend. how do I
turn that into say 7 outlets, for 7 different objects? essentially I want to have a list of 7 different messages with the content "text Hello". the example use case is more clear in case that isn't
@simon I tried a [jstrigger (a.map(function(e) {return ['text', e];}))]. My desired output is of the format [['text', a], ['text', b], ['text', c]] but it seems Max doesn't like nested arrays. if you know how to achieve this in JS then yes an example would be appreciated
@source, this is awesome, thank you. Still reading through it to understand (the purpose of the [del] and [listfunnel] confuse me).
Would you know how to create a [jit.gl.text] object to accept each one of these `text` messages now? I want to be able to handle a variable amount of input text. For now I'm hard wiring 10 [jit.gl.text] and
limiting the text to 10 words.
I am very short of time, so as short answers.
list funnel prepends indexes for each item in a list, starting with 0
if not set differently.
That allows routing of them.
del 10 ist there to bang zl group and so output collected items
after all itered symbols have passed.
---
sorry, no time to look into jit.gl.text now
I had a go at doing this with js. One challenge was that Max uses a rather old version of Javascript, ES5, which is apparently not going to be upgraded to ES6 any time soon. There's a good discussion of this on this forum thread: Any plans to update support for recent versions of JS? I believe @Kevin's nice easy way of making a nested list in Javascript would require version ES6.
Also, I could not work out how to get Max to understand the nested list output by Javascript. So I've commented out the code for doing that and instead output multiple lists, one for each element of the input list.
Here's the Javascript code. It should be pasted to file PrependToListItems.js in the same folder as the test patcher that follows, or elsewhere on Max's search path.
// Outputs multiple lists, each of which contains the same prefix element
// followed by an element of a source list.
// Specify the number of inlets and outlets.
inlets = 1;
outlets = 1;
// Tooltips for the inlet and outlets
setinletassist(0,
"prefix (symbol): The text to be prepended to each item in the list "
+ "specified by the sourcelist message. "
+ "sourcelist (list): The text specified by the prefix message will "
+ "be prepended to each item of this list.");
setoutletassist(0,
"Multiple lists, derived from the prefix and sourcelist input messages.");
var Prefix = "Default Prefix";
// A function with this name accepts any input message from the inlet(s).
function anything() {
// Convert the input Max list (excluding the message name),
// into a Javascript array.
input = arrayfromargs(arguments);
// Process the expected message types
switch (messagename) {
case "prefix":
Prefix = input[0];
break;
case "sourcelist":
// Send a derived array to the outlet for each element of the source list.
// It will be automatically converted to a Max list when sent to the outlet.
for (i = 0; i < input.length; i++) {
outlet(0, [Prefix, input[i]]);
}
// NOTE: I tried outputting just one list, where each element was a sub-list
// corresponding an element of the source list. But I don't know how
// to make Max understand it. Here's the code commented out:
// output = [];
// for (i = 0; i < input.length; i++) {
// output[i] = [Prefix, input[i]];
// }
// outlet(0, output);
break;
}
}
Now that I looked at your initial patch, makes me think that all this text prepending is
not needed at all.
What are you exactly trying to do ?
To split a sentence into single words and display each in separate jit.gl.text object ?
that can be done just by using message text $1 in front of each jit.gl.text object
after splitting of the sentence.
Then you deal with text objects as you want.
just unjoin or unpack the text.

unjoin as many words as needed.
But dealing with multiple jit.gl.text objects ?
What is their purpose ?
Oh wow, that's a lot more elegant, thanks so much. I want to independently position / animate different words in a sentence - would there be a better way?
simple iteration seems to be the most straight forward approach for me for all kind of processes done to list items (except mathematical functions which can be done in vexpr)
due to the nature of [prepend], which changes the list length & can have more than one argument, you would need to multiply the list length likewise, but otherwise it is pretty simple.
if i would need to make an abstraction for it, it would look like that:

All that animation, jit and gl stuff is not my field of interest at all,
and so can't be of any help with it.
You have a good exampe how to split the sentence,
positioning the words on the screen can be done also using lcd
or other objects as well.