File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Groovy and the fly likes Are functional programming languages like Groovy capable of expressing object relationships? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Are functional programming languages like Groovy capable of expressing object relationships?" Watch "Are functional programming languages like Groovy capable of expressing object relationships?" New topic
Author

Are functional programming languages like Groovy capable of expressing object relationships?

Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1049
    
  15
Hello Kenneth,

Are functional programming languages like groovy capable of expressing object relationships as elegantly as Java is capable of expressing? I've looked at simple demo programs in functional programming languages and from the initial look it looks like functional programming languages make it easy to code a program, but would these programs be as extensible as they'd be if they were written in Java?

Are the type checking, syntax checking and such things as strict in groovy as they are in Java? Are programming languages like Groovy a complete solution?

Thanks,
Chan.
Kenneth A. Kousen
gunslinger & author
Ranch Hand

Joined: Sep 18, 2002
Posts: 89
    
    5
Hi Chan,

Chan Ag wrote:

Are functional programming languages like groovy capable of expressing object relationships as elegantly as Java is capable of expressing? I've looked at simple demo programs in functional programming languages and from the initial look it looks like functional programming languages make it easy to code a program, but would these programs be as extensible as they'd be if they were written in Java?

First, I wouldn't call Groovy a functional language. It's an object-oriented language with functional capabilities. You still write classes with attributes and methods, but you can process them functionally. For example, if I have a class called Person, I can write this:

Or this:

Or even this:

But I still wouldn't call Groovy functional. But as you can see it has functional capabilities mixed in with the object-oriented stuff.
Chan Ag wrote:
Are the type checking, syntax checking and such things as strict in groovy as they are in Java? Are programming languages like Groovy a complete solution?

Type checking in Groovy is optional. You can use def everywhere (though most people don't), or nowhere (though most people find it useful in places). There's also an AST transformation called @TypeChecked that will allow Groovy to do compile-time type checking of variables and method arguments like Java. In that sense it's a strict as Java, but only if you want it to be.

Is it a complete solution? It can be, but I prefer to use Groovy and Java together and let each do what it does best. I use Java for tools, libraries, and basic infrastructure (like Spring), and I use Groovy for everything else.

One more trivial example. Once a client asked me to print out the numbers from 0 to 15 in binary, but with the leading zeroes included (actually he said no such thing, but that's what he meant). The solution was:

which returns:

0000
0001
0010
...
1111

Note how the solution moves from Groovy (the range and closure) to Java (Integer.toBinaryString) back to Groovy (padLeft, added to String as part of the Groovy JDK), without worrying about which language you happen to be using at any given moment.

I like Groovy and it makes coding fun for me. I will not, however, criticize functional languages like Clojure or hybrid languages like Scala. Use whatever helps you get your job done, and learning any of them helps make you a better coder.


Kenneth A. Kousen, Ph.D. (assorted certs), President, Kousen IT, Inc.
Author of Making Java Groovy - http://www.kousenit.com
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1049
    
  15
Thanks, Kenneth.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are functional programming languages like Groovy capable of expressing object relationships?