Skip to content

Instantly share code, notes, and snippets.

@O5ten
Last active November 10, 2017 22:32
Show Gist options
  • Save O5ten/7215b56d57316044f0ac to your computer and use it in GitHub Desktop.
Save O5ten/7215b56d57316044f0ac to your computer and use it in GitHub Desktop.
Groovy Higher order functions
class Person {
String name
int age
Gender gender
enum Gender {
MALE, FEMALE, UNKNOWN
}
}
def persons = [new Person(name: 'Bob', age: 25, gender: Person.Gender.MALE),
new Person(name: 'Alice', age: 42, gender: Person.Gender.FEMALE)]
persons.findAll {
Person v ->
(v.age < 27 &&
v.age > 24 &&
(v.gender == Person.Gender.MALE || v.gender ==Person.Gender.UNKNOWN))
}.collect{ "${it.name}, ${it.age}, ${it.gender}" }
@O5ten
Copy link
Author

O5ten commented May 8, 2015

Output:
[Bob, 25, MALE]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment