Functional programming is a style of programming that emphasizes the application of functions to solve computing problems. This is in contrast with imperative programming, which emphasizes changes in state and the execution of sequential commands. If you want use a functional-only programming language, you should consider something like Haskell. If however you like Groovy but want to apply some functional style magic, read on.
Groovy's functions (like Java's) can be used to define functions which contain no imperative steps, e.g. a factorial function may look like:
In Groovy, we gain some slight syntactic sugar over Java in that we can leave out the
We can of course start to mix and match functional and imperative coding styles:
You can also use curry functions as follows:
See reference 1 or Chapter 5 of GINA for further details.
See reference 2 below for all the details, but to give you a flavour, first you must define some lazy list handling functions, then you can define and use infinite streams. Here is an example:
- Practically Groovy: Functional programming with curried closures
- Infinite Streams in Groovy
- Functional Programming Languages
- Why Functional Programming Matters
- Functional programming in the Java language
- Post on functional programming in Java - maybe a tad verbose
- FunctionalJ - A library for Functional Programming in Java