Created
June 8, 2012 22:04
-
-
Save msfroh/2898297 to your computer and use it in GitHub Desktop.
Tuples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoggingFunction1<R, T1> extends Function1<R, T1>{ | |
private final Function1<R, T1> wrappedFunction; | |
private final PrintStream logStream; | |
private final String name; | |
private LoggingFunction1(final Function1<R, T1> wrappedFunction, | |
final PrintStream logStream, final String name) { | |
this.wrappedFunction = wrappedFunction; | |
this.logStream = logStream; | |
this.name = name; | |
} | |
@Override | |
public R evaluate(final Function1<R, T1> self, final T1 i1) { | |
logStream.println(name + " called with " + i1); | |
final R returnValue = wrappedFunction.evaluate(wrappedFunction, i1); | |
logStream.println(name + " returned " + returnValue); | |
return returnValue; | |
} | |
public static <R, T1> Function1<R, T1> | |
addLogging(final Function1<R, T1> wrappedFunction, | |
final PrintStream logStream, final String name) { | |
return new LoggingFunction1<R, T1>(wrappedFunction, logStream, name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static functions.LoggingFunction1.addLogging; | |
public class LoggingFunction1Test { | |
@Test | |
public void testFunction1() throws Exception { | |
Function1<Integer, String> getLength = | |
new Function1<Integer, String>() { | |
@Override | |
public Integer evaluate(final Function1<Integer, String> self, | |
final String i1) { | |
return i1.length(); | |
} | |
}; | |
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); | |
PrintStream outputStream = new PrintStream(bytes); | |
Function1<Integer, String> loggingGetLength = | |
addLogging(getLength, outputStream, "getLength"); | |
loggingGetLength.apply("hello").get(); | |
assertEquals("getLength called with hello\n" + | |
"getLength returned 5\n", | |
new String(bytes.toByteArray())); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static functions.LoggingFunction1.addLogging; | |
import static tuples.TupleUtils.tupled; | |
import static tuples.TupleUtils.untupled; | |
public class LoggingFunction1Test { | |
// ... previous test case ... | |
@Test | |
public void testFunction2() throws Exception { | |
Function2<Integer, Integer, Integer> add = | |
new Function2<Integer, Integer, Integer>() { | |
@Override | |
public Integer evaluate( | |
final Function2<Integer, Integer, Integer> self, | |
final Integer i1, final Integer i2) { | |
return i1 + i2; | |
} | |
}; | |
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); | |
PrintStream outputStream = new PrintStream(bytes); | |
Function2<Integer, Integer, Integer> loggingAdd = | |
untupled(addLogging(tupled(add), outputStream, "add")); | |
loggingAdd.apply(4, 5).get(); | |
assertEquals("add called with (4,5)\n" + | |
"add returned 9\n", | |
new String(bytes.toByteArray())); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class Tuple2<T1,T2> { | |
public final T1 _1; | |
public final T2 _2; | |
public Tuple2(final T1 v1, final T2 v2) { | |
_1 = v1; | |
_2 = v2; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Tuple2Test { | |
private Tuple2<Integer, Integer> vectorAdd(Tuple2<Integer, Integer> v1, | |
Tuple2<Integer, Integer> v2) { | |
return new Tuple2<Integer, Integer>(v1._1 + v2._1, v1._2 + v2._2); | |
} | |
private String describePerson(Tuple2<String, Integer> nameAge) { | |
return nameAge._1 + " is " + nameAge._2 + " years old"; | |
} | |
@Test | |
public void testTuple2() throws Exception { | |
Tuple2<Integer, Integer> v1 = new Tuple2<Integer, Integer>(1, 1); | |
Tuple2<Integer, Integer> v2 = new Tuple2<Integer, Integer>(3, 4); | |
Tuple2<Integer, Integer> vsum = vectorAdd(v1, v2); | |
assertEquals(Integer.valueOf(4), vsum._1); | |
assertEquals(Integer.valueOf(5), vsum._2); | |
assertEquals("Michael is 32 years old", | |
describePerson(new Tuple2<String, Integer>("Michael", 32))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Tuple2Test { | |
private Tuple2<Integer, Integer> vectorAdd(Tuple2<Integer, Integer> v1, | |
Tuple2<Integer, Integer> v2) { | |
return tuple(v1._1 + v2._1, v1._2 + v2._2); | |
} | |
private String describePerson(Tuple2<String, Integer> nameAge) { | |
return nameAge._1 + " is " + nameAge._2 + " years old"; | |
} | |
@Test | |
public void testTuple2() throws Exception { | |
Tuple2<Integer, Integer> v1 = tuple(1, 1); | |
Tuple2<Integer, Integer> v2 = tuple(3, 4); | |
Tuple2<Integer, Integer> vsum = vectorAdd(v1, v2); | |
assertEquals(Integer.valueOf(4), vsum._1); | |
assertEquals(Integer.valueOf(5), vsum._2); | |
assertEquals("Michael is 32 years old", | |
describePerson(tuple("Michael", 32))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class TupleUtils { | |
public static <T1,T2> Tuple2<T1,T2> tuple(final T1 v1, final T2 v2) { | |
return new Tuple2<T1,T2>(v1, v2); | |
} | |
public static <T1,T2,T3> Tuple3<T1,T2,T3> tuple(final T1 v1, final T2 v2, final T3 v3) { | |
return new Tuple3<T1,T2,T3>(v1, v2, v3); | |
} | |
// ... more tuple factory methods, up to highest-needed dimensionality ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class TupleUtils { | |
// ... previous tuple factory methods ... | |
public static <R, T1,T2> Function1<R, Tuple2<T1,T2>> | |
tupled(final Function2<R,T1,T2> f) { | |
return new Function1<R, Tuple2<T1,T2>>() { | |
@Override | |
public R evaluate(Function1<R, Tuple2<T1,T2>> self, | |
Tuple2<T1,T2> i) { | |
return f.evaluate(f, i._1, i._2); | |
} | |
}; | |
} | |
public static <R, T1, T2> Function2<R, T1, T2> | |
untupled(final Function1<R, Tuple2<T1, T2>> f) { | |
return new Function2<R, T1, T2>() { | |
@Override | |
public R evaluate(final Function2<R, T1, T2> self, | |
final T1 i1, final T2 i2) { | |
return f.evaluate(f, tuple(i1, i2)); | |
} | |
}; | |
} | |
public static <R,T1,T2,T3> Function1<R, Tuple3<T1,T2,T3>> | |
tupled(final Function3<R,T1,T2,T3> f) { | |
return new Function1<R, Tuple3<T1,T2,T3>>() { | |
@Override | |
public R evaluate(Function1<R, Tuple3<T1,T2,T3>> self, | |
Tuple3<T1,T2,T3> i) { | |
return f.evaluate(f, i._1, i._2, i._3); | |
} | |
}; | |
} | |
public static <R,T1,T2,T3> Function3<R,T1,T2,T3> | |
untupled(final Function1<R, Tuple3<T1,T2,T3>> f) { | |
return new Function3<R, T1, T2, T3>() { | |
@Override | |
public R evaluate(final Function3<R, T1, T2, T3> self, | |
final T1 i1, final T2 i2, final T3 i3) { | |
return f.evaluate(f, tuple(i1, i2, i3)); | |
} | |
}; | |
} | |
// ... more tupling/untupling methods, up to highest-needed dimensionality ... | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment