Leap year check in Java (functional style) I have done the leap year check on https://exercism.io already in a lot of languages. Today I came back to the exercise in Java and was playing around with some maybe more funny ways to do the check. The following more functional one kept me thinking of:

import java.util.stream.IntStream;

class Leap {

    boolean isLeapYear(final int year) {
        return IntStream.of(4, 100, 400)
            .filter(divisor -&gt; year % divisor == 0)
            .count() % 2 == 1;
    }

}


What do you think? Is this readable? At least it seems to be extensible in case more rules will ever get added ;)

Another funny fact: due to prime factorization we actually could also use the IntStream.of(2, 25, 16) (but for sure that doesn't help in readability).
What do you think?

It certainly is clever. That is not always a good thing, because it might be harder to understand what is going on (you have to be at least as clever)

Is this readable?

Not really. I can't get from a glance that 100 is treated differently from 400 and why. Also there is no room for comments close to you code.

If you make a set of ifs you could do something like:

if (year % 400 ... ) // if 400 then a leap year
{..
..}


In your IntStream you could do something like:

   IntStream.of(  4,    //yes
                100,    //no
                400     //yes
               )


But then you kinda miss the 'one-liner' point of your solution.

Is it efficient?

Not per se. Most efficient would be to first check divisibility by 400, if true, return true, etc.

Prime factorization funny fact

This in incorrect; because it would label 1998 as leap year. Or do you mean something different?