Programming Tools: Java Scripting Languages
Groovy's documentation provides an example that illustrates the benefits of using closures in place of anonymous classes. Using an anonymous class, we can write:
Button b = new Button ("Push Me");
b.onClick (new Action() {
public void execute (Object target)
{
buttonClicked();
}
});
Using a closure, this becomes:
Button b = new Button ("Push Me");
b.onClick { buttonClicked() }
Another nice feature of Groovy is its "each" operator, which is used to iterate over a collection:
SomeCollection stuff = new SomeCollection();
stuff.each() someClosure
// for example:
def myList = [1,2,4,5,6]
myList.each { println it } // where 'it' is current collection item
// outputs:
1
2
3
4
5
6
Groovy also has built-in structured "builders" for languages such as XML, HTML and more. Again, a simple example illustrates how natural it is to build structured text:
xml = new groovy.xml.MarkupBuilder()
def myList = ['acct1':['id':123111, 'type':'savings', 'balance':1234.56],
'acct2':['id':221212, 'type':'checking', 'balance':2010.02]]
doc = xml.accounts() {
for (entry in myList)
acct(id:entry.key) {
for (thing in entry.value)
item(name:thing.key, type=thing.value)
}
}
This small code fragment outputs:
<accounts>
<acct id='acct2'>
<item name='type'>checking</item>
<item name='id'>221212</item>
<item name='balance'>2010.02</item>
</acct>
<acct id='acct1'>
<item name='type'>savings</item>
<item name='id'>123111</item>
<item name='balance'>1234.56</item>
</acct>
Bindings
In the following example, the use of variable c is caught at compile time in a statically defined language. Only when a is greater than b is the use of the undefined variable, c, detected in an interpreted language.
a = 1 b = 2 if a > b: print c print a,b
Another major difference between a compiled language and an interpreted one is when things are bound to their references. In Groovy, the following code prints "Bound to local variable":
def varA = "Bound to global variable"
def closure = { varA }
public class C {
def varA = "Bound to local variable"
def closure = { varA } // bound to local varA at definition time
public def f = closure // f bound to local closure
};
def c = new C(); // create instance of C using new
println c.f() // invoke f in C
in Jython, the equivalent-looking code prints "Bound to global variable":
varA = "Bound to global variable" closure = lambda: varA class C: varA = "Bound to local variable" closure = lambda self: varA f = closure c = C() print c.f()
For those wanting more flexibility, the Jython/Python bindings are handier. For those wanting more stability, the Groovy implementation may be more desirable.
Currying
Currying function arguments is another difference. Groovy has a special "curry" mechanism to bind arguments to a function. In the following example, "foo bar" is printed:
def c = { arg1, arg2-> println "${arg1} ${arg2}" }
def d = c.curry("foo")
d("bar")
Jython inherits Python's natural ability to curry arguments using a number of techniques. One is:
def c(arg1, arg2): print arg1,arg2
def d(arg2): c("foo",arg2)
d("bar")
Maturity
Jython has been around a long time and is based on a mature language, Python. However, its development has stalled in recent years. Groovy is a relatively new language and thus still is developing. For example, its error diagnostics leave a lot to be desired. Also, at the moment, Groovy's following is much smaller than Jython's or Python's. However, both languages are picking up development activity, so you have a chance to influence both languages if you want to become involved.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Home, My Backup Data Center
- What's the tweeting protocol?
- Readers' Choice Awards
- New Products
- RSS Feeds
- Linux on Azure—a Strange Place to Find a Penguin
- Reply to comment | Linux Journal
9 hours 34 min ago - Reply to comment | Linux Journal
12 hours 7 min ago - Reply to comment | Linux Journal
13 hours 24 min ago - great post
13 hours 59 min ago - Google Docs
14 hours 21 min ago - Reply to comment | Linux Journal
19 hours 10 min ago - Reply to comment | Linux Journal
19 hours 57 min ago - Web Hosting IQ
21 hours 30 min ago - Thanks for taking the time to
23 hours 7 min ago - Linux is good
1 day 1 hour ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.



Comments
a sign of dynamic typing is
a sign of dynamic typing is the following (groovy code):
def foo(String s) {"String"}
def foo(Object o) {"Object"}
def obj = "obj"
assert foo(obj) == "String"
Object obj2 = "obj"
assert foo(obj2) == "String"
both, obj and obj2 have the static type java.lang.Object when viewed from Java, but for the function call the type String is used, as this is the runtime/dynamic type. So Groovy does have dynamic typing.
A compile step is not a sign of the typing mechanism.
What about BeanShell?
Sometimes I wonder whether I am the only person in the world who uses BeanShell, It's a java interpreter written in java and uses java syntax (have I written "java" too many times? ;) .)
I think there are two nice advantages when using BeanShell:
1) The programmer does not have to learn new syntax.
2) If desired, a more script-like syntax can be used.
Also BeanShell scripts can have acess to every object within the host application, can acess all of the available java classes and runs at the VM speed (it just means I like it a lot :) .)
I am not trying to make a case for BeanShell, I'm just curious to know why BeanShell has not gotten more coverage in "Java Scripting Languages" articles and learn more about the comparative advantages of using a diferent approach to java scripting.
JavaScript
By the way, JavaScript has nothing to do with the Java language, so it is not considered here.
Historically, this statement is correct, but that is about to change. Mazilla has a javascript implementation in Java called Rhino that is slated to be included in the next major release of the JVM. And we had just convinced everyone that Java and JavaScript are not the same....
NetRexx
Nice Article! I couldn't help but think about NetRexx which is Rexx built for a JVM. Rexx is an old classic scripting language from mainframes, OS/2, Amiga, Unix (sic!). It would be nice to also hear about Rexx's uses in this context, i.e. JVM scripting languages!
Ciao!
Good article. About your ver
Good article. About your very last sentence, if someone wants to actually influence development and design decisions, go with groovy. Jython isn't going to be influenced by anyone, because it is meant to be an exact clone of CPython, nothing more, nothing less (same way with IronPython for .NET).
For those comfortable with statically typed languages, scala (http://scala.epfl.ch/) and nice (http://nice.sf.net/) have some really fascinating features.
Good article; a few typos
I enjoyed the article. I had heard of Groovy, but not seen any examples. While the examples shown here are small, they do at least provide a little bit of flavor as to what the language is like.
There are some typos, however. In the section on Closures, there is obviously some markup intermixed in the first code example. Then in the immediately following paragraph, the word "each" should have double quotes around it, but only the first one is there.