News for hackers. Stuff that matters.
Re:Java Functions: Every Java FunctionalInterface You Want by maaaats on Friday November 07, 2014 @05:06PM (#8574908)
What is the use for this?
Re:Java Functions: Every Java FunctionalInterface You Want by BrandonM on Friday November 07, 2014 @05:29PM (#8575013)
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.

Re:Java Functions: Every Java FunctionalInterface You Want by akaivola on Saturday November 08, 2014 @04:15AM (#8576412)
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.

Re:Java Functions: Every Java FunctionalInterface You Want by BrandonM on Saturday November 08, 2014 @04:41AM (#8576446)
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.
Re:Java Functions: Every Java FunctionalInterface You Want by makeramen on Saturday November 08, 2014 @03:18AM (#8576336)
Have you considered RxJava?
Re:Java Functions: Every Java FunctionalInterface You Want by BrandonM on Saturday November 08, 2014 @04:54AM (#8576464)
For?
Re:Java Functions: Every Java FunctionalInterface You Want by runT1ME on Friday November 07, 2014 @10:17PM (#8575882)
Do you know about Scala?
Re:Java Functions: Every Java FunctionalInterface You Want by BrandonM on Saturday November 08, 2014 @04:51AM (#8576461)
Yes.
Re:Java Functions: Every Java FunctionalInterface You Want by runT1ME on Sunday November 09, 2014 @07:49PM (#8581479)
It sort of seems like you've spent a lot of effort to re-invent a less good version of scala? Why not Scala?
Re:Java Functions: Every Java FunctionalInterface You Want by BrandonM on Sunday November 09, 2014 @10:25PM (#8581891)
This was perhaps a 20-hour project, and we're already using it in our Java codebase. Are you instead recommending a Scala rewrite for a 100,000-line production system?
Re:Java Functions: Every Java FunctionalInterface You Want by frowaway001 on Monday November 10, 2014 @10:53AM (#8584201)
> Are you instead recommending a Scala rewrite for a 100,000-line production system?

Why? Even if a rewrite/translation of such a small system was perfectly possible, it's not necessary because of the excellent Scala -> Java interop.

Re:Java Functions: Every Java FunctionalInterface You Want by dyadic on Friday November 07, 2014 @05:12PM (#8574935)
With Java 8 you can create anonymous functions, but they have to conform to a pre-existing interface. This is a huge collection of those interfaces.
Re:Java Functions: Every Java FunctionalInterface You Want by dfkf on Friday November 07, 2014 @10:44PM (#8575926)
It's Java's take on this. Java's version is somewhat longer, however.

  public delegate void Action<in T1>(T1 arg1);
  public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2);
  public delegate void Action<in T1, in T2, in T3>(T1 arg1, T2 arg2, T3 arg3);
  public delegate TRet Func<out TRet>();
  public delegate TRet Func<in T1, out TRet>(T arg1);
  public delegate TRet Func<in T1, in T2, out TRet>(T1 arg1, T2 arg2);
  public delegate TRet Func<in T1, in T2, in T3, out TRet>(T1 arg1, T2 arg2, T3 arg3);