Re: javascript (was Re: Java > Scala -> new thread: GUI for D)

2011-12-05 Thread Jacob Carlborg

On 2011-12-05 16:53, Adam Ruppe wrote:

Marco Leise Wrote:

This is really one of the largest shortcomings of the language that can
not be explained with a simple design choice.


Aye. One of the newer versions adds a forEach member to the
array prototype, that works like this:

[1, 2, 3].forEach(function(element) { use element here; });

but I don't like that style, personally. All those years of C
and friends have wired my brain to see it as being backward.

And, it doesn't work for everyone out of the box anyway.


That's the Ruby style, quite verbose in plain JavaScript. In 
CoffeeScript it would look like:


[1, 2, 4].forEach (element) -> # use element here

--
/Jacob Carlborg


Re: javascript (was Re: Java > Scala -> new thread: GUI for D)

2011-12-05 Thread Jacob Carlborg

On 2011-12-05 16:47, Adam Ruppe wrote:

Jacob Carlborg Wrote:

for e in arr
  # do something with the element "e"


Heh, I used to think that would work in regular Javascript,
since it does have a for(blah in something) form...

But in regular javascript, that only works on objects!


Yeah, it's very annoying. CoffeeScript also has the following iteration 
syntax:


for key, value of obj
# do something with key and value

Which compiles to:

for (k in obj) {
  v = obj[k];
}

--
/Jacob Carlborg


Re: javascript (was Re: Java > Scala -> new thread: GUI for D)

2011-12-05 Thread Adam Ruppe
Marco Leise Wrote:
> This is really one of the largest shortcomings of the language that can  
> not be explained with a simple design choice.

Aye. One of the newer versions adds a forEach member to the
array prototype, that works like this:

[1, 2, 3].forEach(function(element) { use element here; });

but I don't like that style, personally. All those years of C
and friends have wired my brain to see it as being backward.

And, it doesn't work for everyone out of the box anyway.


Re: javascript (was Re: Java > Scala -> new thread: GUI for D)

2011-12-05 Thread Adam Ruppe
Jacob Carlborg Wrote:
> for e in arr
>  # do something with the element "e"

Heh, I used to think that would work in regular Javascript,
since it does have a for(blah in something) form...

But in regular javascript, that only works on objects!


Re: javascript (was Re: Java > Scala -> new thread: GUI for D)

2011-12-05 Thread Jacob Carlborg

On 2011-12-05 10:10, Marco Leise wrote:

Am 04.12.2011, 21:17 Uhr, schrieb Adam D. Ruppe
:


Jacob Carlborg Wrote:
If you like the idea there, but want something a lot more conservative,
in my html.d (in here:
https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff
)
there's now a JavascriptMacroExpander class which pre-processes
javascript.

