Botre Posted May 24, 2016 Posted May 24, 2016 Inspired by: https://docs.oracle.com/javase/8/docs/api/java/util/function/Consumer.html The functional package does provide a Consumer (1 arg) and a BiConsumer (2args) but you're on your own if you want more args. I stopped at 5 because I have never really needed more than that. Example public static void main(String[] args) { Consumer.One<String> printInput = input -> System.out.println(input); Consumer.Two<Integer, Double> printSum = (a, b) -> System.out.println(a + b); //... Consumer.Five<Integer, Integer, Integer, Integer, Integer> printMultiplication = (a, b, c, d, e) ->System.out.println(a * b * c * d *e); } Consumers /** * Represents an operation that accepts one or multiple arguments and returns no result. * * Created by Botre on 24/05/2016. */ public final class Consumer { private Consumer() { } private interface IConsumer { } @FunctionalInterface public interface One<A> extends IConsumer { void consume(final A a); } @FunctionalInterface public interface Two<A, B> extends IConsumer { void consume(final A a, final B b); } @FunctionalInterface public interface Three<A, B, C> extends IConsumer { void consume(final A a, final B b, final C c); } @FunctionalInterface public interface Four<A, B, C, D> extends IConsumer { void consume(final A a, final B b, final C c, final D d); } @FunctionalInterface public interface Five<A, B, C, D, E> extends IConsumer { void consume(final A a, final B b, final C c, final D d, final E e); } } 3
Botre Posted June 24, 2016 Author Posted June 24, 2016 lol do u know what curried functions are Java doesn't have variadic generics. These are multi-type consumers.
Gangsta Homie Posted June 24, 2016 Posted June 24, 2016 (edited) Java doesn't have variadic generics. These are multi-type consumers. o truewhy do u finalize params? final on locals is useless, maybe for clarity but in this case its just an interface method Edited June 24, 2016 by Gangsta Homie
Botre Posted June 24, 2016 Author Posted June 24, 2016 o true why do u finalize params? final on locals is useless, maybe for clarity but in this case its just an interface method In my opinion, it's safer to finalize everything by default as opposed to leaving everything virtual by default. I prefer to selectively unlock doors instead of selectively locking them, I'm an eternal pessimist who's afraid of getting robbed. 1