Groovy

bls killing__Zoe at web.de
Mon Jan 1 05:03:24 PST 2007


Hallo Thomas,
Just in case that you like Groovy, have a look at Suneido. www.suneido.com
which has a quit similar
language implemented. What I really like is the block construct as follows :
(Would be nice to have something like that in D. ) MfG Bjoern
Suneido supports Smalltalk style "blocks". Basically, a block is a section
of code within a function, that can be called like a function, but that
operates within the context of the function call that created it (i.e.
shares its local variables).

Blocks can be used to implement user defined "control constructs". (In
Smalltalk, all control constructs are implemented with blocks.) For example,
you could implement your own version of "foreach":

for_each = function (list, block)
{
for (i = 0; i < list.Size(); ++i)
block(list[i])
}
list = #(12, 34, 56)
for_each(list)
{ |x| Print(x) }
=>  12
34
56
Suneido treats a block immediately following a function call as an
additional argument.

Blocks can also be used to execute sections of code in specific "contexts".
For example, the Catch function traps exceptions and returns them. (This is
useful in unit tests to verify that expected exceptions occur.)

catcher = function (block)
{
try
return block()
catch (x)
return x
}
catcher( { xyz } ) => "unitialized variable: xyz"
But the interesting part is that a block can outlive the function call that
created it, and when it does so, it keeps its context (set of local
variables). For example:

make_counter = function (next)
{ return { next++ } }
counter = make_counter(10)
Print(counter())
Print(counter())
Print(counter())
=>  10
11
12
In this example, make_counter returns a block. The block returns next++. You
see this type of code in Lisp / Scheme.


"Thomas Kuehne" <thomas-dloop at kuehne.cn> schreef in bericht
news:slrnepg04s.4v0.thomas-dloop at birke.kuehne.cn...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Groovy(http://groovy.codehaus.org/) is a dynamic language implemented on
> the Java VM with strong Java integration. As Groovy's roots and
> aims partially overlap with D's, some solutions might be interesting.
>
> DISCLAIMER: I'm not a Groovy expert.
>
> 1) ranges
>
> Groovy:
> #
> # assert (x in 2 .. 5);
> #
> # switch(y) {
> #    case 1: break;
> #    case 1900 .. 2006: break;
> # }
> #
>
> current D:
> #
> # assert((2 <= x) && (x <= 5));
> #
> # switch(y) {
> #    case 1: break;
> #    default:
> #       if((1900 <= y) && (y <= 2006)){
> #          break;
> #       }
> # }
>
> Especially the ranges support for switch/case is very useful.
>
>
> 2) literals for associative arrays
>
> Groovy:
> #
> # def map = ['a': 1, 'b': 2, 'c': 3]
> #
>
> current D:
> #
> # int[char] map;
> # map['a'] = 1;
> # map['b'] = 2;
> # map['c'] = 3;
> #
>
> Other useful features of Groovy depend too much on the VM
> (e.g. dynamic extending of classes) for integration into D.
>
> Thomas
>
>
> -----BEGIN PGP SIGNATURE-----
>
> iD8DBQFFmACaLK5blCcjpWoRAgKqAJ9BZIaQcCAux4EJn8ZHD4MyJdpn8QCfTb0N
> iq1UGHbq25cNJCN8ZPv+RnY=
> =IPRp
> -----END PGP SIGNATURE-----





More information about the Digitalmars-d mailing list