
Java Functions: Every Java FunctionalInterface You Want - BrandonM
https://github.com/mintern-java/functions
======
ZitchDog
It's so sad that this is necessary. Proper function types would have made
lambdas so much more useful.

~~~
BrandonM
I agree wholeheartedly with your first sentence, but I still find lambdas to
be quite useful in spite of their limitations.

~~~
ZitchDog
I find them useful as well. But their usefulness is just not on the level of
closures in functional languages, and it makes me sad.

------
norswap
Does someone know of a good tree-shaking packager for Java? Something that
builds a jar (or a directory) with only the needed .class files (or even
better, versions of the .class files with the used functions)?

Looks like it would perfectly complement this project.

~~~
TacticalCoder
Proguard exists since many years and does optimization, shrinking, unused
classes and unused methods removal etc. Even obfuscation if you want.

It's been a long time I haven't used it but back in the days I used it for
years as part of the build chain and it was pretty flawless.

As I understand it nowadays it's even used for Android builds.

~~~
_asummers
Last I checked (~3 months ago) Proguard was not Java 8 compatible, at least
according to their website.

~~~
jared314
Proguard 5.x supports Java 8.

~~~
_asummers
That's terrific news! Thanks.

------
chvid
I do hope I am wrong; but isn't this like creating a version of Map for every
possible permutation of the primitive types?

I.e. MapIntInt MapIntShort MapShortBool etc. because you cannot write Map<int,
short> and somehow think that the overhead of boxing (Map<Integer, Short>) is
unbearable.

~~~
makeramen
Yeah I'm confused here too, I understand there might be a need for some of
these, but you should be able to just rely on autoboxing of primitives to
Objects like what RxJava does:
[https://github.com/ReactiveX/RxJava/tree/1.x/src/main/java/r...](https://github.com/ReactiveX/RxJava/tree/1.x/src/main/java/rx/functions)

I can maybe see an extra variation for exceptions, but that would be one more
generic or a Set<? extends Exception> no?

~~~
chvid
Yes.

------
eridal
Isn't possible to generate those like on-demand? That would make those 16k
classes availables to use, but present only when you need them.

~~~
BrandonM
It may be! It would be pretty cool to do something like:

    
    
        import net.mintern.functions.auto.ObjCharToBool;
    

and have the generate-classes stage be able to see that import and
automatically generate the requisite interface. Of course, the IDE might not
be happy until the first compile, but that seems tolerable.

It seems like Project Lombok has some similar issues... I'll have to see how
it handles them.

That would also allow an arbitrary function signature, though I personally
think it starts to be better to write your own @FunctionalInterface for
readability reasons once you hit more than 3 arguments.

------
maaaats
What is the use for this?

~~~
BrandonM
Our company has been slowly converting a medium-sized (~100K LOC) Java
codebase to take advantage of some Java 8 features. One example is a utility
method that accepts a String DB query and then executes a Consumer<ResultSet>
for each resulting row. The utility method creates the connection, uses try-
with-resources to ensure that it's properly closed, etc. This makes a lot of
code a lot cleaner.

But unfortunately, many ResultSet operations can throw SQLException. So we
created a `ConsumerE<T, E extends Exception>` to use in such cases, so that we
can allow some lambdas to throw exceptions where it is sensible
(InterruptedException, SQLException, and IOException being the major
culprits).

Pretty soon we ended up with FunctionE, BiFunctionE, and so on.

At the same time, we found a need for more interfaces, such as
ObjIntFunction<T, R>, that are not provided by JDK 8.

Rather than continuing to implement these on an as-needed basis, I decided to
just solve the problem once-and-for-all by creating this library.

~~~
akaivola
I had to register for this. You have pretty much tried to invent Either via
code generation. I suggest you look at functionaljava's Either for further
reference.

Code generation is in my experience a clear indication that the problem you
have has not really been understood.

~~~
BrandonM
I understand Either, and I would absolutely prefer to be using it. That was
going to be one of my follow up projects, and it would depend on this one.

I took a look functionaljava, and it doesn't provide the following:

    
    
        Either<IOExeption, String> line = Either.from(reader::getLine);
    

For that, you would need to have a signature like:

    
    
        static <E extends Exception, R> Either<E, R> from(NilToObjE<R, E> f);
    

That's exactly what this library gives you.

------
TeeWEE
Omg. Java is getting crippled. The language was never designed with lambda's
in mind. And now we have Java 8 with "not-really-lambdas" But some kind of
interface magic. Its just ugly as hell.

------
nardi
Over 16,000 classes. Better increase your PermGen.

~~~
pron
No more PermGen in Java 8. Besides, you only pay for the classes you use.

