My guide to Snap! lists

I learned how to make lists in this thread:

So I had to make this forum to help new Snap! Users use lists. :+1:
There are many topics about it, I know.

Creating a list:

Use the set variable block and add the “list [ ]” block in the input
Example:

set [list V] to (list [this is a test] @delInput@addInput)

Result:

Deleting items in a list:

If you want to delete items in a list, use this:

delete (all v) of (list)

But if you don’t want the size of the list to reset, use this instead:

for each (item) in (list) {
delete (1 v) of (list)
}

or use this if you want:

set [list V] to (numbers from (1) to (10))

for (i) = (1) to ([length V] of (list)) {
replace item (i) of (list) with []
}

Using lists:

Here’s every single list blocks:

item (1 v) of @list
<@list contains [thing]>
<is @list empty?>
([length V] of @list)
(item (1 v) of @list)
add [thing] to @list

delete (1 v) of @list

for each (item) in @list {

} @loopArrow

(map (() @addInput) over @list)

replace item (1 v) of @list with [test]

(append @list @list @delInput @verticalEllipsis @addInput)
(list [] @delInput@addInput)
(numbers from (1) to (10))
([] in front of @list)
(all but first of @list)
(keep items (<> @addInput) from @list)
insert [] at (1 v) of @list

(combinations @list @list @delInput@verticalEllipsis@addInput)
(reshape [] to (4) (3) @delInput@verticalEllipsis@addInput)

(index of [thing] in @list)
(find first item (<>@addInput) in @list)
(combine @list using (<>@addInput))

“Learning one’s lesson” usually suggests that the lesson is to not do something. I’d recommend you use something else like “I learned how to make lists in this thread.” Also, threads are single posts and their replies, topics are a thread with their first post not being on another thread, while the forum is the thing that has all those threads. Topics have headers and are highlighted on the front page.

done.

Will not keep the size of the list the same
This sort of thing will

thanks but this

for each (item) in (list) {
delete (1 v) of (list)
}

worked for me

That’s strange as it shouldn’t and doesn’t for me

Maybe he just doesnt know what a list size is

That isn’t what happens.

also, you gave this:

without the actually useful list block HOFs, like map and keep.

a list size is not a snap code :slight_smile:

I do. That’s my favorite Snap! code

I meant that this code maintains the list size while deleting every single items in the list

if you delete all the items, then the list size is 0

but then the list size is 0.
EDIT: ninja’d

oops

I believe what @niki650 means is that the variable watcher’s size doesn’t change. I find this useful fairly often, e.g., for the SHAPES list in the Vee project.

An item with a null text in it is different from deleting an item! A tutorial shouldn’t encourage people to be confused about that; they get confused about it without any help. :~) In particular, people write code like this:


and then wonder why they get error messages trying to process the first list item.

Here’s every single list blocks:

Umm no. In particular, you left out


which are the foundational blocks for recursive list functions.

Thanks for pointing out the mistake.


I find it important to note that lists are like numbers and text, you don’t exclusively need a variable to use them.

you’re still missing a ton of blocks. Drag this script into snap, and it will download you an image of all the list blocks:

what’s the (@:> ::list) block

it only works in the dev version (i think, it has an error about ADTs)

just drag it into snap

I made the block using metaprogramming, so I guess I typed in 20 instead of 1 . whoops!