Enums of Lambdas

Millies, Sebastian Sebastian.Millies at softwareag.com
Wed Oct 2 11:59:20 PDT 2013


Hi there,

what's the most elegant way to define an enum of lambdas?
For example, this enum of BinaryOperators looks clumsy:

public static enum Calc {

        CalcMult((n, m) -> n * m),

        CalcAdd((n, m) -> {
            int result = 0;
            for (int i = 0; i < n; i++)
                result += m;
            return result;
        });

        private Calc(IntBinaryOperator op) {
            this.op = op;
        }

        private final IntBinaryOperator op;
    }

I guess I'd perhaps be better off not using an enum at all, but just two static final members.


n  Sebastian



Software AG - Sitz/Registered office: Uhlandstra?e 12, 64297 Darmstadt, Germany - Registergericht/Commercial register: Darmstadt HRB 1562 - Vorstand/Management Board: Karl-Heinz Streibich (Vorsitzender/Chairman), Dr. Wolfram Jost, Arnd Zinnhardt; - Aufsichtsratsvorsitzender/Chairman of the Supervisory Board: Dr. Andreas Bereczky - http://www.softwareag.com



More information about the lambda-dev mailing list