Browse Source

Less awesome version of function composition

master
Julio Biason 5 years ago
parent
commit
5c4fd4eff1
  1. 17
      Compo.java

17
Compo.java

@ -14,10 +14,14 @@ public class Compo {
Doubler doubler = new Doubler(); Doubler doubler = new Doubler();
PlusTwo plusTwo = new PlusTwo(); PlusTwo plusTwo = new PlusTwo();
Printer printer = new Printer(); Printer printer = new Printer(doubler.andThen(plusTwo));
// ^ for the awesomer version, passing the Function to
// the constructor wouldn't be necessary.
List<Long> result = Stream.of(2L, 4L, 5L, 7L) List<Long> result = Stream.of(2L, 4L, 5L, 7L)
.peek(doubler.andThen(plusTwo).andThen(printer)) // this would be awesomer
// .peek(doubler.andThen(plusTwo).andThen(printer))
.peek(printer)
.collect(Collectors.toList()); .collect(Collectors.toList());
System.out.println("Result: " + result); System.out.println("Result: " + result);
} }
@ -37,9 +41,16 @@ public class Compo {
} }
private static class Printer implements Consumer<Long> { private static class Printer implements Consumer<Long> {
Function processor;
public Printer(Function processor) {
this.processor = processor;
}
@Override @Override
public void accept(Long input) { public void accept(Long input) {
System.out.println("Consumer: " + input); System.out.println("Consumer: " + processor.apply(input));
} }
} }
} }

Loading…
Cancel
Save