Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

public static final Lambda?

Is it considered good practice to group common lambda expressions in a utility class to avoid code duplication ?

What's the best way to do so ? Right now, I have a MathUtils class with a few public static final Functions members :

public class MathUtils{
    public static final Function<Long, Long> triangle = n -> n * (n + 1) / 2,
        pentagonal = n -> n * (3 * n - 1) / 2,
        hexagonal = n -> n * (2 * n - 1);
}
like image 337
a44043 Avatar asked Jul 12 '26 15:07

a44043


1 Answers

You could also do it this way

public class MathUtils
{
    public static long triangle(long n)
    {
        return n * (n + 1) / 2;
    }

And use it like

    MathUtils::triangle 

depending on your taste and use cases.

like image 178
ZhongYu Avatar answered Jul 15 '26 05:07

ZhongYu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!