How do I implement a simple JavaScript function in Snap!

I added one.

… in which case both HELP-texts need to be revised: they present examples implying evaluation from the head onwards. The same applies to FOR EACH (btw: an HOP, indeed).

Solution with streams
Utilizing the stream mechanism is always elegant, I agree. The fact that your proposed solution is almost correct, and thus must have been written without testing, only proves your genius :wink: .

Below is a correct version, embedded in a test script:

Recursion will work fine as well, I’m sure.

Faster version?
I’m (also) still open for advice as for how to construct a really fast version, perhaps with JavaScript :smirk: .

"warp" is your friend :wink:

untitled script pic - 2023-04-20T032132.231

untitled script pic - 2023-04-20T032135.343

No, you're misunderstanding what I said. We do promise that the order of items in the result matches the order of items in the input. We just don't promise which item is computed first. It's a question of time, not position.

FOR EACH is different. It does promise to process the list left to right. The idea is that in functional programming you don't care when things happen, because no part of the computation depends on any other part of the computation. FOR EACH isn't functional; it's sequential.

Oh, no, what it proves is that when you said "interval" I read that as, e.g., "between 5 and 8," represented as the list {5.8}.

I know this keeps coming up a lot and folks hate me for discouraging JS discussions, but hear me out: This forum is stuffed to the brim with (males) people who love Snap so much they want to hack it in the "underlying" programming language in search for ground-level truth, believing that learning how to hack Snap will make them competent JS programmers.

I'm not male, I want to get into the weeds with snap because as far as I'm concerned, there is a widening gulf between "human readable" and "human understandable" and as you point out, coders who are usually men adapt to the rules of programming languages, whereas women try to ask why they can't just DO something and I think there is a middle ground.

My issue is that I can read snap code easily, but when I hit "new project" the interpreter sits empty until I close it because "I can do anything... sure where do I start... uhhh..."

I also think that locking everything into browsers is a mistake, but it does make sense for a teaching language to have those locks in place.

I would like to eventually extend snap until it breaks so I can then rewrite it into my own language that does what I want. Because I don't think like a programmer and because of that, I can't really do anything advanced because increasingly tech is becoming "Don't worry about that, someone somewhere else knows the answer" and I really, really, really, REALLY don't like it.

related but unrelated, is there any news on the snap3 course on sap? Or have I missed it because I don't know where to look?

BYOB was a separate program you installed on your computer. That was great for independent learners but not great for teachers, because their school's IT czar wouldn't let them install software that didn't come from Microsoft or Adobe. So for Snap! we went browser-based, but now the IT people have learned to whitelist only sites that come from Google or, these days, Scratch. You can't win.

If that's the problem you're trying to solve, I don't see how moving to a language you can't even read will help!

Here are some ways to get project ideas:

  • Look at other people's projects on the community site.

  • In our Beauty and Joy of Computing curriculum (bjc.edc.org) each unit has an "optional projects" section with ideas you can browse.

  • Build a non-video game, such as Wordle or Sudoku or Mastermind. I suggest those rather than video games because you don't get caught up in details of timing or multiprocessing, but can focus on the logic.

  • Make an animation. It doesn't have to be very complicated; one of my favorites is a ferris wheel.

Does that help?

BYOB was a separate program you installed on your computer

Yeah, I've been following snap for a long time. I'm aware of that stuff and even agree on that logic.
What I'm after is a downloadable and native snap, now keeping in mind, I'm also aware that my goals and yours absolutely diverge, so I would happily do it myself ... In Theory.

Now as to not understanding, I suppose I can read code just fine. I'm a little (Lot) self deprecating, the issue lies in that I'm a visual person and snap opens up a window that I've been wanting open for a while, whereas text, for me is an encyclopedia entry about how the sausage is made, but does not show me how the sausage is made.
Whereas I click the code in snap and the turtle updates, and if I've got stepping mode on, it shows me what it's doing and I have a visual tool that can and shows me what it's doing and what it's referencing? Yeah. That'd get past my stubborn brain. All the way through. Fast.

I can't however drag and drop r_draw.c into snap because snap does not understand it. Whereas if I ever got past my brain block, that's what I'd do, but not just c or cpp, but any language type, cause the language doesn't matter. What the machine does is the key.

Also from a modern computing standpoint, drag and dropping blocks in an AR environment with my hands makes far, far, far, FAR more sense than insisting a keyboard/mouse is the pinnacle of tech. Don't get me wrong, I'm using an expensive logitech keyboard/mouse set and they are very nice, but beyond RGB lighting, what's the difference between this and an old 1985 Model M IBM keyboard... and the answer is honestly? Very little. Same with the mouse, it's wireless at an insane dpi sensitivity, but really? Aside from the RGB lighting and button count? Same thing.

Tech's been in a "the same thing but SHINY" loop for as long as I remember, and it's achieved a lot, but a lot less than it should have in my long winded opinion.

Two things about that.

First, it's an unreasonable expectation. You can't import a .c file into a Python program or a Javascript program either; each programming language is different from the others.

Second, drawing in C isn't any more "how the sausage is made" than drawing in Snap!. People keep wanting to see "the real code" in a Snap! project, as if Snap! were a GUI layered on top of a "real" language, but it's not. We're just as "real" as any other programming language! When you use a drawing library in C you're sitting on top of an abstraction, same as in Snap!. Abstraction is what makes it possible to write the program you want to write, instead of (for example) having to draw a line pixel by pixel.

If you insist on a pixel-by-pixel view, the place to look is in your browser's implementation of HTML Canvas.

I have strong feelings about keyboards, namely, the only good one is the original Microsoft Natural Ergonomic Keyboard 4000, the only good thing Microsoft ever did, so naturally they don't make them any more (but you can still buy one with a little research online). They split the key layout so that each hand can be at its natural angle, but the thing that makes this keyboard unique in the world, as far as I've been able to tell, is that they let you raise the front of the keyboard, so your wrist can bend down a little, which is what it wants to do, instead of having to bend backwards as with any other keyboard. The other keyboards raise the back of the keyboard (which this one also allows), which is just stupid.

I know that the entire world can't really be stupid in the same way, so they must have some idea in mind that I'm missing. All I know is that my wrists love this keyboard, and I won't use any other.

</rant>

@dardoro: thanks for the DURATION block, I think it's really easy to use when measuring and comparing execution speed !

The proposed faster solution for CUMULATIVE function will only work when MAP is evaluated in one direction (from ITEM (1) up), see post #4. Thanks for the hint, anyway.

You're right, of course. It's a subtle yet important difference. I hadn't thought of it that way yet.

cumulative script pic (4)

cumulative script pic (1)

cumulative script pic (2)

cumulative script pic (5)

Second, drawing in C isn't any more "how the sausage is made" than drawing in Snap*!*

R_Draw.c is for example, how Quake draws between each refresh. I'm not after the "real code" of snap! at all, I just want to open a .c file in a block editor. Something along the lines of notepad++ and/or programmers notepad or pick editor of choice using the language of choice.

Once I've I've loaded quake into snap and then snap into quake, then I'll try something else, probably something like a demotool like werkkzeug, which uses C++. It's about seeing the code work instead of guessing how the code works, because no matter which text language I look at. It flies all the way over head.

I have no strong inclination between c, c++, python or snap! but I'd much rather use blocks regardless of language. Snap! is not my end goal, but it is the tool I like the most that I want to use to build towards my own end goal. Am I trying too much with the wrong intent? The probability is very close to 1, but I want to try anyway, because if I'm wrong or even in the unlikely circumstance that I'm right, I learned it my way. Stubborn? YUP. That's meeeeee :slight_smile:

I have the same issue with writing. I can write the story fine, but then I ask myself what billboard is 50m to the left of the character as she crosses the street and I then sit in my chair picturing the entire scene instead of writing "The character crossed the street when the light went green"

I’m afraid @dardoro ’s JavaScript implementation is unbeatable.
However I did find a non-JS algorithm that outperforms any other (so far) in Snap ! :

The algorithm takes the original list and adds the same list but with "0" inserted at item 1:

x1 x2 x3 x4 x5 x6 x7 x8 ...
(0) x1 x2 x3 x4 x5 x6 x7 x8 ...
------------------------------------ +
y1 y2 y3 y4 y5 y6 y7 y8 y9 ...

Then it takes this new list and adds the same list but with 2 "0"-s inserted at item 1.

y1 y2 y3 y4 y5 y6 y7 y8 y9 ...
(0)(0) y1 y2 y3 y4 y5 y6 y7 y8 y9 …
---------------------------------------- +
z1 z2 z3 z4 z5 z6 z7 z8 z9 z10 ...

and this is equivalent to:

x1 x2 x3 x4 x5 x6 x7 x8 ...
(0) x1 x2 x3 x4 x5 x6 x7 x8 ...
(0)(0) x1 x2 x3 x4 x5 x6 x7 x8 ...
(0)(0)(0) x1 x2 x3 x4 x5 x6 x7 x8 ...
---------------------------------------- +
z1 z2 z3 z4 z5 z6 z7 z8 z9 z10 ...

Next it takes the newest list and adds the same list but with 4 "0"-s inserted at item 1… and so on until 2^n versions of the original list have been added, each shifted to the right by one more position. 2^n must be at least as large as the length of the original list, and the sum is finished.

That gave me a smile. But you can put that characteristic to work; Real Writers become known as Real Writers because they include that sort of detail, and make it relevant by describing the character's reaction to the billboard. Go read the first several pages of any chapter of Tolkien.

By all means, do it your way! It's my job to suggest alternatives, but it's not my job to hit you over the head with them.

But you're not going to get the ability to drag C code into Snap! blocks. That would be a huge distraction from our own purposes; I think maybe you underestimate how hard it'd be to implement. We can do it with Javascript because that's what the browser provides; in that sense it really is how the sausage is made.

Look into Blockly. They see their job as more like what you want, being a GUI around some "real" language. Maybe someone has used it with C.

But know that Snap! is a "real" language. That's why we're not Scratch, whose goal is to be a simple language. Because of hyperblocks, we're a more expressive language than most of those grownup ones.

That gave me a smile. But you can put that characteristic to work; Real Writers become known as Real Writers because they include that sort of detail, and make it relevant by describing the character's reaction to the billboard. Go read the first several pages of any chapter of Tolkien.

Yup. World building is very much a thing, but translating the world in my brain to the world on the screen is the problem. Always has been. Whether it's writing, drawing, programming. I get stuck because the canvas I want is not the canvas available.

But you're not going to get the ability to drag C code into Snap*!* blocks. That would be a huge distraction from our own purposes; I think maybe you underestimate how hard it'd be to implement. We can do it with Javascript because that's what the browser provides; in that sense it really is how the sausage is made.

Sure, but that's my point. I know that. I've been saying that what I want diverges from what you want, which is why I want to do it myself... somehow. If I ever get past my defective brain I'll do it.

Snap is absolutely a real language, you have no disagreement from me on that statement. Absolutely none. It's incredible work :slight_smile:

Thank you!

how do you get those blocks :thinking:

hi @mauro.sanmiguel573 , welcome to snap! I'm assuming you mean the javascript extensions, which is accessed by pressing the settings button at the top, and enabling javascript extensions. Then you should find it in the "Operators" section.

no the duration and the cumulative
blocks

dardoro made those blocks. To use them easily, drag the pictures to your web browser's tab selector, and drag them to the tab with your editor.