From ac1e7a7a41b419becb5a6ae1d126144623d1e7b8 Mon Sep 17 00:00:00 2001 From: John McClean Date: Tue, 6 Dec 2016 13:47:48 +0000 Subject: [PATCH] consumeAny, applyAny --- cyclops-sum-types/readme.md | 15 +++++- .../com/aol/cyclops/sum/types/Either.java | 24 ++++++++- .../com/aol/cyclops/sum/types/Either3.java | 24 ++++++++- .../com/aol/cyclops/sum/types/Either4.java | 50 ++++++++++++++---- .../com/aol/cyclops/sum/types/Either5.java | 52 ++++++++++++++----- .../aol/cyclops/sum/types/Either3Test.java | 10 +++- 6 files changed, 147 insertions(+), 28 deletions(-) diff --git a/cyclops-sum-types/readme.md b/cyclops-sum-types/readme.md index 6d8f6c08..d6656123 100644 --- a/cyclops-sum-types/readme.md +++ b/cyclops-sum-types/readme.md @@ -68,11 +68,24 @@ interface OS { OS os; os.match() - .to(e->Either3.visitAny(e,System.out::println)); + .to(e->Either3.visitAny(System.out::println,e)); //prints OS details ``` +applyAny and consumeAny can be used with method references + +```java +//apply a function to any of the types +test.to(Either3::applyAny) + .apply(b->b.toString()); + +//consume any of the types +just.to(Either3::consumeAny) + .accept(System.out::println); + +``` + diff --git a/cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either.java b/cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either.java index c85e89dc..d9d13f14 100644 --- a/cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either.java +++ b/cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either.java @@ -237,14 +237,34 @@ public static Either leftEval(final Eval left) { } - + /** + * Static method useful as a method reference for fluent consumption of any value type stored in this Either + * (will capture the lowest common type) + * + *
+     * {@code 
+     * 
+     *   myEither.to(Either::consumeAny)
+                 .accept(System.out::println);
+     * }
+     * 
+ * + * @param either Either to consume value for + * @return Consumer we can apply to consume value + */ + static Consumer> consumeAny(Either either){ + return in->visitAny(in,either); + } + static Function,R> applyAny(Either either){ + return in->visitAny(either,in); + } static R visitAny(Either either, Function fn){ return either.visit(fn, fn); } - static X visitAny(Either either, Consumer c){ + static X visitAny(Consumer c,Either either){ Function fn = x ->{ c.accept(x); return x; diff --git a/cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either3.java b/cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either3.java index 98071278..3480f0d5 100644 --- a/cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either3.java +++ b/cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either3.java @@ -201,10 +201,32 @@ public static Either3 fromIterable(final Iterable itera return it.hasNext() ? Either3.right( it.next()) : Either3.left1(null); } + /** + * Static method useful as a method reference for fluent consumption of any value type stored in this Either + * (will capture the lowest common type) + * + *
+     * {@code 
+     * 
+     *   myEither.to(Either3::consumeAny)
+                 .accept(System.out::println);
+     * }
+     * 
+ * + * @param either Either to consume value for + * @return Consumer we can apply to consume value + */ + static Consumer> consumeAny(Either3 either){ + return in->visitAny(in,either); + } + + static Function,R> applyAny(Either3 either){ + return in->visitAny(either,in); + } static R visitAny(Either3 either, Function fn){ return either.visit(fn, fn,fn); } - static X visitAny(Either3 either, Consumer c){ + static X visitAny(Consumer c,Either3 either){ Function fn = x ->{ c.accept(x); return x; diff --git a/cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either4.java b/cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either4.java index e328ffba..e8b25925 100644 --- a/cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either4.java +++ b/cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either4.java @@ -70,22 +70,52 @@ public interface Either4 extends Functor, MonadicValue, Supplier, ApplicativeFunctor { - static R visitAny(Either4 either, Function fn){ - return either.visit(fn, fn,fn,fn); + + /** + * Static method useful as a method reference for fluent consumption of any value type stored in this Either + * (will capture the lowest common type) + * + *
+     * {@code 
+     * 
+     *   myEither.to(Either4::consumeAny)
+                 .accept(System.out::println);
+     * }
+     * 
+ * + * @param either Either to consume value for + * @return Consumer we can apply to consume value + */ + static Consumer> consumeAny( + Either4 either) { + return in -> visitAny(in, either); + } + + static Function, R> applyAny( + Either4 either) { + return in -> visitAny(either, in); + } + + static R visitAny( + Either4 either, Function fn) { + return either.visit(fn, fn, fn, fn); } - static X visitAny(Either4 either, Consumer c){ - Function fn = x ->{ + + static X visitAny(Consumer c, + Either4 either) { + Function fn = x -> { c.accept(x); return x; }; - return visitAny(either,fn); + return visitAny(either, fn); } - - static Either4 fromMonadicValue(MonadicValue mv4){ - if(mv4 instanceof Either4){ - return (Either4)mv4; + + static Either4 fromMonadicValue(MonadicValue mv4) { + if (mv4 instanceof Either4) { + return (Either4) mv4; } - return mv4.toOptional().isPresent()? Either4.right(mv4.get()) : Either4.left1(null); + return mv4.toOptional() + .isPresent() ? Either4.right(mv4.get()) : Either4.left1(null); } /** diff --git a/cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either5.java b/cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either5.java index e6d4d3fd..9fcd17c2 100644 --- a/cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either5.java +++ b/cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either5.java @@ -71,24 +71,52 @@ public interface Either5 extends Functor, MonadicValue, Supplier, ApplicativeFunctor { - - - static R visitAny(Either5 either, Function fn){ - return either.visit(fn, fn,fn,fn,fn); + + /** + * Static method useful as a method reference for fluent consumption of any value type stored in this Either + * (will capture the lowest common type) + * + *
+     * {@code 
+     * 
+     *   myEither.to(Either5::consumeAny)
+                 .accept(System.out::println);
+     * }
+     * 
+ * + * @param either Either to consume value for + * @return Consumer we can apply to consume value + */ + static Consumer> consumeAny( + Either5 either) { + return in -> visitAny(in, either); } - static X visitAny(Either5 either, Consumer c){ - Function fn = x ->{ + + static Function, R> applyAny( + Either5 either) { + return in -> visitAny(either, in); + } + + static R visitAny( + Either5 either, Function fn) { + return either.visit(fn, fn, fn, fn, fn); + } + + static X visitAny( + Consumer c, Either5 either) { + Function fn = x -> { c.accept(x); return x; }; - return visitAny(either,fn); + return visitAny(either, fn); } - - static Either5 fromMonadicValue(MonadicValue mv5){ - if(mv5 instanceof Either5){ - return (Either5)mv5; + + static Either5 fromMonadicValue(MonadicValue mv5) { + if (mv5 instanceof Either5) { + return (Either5) mv5; } - return mv5.toOptional().isPresent()? Either5.right(mv5.get()) : Either5.left1(null); + return mv5.toOptional() + .isPresent() ? Either5.right(mv5.get()) : Either5.left1(null); } /** diff --git a/cyclops-sum-types/src/test/java/com/aol/cyclops/sum/types/Either3Test.java b/cyclops-sum-types/src/test/java/com/aol/cyclops/sum/types/Either3Test.java index 42bfcfdd..9b97a305 100644 --- a/cyclops-sum-types/src/test/java/com/aol/cyclops/sum/types/Either3Test.java +++ b/cyclops-sum-types/src/test/java/com/aol/cyclops/sum/types/Either3Test.java @@ -73,10 +73,16 @@ public void mapFlatMapTest(){ .flatMap(i->Either3.right(i*4)) .get(),equalTo(80)); } - + static class Base{ } + static class One extends Base{ } + static class Two extends Base{} @Test public void visitAny(){ - just.to(e->Either3.visitAny(e,System.out::println)); + + Either3 test = Either3.right(new Two()); + test.to(Either3::applyAny).apply(b->b.toString()); + just.to(Either3::consumeAny).accept(System.out::println); + just.to(e->Either3.visitAny(System.out::println,e)); Object value = just.to(e->Either3.visitAny(e,x->x)); assertThat(value,equalTo(10)); }