This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#6601 2013-01-18 11:39:52

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: BYOB 3 - Discussion Thread

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.


nXIII

Offline

 

#6602 2013-01-20 15:29:23

joefarebrother
Scratcher
Registered: 2011-04-08
Posts: 1000+

Re: BYOB 3 - Discussion Thread

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.


My latest project is called http://tinyurl.com/d2m8hne! It has http://tinyurl.com/d395ygk views, http://tinyurl.com/cnasmt7 love-its, and http://tinyurl.com/bwjy8xs comments.
http://tinyurl.com/756anbk   http://tinyurl.com/iplaychess

Offline

 

#6603 2013-01-20 15:45:27

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: BYOB 3 - Discussion Thread

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)


nXIII

Offline

 

#6604 2013-01-20 20:17:16

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: BYOB 3 - Discussion Thread

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!


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#6605 2013-01-21 04:34:34

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: BYOB 3 - Discussion Thread

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!


Jens Mönig

Offline

 

#6606 2013-01-21 05:38:36

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: BYOB 3 - Discussion Thread

Well, my project works like a charm now!  big_smile

Can you post the project that's not working?

Also:
http://i45.tinypic.com/r0bebp.png
Anti-aliasing problem?  hmm

Last edited by Hardmath123 (2013-01-21 05:38:58)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#6607 2013-01-21 06:22:24

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: BYOB 3 - Discussion Thread

Which browser and OS do you notice this anti-aliasing behavior in? Is this something new (the rendering hasn't been changed for ages)


Jens Mönig

Offline

 

#6608 2013-01-21 11:13:56

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: BYOB 3 - Discussion Thread

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).


Jens Mönig

Offline

 

#6609 2013-01-21 12:09:44

joefarebrother
Scratcher
Registered: 2011-04-08
Posts: 1000+

Re: BYOB 3 - Discussion Thread

There should be a way to specify that you should do something when a user-created data structure is garbage collected.


My latest project is called http://tinyurl.com/d2m8hne! It has http://tinyurl.com/d395ygk views, http://tinyurl.com/cnasmt7 love-its, and http://tinyurl.com/bwjy8xs comments.
http://tinyurl.com/756anbk   http://tinyurl.com/iplaychess

Offline

 

#6610 2013-01-21 14:14:29

bharvey
Scratcher
Registered: 2008-08-10
Posts: 1000+

Re: BYOB 3 - Discussion Thread

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.


http://cs.berkeley.edu/~bh/sig5.png

Offline

 

#6611 2013-01-21 15:53:57

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: BYOB 3 - Discussion Thread

joefarebrother wrote:

There should be a way to specify that you should do something when a user-created data structure is garbage collected.

Implement your own garbage collection.  wink


nXIII

Offline

 

#6612 2013-01-21 20:05:47

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: BYOB 3 - Discussion Thread

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.


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#6613 2013-01-22 01:50:00

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: BYOB 3 - Discussion Thread

Hmmm. I'm using the same configuration and it doesn't produce any problems. When did you last reboot?  smile

(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).


Jens Mönig

Offline

 

#6614 2013-01-22 03:17:27

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: BYOB 3 - Discussion Thread

I just updated Snap! with a few fixes, so now that broken student Chess projects works correctly again. But keep on testing nonetheless!  smile


Jens Mönig

Offline

 

#6615 2013-01-22 05:39:03

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: BYOB 3 - Discussion Thread

I don't know. Anyway, it's gone now.  hmm

Third time I'm bumping my JSON idea. This time, I've got a reason you've got to be interested in.  smile  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.  big_smile

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.  sad

Convincing? How about just an "export []" block which turns the input whatever type it is, into JSON and opens it in a new tab?  smile


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#6616 2013-01-22 06:10:04

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: BYOB 3 - Discussion Thread

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!


Jens Mönig

Offline

 

#6617 2013-01-22 09:04:58

technoboy10
Scratcher
Registered: 2007-08-25
Posts: 1000+

Re: BYOB 3 - Discussion Thread

@Jens Whoa, 3D printing? How'd you do that?


So long, 1.4.
http://goo.gl/3JEV9

Offline

 

#6618 2013-01-22 09:41:31

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: BYOB 3 - Discussion Thread

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.stringifysmile


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#6619 2013-01-22 15:39:04

joefarebrother
Scratcher
Registered: 2011-04-08
Posts: 1000+

Re: BYOB 3 - Discussion Thread

What's CORS?


My latest project is called http://tinyurl.com/d2m8hne! It has http://tinyurl.com/d395ygk views, http://tinyurl.com/cnasmt7 love-its, and http://tinyurl.com/bwjy8xs comments.
http://tinyurl.com/756anbk   http://tinyurl.com/iplaychess

Offline

 

#6620 2013-01-22 18:49:46

xly
Scratcher
Registered: 2010-04-17
Posts: 100+

Re: BYOB 3 - Discussion Thread

@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

 

#6621 2013-01-22 20:20:01

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: BYOB 3 - Discussion Thread

joefarebrother wrote:

What's CORS?

Cross-origin resource sharing

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?  smile

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  tongue

Last edited by nXIII (2013-01-22 20:31:38)


nXIII

Offline

 

#6622 2013-01-22 20:55:49

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: BYOB 3 - Discussion Thread

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...  sad


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#6623 2013-01-22 21:13:03

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: BYOB 3 - Discussion Thread

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...  sad

Code:

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)

big_smile

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)


nXIII

Offline

 

#6624 2013-01-23 04:30:16

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: BYOB 3 - Discussion Thread

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.


Jens Mönig

Offline

 

#6625 2013-01-23 05:33:15

xly
Scratcher
Registered: 2010-04-17
Posts: 100+

Re: BYOB 3 - Discussion Thread

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

 

Board footer