The only build in function it provides right now is foreach() (just
because I find
it's lack to be the easiest thing to take care of...) but the macro
system might
be useful too for certain things.

Again, it's just a preprocessor, so you drop stuff like this into the
middle of
regular js code:

¤foreach(element; document.querySelectorAll("p")) {
element.style.color = "red";
}

converts to

var arsd_foreach_loop_temporary_2 = document.querySelectorAll("p");
for(var arsd_foreach_loop_counter_1 = 0; arsd_foreach_loop_counter_1 <
arsd_foreach_loop_temporary_2.length; arsd_foreach_loop_counter_1++) {
var element = arsd_foreach_loop_temporary_2[arsd_foreach_loop_counter_1];
element.style.color = "red";
}


There is also this macro in the JavaScript plugin for Eclipse:

for (var ${index} = 0; ${index} < ${array}.length; ${index}++) {
var ${array_element} = ${array}[${index}];
${cursor}
}

This is really one of the largest shortcomings of the language that can
not be explained with a simple design choice.


CoffeeScript makes it a correct for-each loop:

for e in arr
# do something with the element "e"

--
/Jacob Carlborg


Re: javascript (was Re: Java > Scala -> new thread: GUI for D)

2011-12-05 Thread Marco Leise
Am 04.12.2011, 21:17 Uhr, schrieb Adam D. Ruppe  
:



Jacob Carlborg Wrote:
If you like the idea there, but want something a lot more conservative,
in my html.d (in here:  
https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff  
)
there's now a JavascriptMacroExpander class which pre-processes  
javascript.


The only build in function it provides right now is foreach() (just  
because I find
it's lack to be the easiest thing to take care of...) but the macro  
system might

be useful too for certain things.

Again, it's just a preprocessor, so you drop stuff like this into the  
middle of

regular js code:

¤foreach(element; document.querySelectorAll("p")) {
 element.style.color = "red";
}

converts to

var arsd_foreach_loop_temporary_2 = document.querySelectorAll("p");
for(var arsd_foreach_loop_counter_1 = 0; arsd_foreach_loop_counter_1  
< arsd_foreach_loop_temporary_2.length; arsd_foreach_loop_counter_1++) {
 var element =  
arsd_foreach_loop_temporary_2[arsd_foreach_loop_counter_1];

 element.style.color = "red";
}


There is also this macro in the JavaScript plugin for Eclipse:

for (var ${index} = 0; ${index} < ${array}.length; ${index}++) {
var ${array_element} = ${array}[${index}];
${cursor}
}

This is really one of the largest shortcomings of the language that can  
not be explained with a simple design choice.


Re: javascript (was Re: Java > Scala -> new thread: GUI for D)

2011-12-05 Thread bearophile
Jacob Carlborg:

> I think they're good languages, regardless of the indent-syntax or not. 
> CoffeeScript and Ruby share a couple of language features that I'm not 
> sure if Python does:
> 
> * Instance variables start with @ (shortcut for "this." in CS)
> 
> * Functions can be called without parentheses (they have to take at 
> least one argument in CS)

In Python instance variables start with "self.". And function call requires a 
(). There is a way to define properties that don't need the () but they can't 
take arguments.

Bye,
bearophile


Re: javascript (was Re: Java > Scala -> new thread: GUI for D)

2011-12-05 Thread Jacob Carlborg

On 2011-12-05 07:59, Nick Sabalausky wrote:

"Jacob Carlborg"  wrote in message
news:jbglgs$2no2$1...@digitalmars.com...


I think CoffeeScript works really well, it's been around a while and it's
the default way to handle JavaScript in Rails 3.1 and later versions (SASS
is the default way of handling CSS).


That seems slightly strange to me since Ruby is not an indent-syntax
langauge. I would have expected something like Django to be more likely to
standardize on on SASS/CoffeeScript.


I think they're good languages, regardless of the indent-syntax or not. 
CoffeeScript and Ruby share a couple of language features that I'm not 
sure if Python does:


* Instance variables start with @ (shortcut for "this." in CS)

* Functions can be called without parentheses (they have to take at 
least one argument in CS)


* Braces around hashes are optional
* No need to declare variables
* Existential operator
* Trailing if-statements

On the other hand, CoffeeScript and Python shares a couple of features 
as well:


* Indentations are used for scopes
* Array comprehension

I'm not very familiar with Python.

BTW, SASS comes with two syntaxes, SCSS and SASS. SCSS is a superset of 
CSS and uses braces for scope and semicolons, just as CSS. SASS uses 
indentation and no semicolons. SCSS is the default in Rails 3.1.


--
/Jacob Carlborg


Re: javascript (was Re: Java > Scala -> new thread: GUI for D)

2011-12-04 Thread Jacob Carlborg

On 2011-12-04 21:40, Adam D. Ruppe wrote:

Jacob Carlborg Wrote:

Maybe you should take a look at SASS, it has if-statements, for-loops,



Yea, I've looked at it before (and like some of the ideas - their lighten, 
darken,
etc. functions are nice and I intend to implement them myself as I find the
time - see color.d in that github page.)

But it went a little too far away from the css base for me, and I wanted
something I could more easily customize and integrate with D anyway,
so I did my own.


If you use the SCSS syntax then you can basically choose what SASS 
specific features you want. For example, use regular CSS + variables. Or 
regular CSS + mixins. I mean, you're not forced to use any feature. But 
I can understand you want something integrated with D.



Rails is what I'm using for web development.


Indeed. You've linked me to some stuff before, and I like some of it,
so I'm stealing a few of their ideas :)


Yeah, I think it's a good framework and there's plenty of stuff to steal 
from it. I have looked at other frameworks, in other languages, PHP and 
Python for example. But these frameworks just look like bad 
implementations of Rails. They're not as good as Rails, or have syntax 
that look as good as Rails', usually because the languages are lacking 
in some way (or several ways).



That message was more aimed toward Nick or any other lurker who,
like me, might find the Rails end to go a bit too far away from our comfort
zone.


Hehe, I see.

--
/Jacob Carlborg


Re: javascript (was Re: Java > Scala -> new thread: GUI for D)

2011-12-04 Thread Nick Sabalausky
"Jacob Carlborg"  wrote in message 
news:jbglgs$2no2$1...@digitalmars.com...
>
> I think CoffeeScript works really well, it's been around a while and it's 
> the default way to handle JavaScript in Rails 3.1 and later versions (SASS 
> is the default way of handling CSS).

That seems slightly strange to me since Ruby is not an indent-syntax 
langauge. I would have expected something like Django to be more likely to 
standardize on on SASS/CoffeeScript. 




Re: javascript (was Re: Java > Scala -> new thread: GUI for D)

2011-12-04 Thread Adam D. Ruppe
Jacob Carlborg Wrote:
> Maybe you should take a look at SASS, it has if-statements, for-loops, 


Yea, I've looked at it before (and like some of the ideas - their lighten, 
darken,
etc. functions are nice and I intend to implement them myself as I find the
time - see color.d in that github page.)

