Page 1 of 1

'Folding' a list

Posted: Fri Jul 06, 2012 7:19 am
by cip23
I'm looking for a way to 'fold' a list, so that a given list [1, 2, 3, 4] repeats until a certain index is reached.

Pseudocode:
[1,2,3,4].fold(10)
becomes [1, 2, 3, 4, 1, 2, 3, 4, 1, 2]

I didn't find a zl object to do this, but I could be wrong...

Any ideas?

best regards,
Christoph

Re: 'Folding' a list

Posted: Fri Jul 06, 2012 3:08 pm
by toscanini
That's pretty much what zl group does...

Re: 'Folding' a list

Posted: Fri Jul 06, 2012 3:29 pm
by ShelLuser
I don't think group is very useful here since it also requires to know the size of the list.

Reg otoh. allows you to store a list and output it as many times as you like. So all you need is something like uzi to output a specified number of bangs, like so:

Code: Select all

----------begin_max5_patcher----------
939.3ocyWszaaCCC9rKP+OH3yYA1JNoI6VOVfcaGKJBTrYRTmsjmsRZVK1+8
IK4WJwJw4QK1EaCJQZxO9QRoOt+NG2E7cPtK56nmQNNeHk3njUHwoRfiaBYW
XLIWsQWF7FewqtCJWS.6DJ4oYTlnV7RNSvHIfZoGynj35knQJgRi7M+oFJjS
eWofOdnWk7ThHbMksZdFDJzd5jYxkQ9dSKdMJn3I1anG5kJcXaRnrXPnbX+V
R4aDUh8TR+682U7V9ZPuAfDHOmrBNDAFWKR+eD+IEzdrqaiy0SjYxkfL93wJ
LYh5Id3XILM0Jxf6FY7u8HiOBiFgBtk3SvEwbl1.OAOnXQegvi0JGTLMWHcy
TfHtkXD9xwnGFodN4DEW1vHC6CYknRErH8GZLrExxobV6863RRSaI2osREn4
qbksFOnQFkok42HKC1RqLwrRoEYppeRSvNxCqhvY9JxfNd8mNoU.KSqqh4g+
BT.qWszHX4YZGdJvZTwqXKFO1aqTVZFjCLAQTFKF+bxlXwbK4UyMrjDB1Uua
dji6pLZDmU3Hl5VHu5WJ63nZ3T7rk2q1Bij1k5BNOdAIqHAsHFLS9x5GBilP
DffpcJrWilzD8HFCyALhzLqyCy3wwlVSuz1tVJRRPBg2nQh0Jy0B3LlHZP+Z
WeatvQqyMq027N0XgCpxWPXqbGz7VEwuzVkijzrNd8XMAr0H3AUCxwpgsAG1
HvdyfNZHLpdkxFmUkjWLHqMtsHeh6IBNe8YHFohQ8TS73iFc91iNCjLjmj.Z
Zp6Oj80QeC8npAOhlijkyQHolHBCsCQjFIQf3KQE787gt2bT5XTw2iQYvpiy
FKXfWL8K3ZXekIHOUpIv65ne3aNvp9sm.6L801MyaEm3QpIsX7ntGErGlhc6
UUK1u27ZO6vluMd8SrBZ8OAARrFTzXlhFmA+dCMChP5yxHizOCR84i8s6IdJ
.rjq84BfkMFdhktQigpFDBNZAThcPjUjq0j1XJy5zJUrUrAKXZNeSVXEfUMt
.sWjFA4BJq9DHOWWYu+FWSih1aVu93YQob4LrRmTdhH4cRloOrzzpuQuzeNx
YGU39FTcE98Kp9T8+f95+S9uz88OGNk+M08qVn4n+t4jsPzb4uSVtOmHDYzE
aD5JHy6mbjCFe8GstaMsdVd40OVPhKuJUsMcO4kApi98twpFjM6cTdC18SuU
2i8vzpYYU+Jo5HgZIY140q6uyMoWNG1fw8k4bA814tNjqPf70+P8cmBf
-----------end_max5_patcher-----------

Re: 'Folding' a list

Posted: Fri Jul 06, 2012 4:46 pm
by doubleUG

Code: Select all

<pre><code>
----------begin_max5_patcher----------
583.3oc0VFraiBCDF9LUpuCV9b1JClPB6s8deCVUU4.yl3HvDgcZy1p8cesG
fDRangfnQsW7HFOYxOeLyX+5s23QWTrCzTxOI+l348p0iG5y4wqwgGMWrKIS
nw.oIE44fxPmTuoA1YvMLxrLhMTx8RsgjApkqND0eJTFs7EvEoO6NVi+MBSx
JoZ4ikPhoRHAQyr6SBBQiOKBMyuiQdnc1ThbLazeUJEY6+iTaykpLvfh0uwq
LECsXw5eDD1N1hsllfYn2+c6MNq0Lo2HIGzZwR38HwmDP3jPxQ18gkIUPRwV
EFK+bjpRol+tApvDkdfGmfh7Xtia7PWJHSwUd7PfXvofXPGPzevPzlmEPI8h
wfzUKRnKDpkeLQB4QspqpHxwkUaDkVdXfxGAkXQF9OvFkZNVG3JX33Bd1l42
Wx8RFQmISfKGjNJdlZp49HAiQSHe3Mlmplxe9nCoKqw7f8zsngibKZyft33C
Ejt0QBmyF+Vztq4VWHUelkbUMs73Qtji2UI2dIUBZ6ocBirP0dVhSCVYMEUG
qdg7v2JzFGfuCL1mBZYW8tY5neDJBH+PDMg3mZ+nwBPc1ddtRu5KDE+wkdUh
DGf81K3guptMdCr0EaKSZPSyDYRqW2TPajJTPshxcW.6fy8QsRllBpiN6LUp
cmmldlqY0ag4NO87BCk+0UXtAJ8TXruhBa10mX86SI+qrvtteJm0Kcwt5.q+
5Z37pdplXylmfRccpqjj8rh0EktmilT8rTU8bUlokvSxlexTqGLq1k+CiI4Q
9A
-----------end_max5_patcher-----------
</code></pre>

Re: 'Folding' a list

Posted: Fri Jul 06, 2012 8:20 pm
by cip23
ShelLuser wrote:I don't think group is very useful here since it also requires to know the size of the list.

Reg otoh. allows you to store a list and output it as many times as you like. So all you need is something like uzi to output a specified number of bangs, like so:
Thanks. I needed to take care of the case that the new list is shorter than the old one, and also not output anything if the list is changed (the int with the desired new length should trigger the output). So group was needed, as well as reg, uzi, etc.

So it became a little bit more complicated:

Code: Select all


<pre><code>
----------begin_max5_patcher----------
1484.3ocyZszjahCD9L9WgJttSlEId58VNlp1a6wTolBaz3QIXgWPdxrIU9u
uRpEXvFH7v3wWvVBjn6u9qeHg94JK6MYuQKrQ+E5yHKqetxxR2kpCKSaK68w
usMMtP+X16oEEw6n1O.2SPeSn6GiHHWjGxGEfBQQn0HrS4CwOtO6nHkJzSA1
z6gXw1WX7cOkS2J.Q.6D7nyCnvP0UL1S+SziNnuXFyyYbQA6GT87Pj2F5Flc
w+cfByisc0HXIZwKayW+fmcsYgGuW+v1eLmEmVSRY7RAkTJn4zBJWDKXY75B
qRxjBKVc0wbA8E0X90pUpKObsvzwCjtq0XmiBiP9gWWfz85Bj9ZbzMb133li
BQFezfEwSy5BzPFwo7ZkleAnrIluqUfgzp5im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Fopow0KB1eNRUqAteKiUeT6azfzmVT7AoPKjbq1UfAI2snfCRt
Ij.c0kPXVvrnarTJzXzG730Gr2ZkJ.aufe0+WJZ0XXUSQahBfuWjVcV6T0XI
oa3gnPQSjtAlDhKTEjg7oZsTZzfMQg2WN9CNfkyDsDNZ1DYMTufYGiazxUWK
UU3YUqkRacGJuahg4vlxtCLewYP2UsVRMZH1OuoEY3cN7bKp26pX6rrkq7Nm
9XhAaKydPBpErU258NcnyrRGV9sCBV3r6tiv7LEsACGFD3iTSLq+.ub5SzPM
Oj6JuDuENSHl.KFAN0ikY6bBd2qAdhwXaTBrgjsfdI3nQjoaFqPID7O.iktU
qJjoyxSQicQ7qzjmjue4J8eJVHxYaNJfkIW+H976NtJsdLU55g2klsIN0bXh
p1BI6AczW54HuzyohY0ITvb3tz.cysD.NqWmaeMm3q9c6FTjhKsmWFxyWGxy
EB4gcqZ0wIMcnBq6BHryTjHCQjtoRDdnfD91IRd2eFN7frbQyPljM90p+Grc
AXWN
-----------end_max5_patcher-----------
</code></pre>
Do you experienced max programmers have a rule of thumb when it's time to write a javascript function? Like saying: »If there's no object that does more or less what I want out-of-the-box, write a function.«

In Python, this is done in a few lines:

Code: Select all


>>> def foldlist(l,i):
...     nl = []
...     while len(nl)<i:
...             nl.extend(l)
...     return nl[:i]
... 
>>> foldlist([2,3,4],6)
[2, 3, 4, 2, 3, 4]
It's obvious that some things are better done in code, I'm just wondering where one draws the line.

Thanks for your assistance,
best regards,
Christoph