monome / crow

Crow speaks and listens and remembers bits of text. A scriptable USB-CV-II machine
GNU General Public License v3.0
162 stars 34 forks source link

Sequins strings + other goodies #446

Closed trentgill closed 2 years ago

trentgill commented 2 years ago

a set of new features, shortcuts & nice-to-haves in the sequins library:

a sequins of a string is converted into a sequins of a table of characters, eg:

sequins"abc" --> sequins{'a', 'b', 'c'}

printing a sequins, now prints a representation of the execution state & data. should be very helpful in terms of debugging and being able to introspect the current status of a sequins.

s1 = sequins{1, 2, sequins{3, 4}}
s1(); s1() -- advance 2 steps
print(s1) --> s[2]{1,2,s[1]{3,4}}

the # length operator is now meaningfully supported on sequins. it will return the count of items in the top-level table (ie nested tables are treated as length-of-1:

s2 = sequins{1, 2, sequins{3, 4}}
print(#s2) -- 3

add a new :copy() method so that sequins can be duplicated. this is useful if you want to have 2 duplicate sequins (or to make permutations) but maintain their table counters separately:

s1 = sequins{1,2,3}
s2 = s1:copy()
s1() --> 1
s2() --> 1

the :settable(t) method is extended to handle nested sequins. if your sequins has the same structure (eg. element 3 is a sequins in both old & new tables) the execution state of the old table will be carried across. this means you can replace more complex sequences without restarting any nested elements. NB: does not copy flow-modifiers yet (eg. every, count etc).

additionally, the 'table' argument to settable can now be a table or a sequins (to make the syntax a little more flexible):

s1 = sequins{1,2,sequins{3,4}
for i=1,5 do s1() end -- step forward 5 steps
print(s1) --> s[2]{1,2,s[2]{3,4}}

s1:settable{5,6,sequins{7,8,9}}
print(s1) --> s[2]{5,6,s[2]{7,8,9}} -- table-length can change too!

s2 = sequins{'a','b',sequins{'c','d'}
s1:settable(s2) -- switch to another set of data, but maintain state
print(s1) --> s[2]{'a','b',s[2]{'c','d'}} -- table-length can change too!
trentgill commented 2 years ago

@tehn absolutely! i'll open a PR pulling all the new changes into norns when this all gets merged.

i'm mostly building stuff up in the PR list here so it's easier for me to track heh.

honestly more excited for this new stuff to work on norns than crow! it's really just fun lua magic :)