package ch.claude_martin.java8; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Objects; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; /** * Currying is great to spice up your code! *
* Java isn't as elegant as Haskell when it comes to Currying, but here are some * examples. *
* Read the HaskellWiki for more * info. *
* This was made with JDK8 Beta 2013-oct-10 (1.8.0-ea-b111). Also tested with
* the newer Beta 120.
*
* @author Claude Martin
*
*/
public class Currying {
/** A 2-Tuple with two values. */
static class Pair {
public Pair(final A first, final B second) {
super();
this._1 = first;
this._2 = second;
}
final A _1;
final B _2;
}
/** A 3-Tuple with three values. */
static class Triple extends Pair {
public Triple(final A first, final B second, final C third) {
super(first, second);
this._3 = third;
}
final C _3;
}
/** A 4-Tuple with four values. */
static class Quad extends Triple {
public Quad(final A first, final B second, final C third, final D fourth) {
super(first, second, third);
this._4 = fourth;
}
final D _4;
}
/**
* Run this to see what it does. I recommend using a debugger for this.
*/
public static void main(final String... args) {
{
// A function always consumes only one parameter!
// If you want more parameters you must return a function to consume more
// parameters.
// This is the "add"-function (addition):
final Function