0

In the Lambda Expressions Java Tutorial, approach 5 improves upon approach 4 by converting the anonymous class instantiation

new CheckPerson() {
    public boolean test(Person p) {
        return p.getGender() == Person.Sex.MALE
            && p.getAge() >= 18
            && p.getAge() <= 25;
    }
}

to

(Person p) -> p.getGender() == Person.Sex.MALE
    && p.getAge() >= 18
    && p.getAge() <= 25

Is there an equivalent in Scala so that I can avoid doing:

new CheckPerson {
    def test(p: Person): Boolean = {
        p.getGender == Person.Sex.MALE
            && p.getAge >= 18
            && p.getAge <= 25
    }
}
Paul I
  • 860
  • 2
  • 9
  • 16

1 Answers1

4
val x: CheckPerson = (p: Person) => p.getGender == Person.Sex.MALE &&
                                    p.getAge >= 18 &&
                                    p.getAge <= 25
Joe K
  • 18,204
  • 2
  • 36
  • 58