Hardmath123 wrote:
EDIT: Maybe I'll write a JS library to do stuff like that:
Code:
ITER([1,2,3,4,5,6,7,8,9,10], function(num) { alert(num*3); });Shouldn't be too hard.
It's called [].forEach.
Jens wrote:
Okay, here is the thing: When setting a variable, if the value can be interpreted as a number (e.g. if it is a string beginning with a number), Snap will cast it to one. Numbers take precedence over strings. In your example you can prevent this from happening by adding an alphabetical character at the beginning of the word.
That's not the correct behavior. Operations that expect a number should cast their operands to numbers right before they use them (using the expression `+x || 0`—not parseInt(x), which does weird things with stuff like '0x123' or '123abc'). Variables can hold any string; the SET block takes a string as its second argument and should not convert that argument to a number.
Offline
Jens wrote:
Okay, here is the thing: When setting a variable, if the value can be interpreted as a number (e.g. if it is a string beginning with a number), Snap will cast it to one. Numbers take precedence over strings. In your example you can prevent this from happening by adding an alphabetical character at the beginning of the word.
That's what happens in scratch, too. I've found a very useful purpose for it in my scheme interpreter i'm writing in scratch.
Offline
joefarebrother wrote:
Jens wrote:
Okay, here is the thing: When setting a variable, if the value can be interpreted as a number (e.g. if it is a string beginning with a number), Snap will cast it to one. Numbers take precedence over strings. In your example you can prevent this from happening by adding an alphabetical character at the beginning of the word.
That's what happens in scratch, too. I've found a very useful purpose for it in my scheme interpreter i'm writing in scratch.
Scratch casts strings to numbers with asNumberNoError (which is exactly equivalent to `+x || 0` in JavaScript) only when it requires a number, e.g., in the implementation of <() + ()>. It never casts strings to numbers when they're used as inputs to a block which takes a string, e.g., in the implementation of <set () to ()>. Try this simple script:
<script variables (var)>
<set (var) to (123abc)>
<say <var>>
In Scratch, the sprite says "123abc". In Snap!, the sprite says "123". Scratch is right. Snap! is wrong.
Last edited by nXIII (2013-01-20 16:09:09)
Offline
joefarebrother wrote:
Jens wrote:
Okay, here is the thing: When setting a variable, if the value can be interpreted as a number (e.g. if it is a string beginning with a number), Snap will cast it to one. Numbers take precedence over strings. In your example you can prevent this from happening by adding an alphabetical character at the beginning of the word.
That's what happens in scratch, too. I've found a very useful purpose for it in my scheme interpreter i'm writing in scratch.
In Scratch? Wow. Tell me when you upload it!
Offline
I changed the SET type coercion. My test cases all work fine with it, but there is a really cool CS10 student project which doesn't work right anymore. Therefore I'd appreciate some more testing, if anyone has got time at their hands. Do your experimental Snap! projects still work as they used to / as you'd expect them to?
Thanks!
Offline
Well, my project works like a charm now!
Can you post the project that's not working?
Also:
Anti-aliasing problem?
Last edited by Hardmath123 (2013-01-21 05:38:58)
Offline
this student Chess project used to work, now it doesn't anymore. It "hangs" in the stage's "When I receive movement" script (there are some other glitches in there, e.g. you have to hit the green flag twice before you can move the pieces correctly, but overal it was working).
Offline
There should be a way to specify that you should do something when a user-created data structure is garbage collected.
Offline
joefarebrother wrote:
There should be a way to specify that you should do something when a user-created data structure is garbage collected.
Why? Do you have an application in mind? I'd just as soon users not be thinking about garbage collection; everything lasts forever and there's infinite storage.
Offline
Jens wrote:
Which browser and OS do you notice this anti-aliasing behavior in? Is this something new (the rendering hasn't been changed for ages)
Safari 6.0.2 on Mac Mountain Lion.
Offline
Hmmm. I'm using the same configuration and it doesn't produce any problems. When did you last reboot?
(Heheh, this used to be the antidote to whatever Windows was doing wrong. Turns out rebooting often is a remedy for strange Mac behavior, too).
Offline
I don't know. Anyway, it's gone now.
Third time I'm bumping my JSON idea. This time, I've got a reason you've got to be interested in. I'm writing a raytracer in Python, and yesterday night I had a flash of inspiration and implemented triangle primitives (I'm trying to derive all the math I use so that I actually learn something). I wanted a cool little demo, So I thought I'd make a 3D "S" in Scratch-style font. I basically aimed to do what I did here; except raytraced.
I wanted to write myself a simple little GUI to do it (inputting a large number of 3D coordinates manually is painful!), so I tried to use Snap!. The problem was that I didn't have a good way to get the coordinate data from Snap! lists.
If we had JSON support, I would simply dump the list into a JSON string and open that as a datauri in a new tab (ok, you also need an "open [] in new window" block). But since we don't I tried to write a ist-to-JSON block within Snap!. And crashed Safari.
Convincing? How about just an "export []" block which turns the input whatever type it is, into JSON and opens it in a new tab?
Offline
Hi Hardmath123,
I've been playing with interfacing from Snap! to various other applications and hardware extension lately (3d printing being one them), so I'm probably going to add a reporter similar to the monadic one we have for numbers (sqrt, sin, cos...) for text also. Possible useful primitive functions would be
* sha512 hash
* encode URI component
* decode URI component
* escape XML
* un-escape XML
But I wouldn't do JSON, because this would have to EVAL a string first, which would be problematic, because people could wreck their Snap sessions with it.
So, we need a way to import data into Snap, similar to importing the contents of a text file into a Scratch (1.4) list. Right now you can do this - somewhat awkwardly - by setting up a simple CORS web server and by using Snap's HTTP block to fetch a document, which you can then parse. But I'm also thinking about just letting you drag and drop text files onto Snap, and letting you choose which variable you'd like to assign their contents to.
Hang on, first we need to get 4.0 out, though!
Offline
Yeah, I totally understand that you have other priorities. And I suppose I'll have to survive with a makeshift CORS server for now...
Also, eval isn't the best way to parse JSON. Use JSON.parse and JSON.stringify.
Offline
What's CORS?
Offline
@Jens
In the good old Byob3 days we had a feature, where by right-clicking on a list display window we had the two commands : Export, Import allowing to create and read text files. I've used this to export/import lists of coordinates !
Offline
joefarebrother wrote:
What's CORS?
Hardmath123 wrote:
Convincing? How about just an "export []" block which turns the input whatever type it is, into JSON and opens it in a new tab?
![]()
You can write your own pretty trivially, and just copy the result into a file somewhere.
EDIT: Oh wait…Morphic doesn't support copy-paste. You can use that bookmarklet thingy I made a while ago.
EDIT2: @bharvey your ring has quite a following
Last edited by nXIII (2013-01-22 20:31:38)
Offline
nXIII wrote:
EDIT: Oh wait…Morphic doesn't support copy-paste.
Exactly. There's no way to export or import a large amount of data. I guess I'll have to make that server after all...
Offline
Hardmath123 wrote:
nXIII wrote:
EDIT: Oh wait…Morphic doesn't support copy-paste.
Exactly. There's no way to export or import a large amount of data. I guess I'll have to make that server after all...
![]()
javascript:var d=document,s=d.createElement('script');s.src='http://dl.dropbox.com/u/10715865/Web/Snap-Old/inject/native-editors.js';d.querySelector('head').appendChild(s)
It's a little buggy/outdated, but it should do fine if you need to copy-paste. (Run it on startup so that all new textbox morphs are native, or even better include it in a local copy to catch the ones created at startup)
Last edited by nXIII (2013-01-22 21:14:40)
Offline
xly wrote:
@Jens
In the good old Byob3 days we had a feature, where by right-clicking on a list display window we had the two commands : Export, Import allowing to create and read text files. I've used this to export/import lists of coordinates !
Yes, this is clearly The Right Thing. Only that in Snap we're using generic variable watchers for lists, so we want to import plain text files directly into a variable's value, and do the parsing ourselves.
Check out today's version: I've added "import..." and "export..." entries in variable watchers' context menus. This lets you get text data in and out of Snap! in a hopefully simple way.
Offline
Jens wrote:
xly wrote:
@Jens
In the good old Byob3 days we had a feature, where by right-clicking on a list display window we had the two commands : Export, Import allowing to create and read text files. I've used this to export/import lists of coordinates !Yes, this is clearly The Right Thing. Only that in Snap we're using generic variable watchers for lists, so we want to import plain text files directly into a variable's value, and do the parsing ourselves.
Check out today's version: I've added "import..." and "export..." entries in variable watchers' context menus. This lets you get text data in and out of Snap! in a hopefully simple way.
With Firefox I get only the Import feature
With Chrome, nothing, neither Import nor Export.
I'm not qualified to give you any advice, but, if the Imported file is a text file, each of its line could be an item of a named list (as for Byob) Then up to the user to parse each item of this list accorgingly. I've done this for coordinates where + and - were converted into and m and numeric coordinates into strings.
Offline