But it went a little too far away from the css base for me, and I wanted
something I could more easily customize and integrate with D anyway,
so I did my own.


> Rails is what I'm using for web development.

Indeed. You've linked me to some stuff before, and I like some of it,
so I'm stealing a few of their ideas :)

That message was more aimed toward Nick or any other lurker who,
like me, might find the Rails end to go a bit too far away from our comfort
zone.


Re: javascript (was Re: Java > Scala -> new thread: GUI for D)

2011-12-04 Thread Jacob Carlborg

On 2011-12-04 21:17, Adam D. Ruppe wrote:

Jacob Carlborg Wrote:

I hide JavaScript behind CoffeeScript, makes it a bit more usable.


If you like the idea there, but want something a lot more conservative,
in my html.d (in here: 
https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff
 )
there's now a JavascriptMacroExpander class which pre-processes javascript.

The only build in function it provides right now is foreach() (just because I 
find
it's lack to be the easiest thing to take care of...) but the macro system might
be useful too for certain things.

Again, it's just a preprocessor, so you drop stuff like this into the middle of
regular js code:

¤foreach(element; document.querySelectorAll("p")) {
  element.style.color = "red";
}

converts to

 var arsd_foreach_loop_temporary_2 = document.querySelectorAll("p");
 for(var arsd_foreach_loop_counter_1 = 0; arsd_foreach_loop_counter_1<  
arsd_foreach_loop_temporary_2.length; arsd_foreach_loop_counter_1++) {
  var element = 
arsd_foreach_loop_temporary_2[arsd_foreach_loop_counter_1];
  element.style.color = "red";
 }


which I realize is an ugly mess but just because I used long names for the 
temporaries.
It: a) puts the iterable in it's own var, b) does a for loop over it, c) sets 
up a local for
the iterated thing inside the loop and d) pastes your code in there.


(that symbol before foreach is the one I did to denote a macro. I put it
on a hotkey in my editor... but I wanted something that I'd never use in
normal code, to keep the preprocessor both dead simple and out of the
way.)


You can also define your own macros and variables in the JS code
or as delegates in D.


I actually wrote this macro system for css, but it works pretty well
here too. (html.d also includes a CssMacroExpander, which runs this
and a de-nesting function.)


Maybe you should take a look at SASS, it has if-statements, for-loops, 
functions, mixins, variables and other useful features. Two syntaxes are 
available, one which is a superset of CSS (SCSS) and one which doesn't 
use semicolon and uses indentation for scopes (SASS).


http://sass-lang.com/


This is pretty new stuff, so I'll probably be adding more functions
as I use it more.


I think CoffeeScript works really well, it's been around a while and 
it's the default way to handle JavaScript in Rails 3.1 and later 
versions (SASS is the default way of handling CSS). Rails is what I'm 
using for web development.


--
/Jacob Carlborg


javascript (was Re: Java > Scala -> new thread: GUI for D)

2011-12-04 Thread Adam D. Ruppe
Jacob Carlborg Wrote:
> I hide JavaScript behind CoffeeScript, makes it a bit more usable.

If you like the idea there, but want something a lot more conservative,
in my html.d (in here: 
https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff
 )
there's now a JavascriptMacroExpander class which pre-processes javascript.

The only build in function it provides right now is foreach() (just because I 
find
it's lack to be the easiest thing to take care of...) but the macro system might
be useful too for certain things.

Again, it's just a preprocessor, so you drop stuff like this into the middle of
regular js code:

¤foreach(element; document.querySelectorAll("p")) {
 element.style.color = "red";
}

converts to

var arsd_foreach_loop_temporary_2 = document.querySelectorAll("p");
for(var arsd_foreach_loop_counter_1 = 0; arsd_foreach_loop_counter_1 < 
arsd_foreach_loop_temporary_2.length; arsd_foreach_loop_counter_1++) {
 var element = 
arsd_foreach_loop_temporary_2[arsd_foreach_loop_counter_1];
 element.style.color = "red";
}


which I realize is an ugly mess but just because I used long names for the 
temporaries.
It: a) puts the iterable in it's own var, b) does a for loop over it, c) sets 
up a local for
the iterated thing inside the loop and d) pastes your code in there.


(that symbol before foreach is the one I did to denote a macro. I put it
on a hotkey in my editor... but I wanted something that I'd never use in
normal code, to keep the preprocessor both dead simple and out of the
way.)


You can also define your own macros and variables in the JS code
or as delegates in D.


I actually wrote this macro system for css, but it works pretty well
here too. (html.d also includes a CssMacroExpander, which runs this
and a de-nesting function.)


This is pretty new stuff, so I'll probably be adding more functions
as I use it more.