Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Chan Ag
Rancher
Posts: 1089
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chan Ag
Rancher
Posts: 1089
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Kenneth.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic