Skip to content

Commit

Permalink
consumeAny, applyAny
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmcclean committed Dec 6, 2016
1 parent 763da11 commit ac1e7a7
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 28 deletions.
15 changes: 14 additions & 1 deletion cyclops-sum-types/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);

```



Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,34 @@ public static <LT, B, RT> Either<LT, RT> leftEval(final Eval<LT> 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)
*
* <pre>
* {@code
*
* myEither.to(Either::consumeAny)
.accept(System.out::println);
* }
* </pre>
*
* @param either Either to consume value for
* @return Consumer we can apply to consume value
*/
static <X, LT extends X, M extends X, RT extends X> Consumer<Consumer<? super X>> consumeAny(Either<LT,RT> either){
return in->visitAny(in,either);
}

static <X, LT extends X, M extends X, RT extends X,R> Function<Function<? super X, R>,R> applyAny(Either<LT,RT> either){
return in->visitAny(either,in);
}

static <X, PT extends X, ST extends X,R> R visitAny(Either<ST,PT> either, Function<? super X, ? extends R> fn){
return either.visit(fn, fn);
}

static <X, LT extends X, RT extends X> X visitAny(Either<LT,RT> either, Consumer<? super X> c){
static <X, LT extends X, RT extends X> X visitAny(Consumer<? super X> c,Either<LT,RT> either){
Function<? super X, X> fn = x ->{
c.accept(x);
return x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,32 @@ public static <ST, T,RT> Either3<ST, T,RT> fromIterable(final Iterable<RT> 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)
*
* <pre>
* {@code
*
* myEither.to(Either3::consumeAny)
.accept(System.out::println);
* }
* </pre>
*
* @param either Either to consume value for
* @return Consumer we can apply to consume value
*/
static <X, LT extends X, M extends X, RT extends X> Consumer<Consumer<? super X>> consumeAny(Either3<LT,M,RT> either){
return in->visitAny(in,either);
}

static <X, LT extends X, M extends X, RT extends X,R> Function<Function<? super X, R>,R> applyAny(Either3<LT,M,RT> either){
return in->visitAny(either,in);
}
static <X, LT extends X, M extends X, RT extends X,R> R visitAny(Either3<LT,M,RT> either, Function<? super X, ? extends R> fn){
return either.visit(fn, fn,fn);
}
static <X, LT extends X, M extends X, RT extends X> X visitAny(Either3<LT,M,RT> either, Consumer<? super X> c){
static <X, LT extends X, M extends X, RT extends X> X visitAny(Consumer<? super X> c,Either3<LT,M,RT> either){
Function<? super X, X> fn = x ->{
c.accept(x);
return x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,52 @@ public interface Either4<LT1, LT2,LT3, RT> extends Functor<RT>,
MonadicValue<RT>,
Supplier<RT>,
ApplicativeFunctor<RT> {
static <X, LT1 extends X, LT2 extends X,LT3 extends X, RT extends X,R> R visitAny(Either4<LT1,LT2,LT3,RT> either, Function<? super X, ? extends R> 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)
*
* <pre>
* {@code
*
* myEither.to(Either4::consumeAny)
.accept(System.out::println);
* }
* </pre>
*
* @param either Either to consume value for
* @return Consumer we can apply to consume value
*/
static <X, LT1 extends X, LT2 extends X, LT3 extends X, RT extends X> Consumer<Consumer<? super X>> consumeAny(
Either4<LT1, LT2, LT3, RT> either) {
return in -> visitAny(in, either);
}

static <X, LT1 extends X, LT2 extends X, LT3 extends X, RT extends X, R> Function<Function<? super X, R>, R> applyAny(
Either4<LT1, LT2, LT3, RT> either) {
return in -> visitAny(either, in);
}

static <X, LT1 extends X, LT2 extends X, LT3 extends X, RT extends X, R> R visitAny(
Either4<LT1, LT2, LT3, RT> either, Function<? super X, ? extends R> fn) {
return either.visit(fn, fn, fn, fn);
}
static <X, LT1 extends X, LT2 extends X, LT3 extends X, RT extends X> X visitAny(Either4<LT1,LT2,LT3,RT> either, Consumer<? super X> c){
Function<? super X, X> fn = x ->{

static <X, LT1 extends X, LT2 extends X, LT3 extends X, RT extends X> X visitAny(Consumer<? super X> c,
Either4<LT1, LT2, LT3, RT> either) {
Function<? super X, X> fn = x -> {
c.accept(x);
return x;
};
return visitAny(either,fn);
return visitAny(either, fn);
}
static <LT1,LT2,LT3,RT> Either4<LT1,LT2,LT3,RT> fromMonadicValue(MonadicValue<RT> mv4){
if(mv4 instanceof Either4){
return (Either4)mv4;

static <LT1, LT2, LT3, RT> Either4<LT1, LT2, LT3, RT> fromMonadicValue(MonadicValue<RT> 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);

}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,52 @@ public interface Either5<LT1, LT2,LT3, LT4,RT> extends Functor<RT>,
MonadicValue<RT>,
Supplier<RT>,
ApplicativeFunctor<RT> {


static <X, LT1 extends X, LT2 extends X,LT3 extends X,LT4 extends X, RT extends X,R> R visitAny(Either5<LT1,LT2,LT3,LT4,RT> either, Function<? super X, ? extends R> 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)
*
* <pre>
* {@code
*
* myEither.to(Either5::consumeAny)
.accept(System.out::println);
* }
* </pre>
*
* @param either Either to consume value for
* @return Consumer we can apply to consume value
*/
static <X, LT1 extends X, LT2 extends X, LT3 extends X, LT4 extends X, RT extends X> Consumer<Consumer<? super X>> consumeAny(
Either5<LT1, LT2, LT3, LT4, RT> either) {
return in -> visitAny(in, either);
}
static <X, LT1 extends X, LT2 extends X, LT3 extends X,LT4 extends X, RT extends X> X visitAny(Either5<LT1,LT2,LT3,LT4,RT> either, Consumer<? super X> c){
Function<? super X, X> fn = x ->{

static <X, LT1 extends X, LT2 extends X, LT3 extends X, LT4 extends X, RT extends X, R> Function<Function<? super X, R>, R> applyAny(
Either5<LT1, LT2, LT3, LT4, RT> either) {
return in -> visitAny(either, in);
}

static <X, LT1 extends X, LT2 extends X, LT3 extends X, LT4 extends X, RT extends X, R> R visitAny(
Either5<LT1, LT2, LT3, LT4, RT> either, Function<? super X, ? extends R> fn) {
return either.visit(fn, fn, fn, fn, fn);
}

static <X, LT1 extends X, LT2 extends X, LT3 extends X, LT4 extends X, RT extends X> X visitAny(
Consumer<? super X> c, Either5<LT1, LT2, LT3, LT4, RT> either) {
Function<? super X, X> fn = x -> {
c.accept(x);
return x;
};
return visitAny(either,fn);
return visitAny(either, fn);
}
static <LT1,LT2,LT3,LT4,RT> Either5<LT1,LT2,LT3,LT4,RT> fromMonadicValue(MonadicValue<RT> mv5){
if(mv5 instanceof Either5){
return (Either5)mv5;

static <LT1, LT2, LT3, LT4, RT> Either5<LT1, LT2, LT3, LT4, RT> fromMonadicValue(MonadicValue<RT> 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);

}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<One,Two,Two> 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));
}
Expand Down

0 comments on commit ac1e7a7

Please sign in to comment.