Skip to content

Commit ac1e7a7

Browse files
committed
consumeAny, applyAny
1 parent 763da11 commit ac1e7a7

File tree

6 files changed

+147
-28
lines changed

6 files changed

+147
-28
lines changed

cyclops-sum-types/readme.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,24 @@ interface OS {
6868
OS os;
6969

7070
os.match()
71-
.to(e->Either3.visitAny(e,System.out::println));
71+
.to(e->Either3.visitAny(System.out::println,e));
7272

7373
//prints OS details
7474

7575
```
7676

77+
applyAny and consumeAny can be used with method references
78+
79+
```java
80+
//apply a function to any of the types
81+
test.to(Either3::applyAny)
82+
.apply(b->b.toString());
83+
84+
//consume any of the types
85+
just.to(Either3::consumeAny)
86+
.accept(System.out::println);
87+
88+
```
89+
7790

7891

cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,34 @@ public static <LT, B, RT> Either<LT, RT> leftEval(final Eval<LT> left) {
237237
}
238238

239239

240-
240+
/**
241+
* Static method useful as a method reference for fluent consumption of any value type stored in this Either
242+
* (will capture the lowest common type)
243+
*
244+
* <pre>
245+
* {@code
246+
*
247+
* myEither.to(Either::consumeAny)
248+
.accept(System.out::println);
249+
* }
250+
* </pre>
251+
*
252+
* @param either Either to consume value for
253+
* @return Consumer we can apply to consume value
254+
*/
255+
static <X, LT extends X, M extends X, RT extends X> Consumer<Consumer<? super X>> consumeAny(Either<LT,RT> either){
256+
return in->visitAny(in,either);
257+
}
241258

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

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

247-
static <X, LT extends X, RT extends X> X visitAny(Either<LT,RT> either, Consumer<? super X> c){
267+
static <X, LT extends X, RT extends X> X visitAny(Consumer<? super X> c,Either<LT,RT> either){
248268
Function<? super X, X> fn = x ->{
249269
c.accept(x);
250270
return x;

cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either3.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,32 @@ public static <ST, T,RT> Either3<ST, T,RT> fromIterable(final Iterable<RT> itera
201201
return it.hasNext() ? Either3.right( it.next()) : Either3.left1(null);
202202
}
203203

204+
/**
205+
* Static method useful as a method reference for fluent consumption of any value type stored in this Either
206+
* (will capture the lowest common type)
207+
*
208+
* <pre>
209+
* {@code
210+
*
211+
* myEither.to(Either3::consumeAny)
212+
.accept(System.out::println);
213+
* }
214+
* </pre>
215+
*
216+
* @param either Either to consume value for
217+
* @return Consumer we can apply to consume value
218+
*/
219+
static <X, LT extends X, M extends X, RT extends X> Consumer<Consumer<? super X>> consumeAny(Either3<LT,M,RT> either){
220+
return in->visitAny(in,either);
221+
}
222+
223+
static <X, LT extends X, M extends X, RT extends X,R> Function<Function<? super X, R>,R> applyAny(Either3<LT,M,RT> either){
224+
return in->visitAny(either,in);
225+
}
204226
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){
205227
return either.visit(fn, fn,fn);
206228
}
207-
static <X, LT extends X, M extends X, RT extends X> X visitAny(Either3<LT,M,RT> either, Consumer<? super X> c){
229+
static <X, LT extends X, M extends X, RT extends X> X visitAny(Consumer<? super X> c,Either3<LT,M,RT> either){
208230
Function<? super X, X> fn = x ->{
209231
c.accept(x);
210232
return x;

cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either4.java

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,52 @@ public interface Either4<LT1, LT2,LT3, RT> extends Functor<RT>,
7070
MonadicValue<RT>,
7171
Supplier<RT>,
7272
ApplicativeFunctor<RT> {
73-
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){
74-
return either.visit(fn, fn,fn,fn);
73+
74+
/**
75+
* Static method useful as a method reference for fluent consumption of any value type stored in this Either
76+
* (will capture the lowest common type)
77+
*
78+
* <pre>
79+
* {@code
80+
*
81+
* myEither.to(Either4::consumeAny)
82+
.accept(System.out::println);
83+
* }
84+
* </pre>
85+
*
86+
* @param either Either to consume value for
87+
* @return Consumer we can apply to consume value
88+
*/
89+
static <X, LT1 extends X, LT2 extends X, LT3 extends X, RT extends X> Consumer<Consumer<? super X>> consumeAny(
90+
Either4<LT1, LT2, LT3, RT> either) {
91+
return in -> visitAny(in, either);
92+
}
93+
94+
static <X, LT1 extends X, LT2 extends X, LT3 extends X, RT extends X, R> Function<Function<? super X, R>, R> applyAny(
95+
Either4<LT1, LT2, LT3, RT> either) {
96+
return in -> visitAny(either, in);
97+
}
98+
99+
static <X, LT1 extends X, LT2 extends X, LT3 extends X, RT extends X, R> R visitAny(
100+
Either4<LT1, LT2, LT3, RT> either, Function<? super X, ? extends R> fn) {
101+
return either.visit(fn, fn, fn, fn);
75102
}
76-
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){
77-
Function<? super X, X> fn = x ->{
103+
104+
static <X, LT1 extends X, LT2 extends X, LT3 extends X, RT extends X> X visitAny(Consumer<? super X> c,
105+
Either4<LT1, LT2, LT3, RT> either) {
106+
Function<? super X, X> fn = x -> {
78107
c.accept(x);
79108
return x;
80109
};
81-
return visitAny(either,fn);
110+
return visitAny(either, fn);
82111
}
83-
84-
static <LT1,LT2,LT3,RT> Either4<LT1,LT2,LT3,RT> fromMonadicValue(MonadicValue<RT> mv4){
85-
if(mv4 instanceof Either4){
86-
return (Either4)mv4;
112+
113+
static <LT1, LT2, LT3, RT> Either4<LT1, LT2, LT3, RT> fromMonadicValue(MonadicValue<RT> mv4) {
114+
if (mv4 instanceof Either4) {
115+
return (Either4) mv4;
87116
}
88-
return mv4.toOptional().isPresent()? Either4.right(mv4.get()) : Either4.left1(null);
117+
return mv4.toOptional()
118+
.isPresent() ? Either4.right(mv4.get()) : Either4.left1(null);
89119

90120
}
91121
/**

cyclops-sum-types/src/main/java/com/aol/cyclops/sum/types/Either5.java

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,52 @@ public interface Either5<LT1, LT2,LT3, LT4,RT> extends Functor<RT>,
7171
MonadicValue<RT>,
7272
Supplier<RT>,
7373
ApplicativeFunctor<RT> {
74-
75-
76-
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){
77-
return either.visit(fn, fn,fn,fn,fn);
74+
75+
/**
76+
* Static method useful as a method reference for fluent consumption of any value type stored in this Either
77+
* (will capture the lowest common type)
78+
*
79+
* <pre>
80+
* {@code
81+
*
82+
* myEither.to(Either5::consumeAny)
83+
.accept(System.out::println);
84+
* }
85+
* </pre>
86+
*
87+
* @param either Either to consume value for
88+
* @return Consumer we can apply to consume value
89+
*/
90+
static <X, LT1 extends X, LT2 extends X, LT3 extends X, LT4 extends X, RT extends X> Consumer<Consumer<? super X>> consumeAny(
91+
Either5<LT1, LT2, LT3, LT4, RT> either) {
92+
return in -> visitAny(in, either);
7893
}
79-
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){
80-
Function<? super X, X> fn = x ->{
94+
95+
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(
96+
Either5<LT1, LT2, LT3, LT4, RT> either) {
97+
return in -> visitAny(either, in);
98+
}
99+
100+
static <X, LT1 extends X, LT2 extends X, LT3 extends X, LT4 extends X, RT extends X, R> R visitAny(
101+
Either5<LT1, LT2, LT3, LT4, RT> either, Function<? super X, ? extends R> fn) {
102+
return either.visit(fn, fn, fn, fn, fn);
103+
}
104+
105+
static <X, LT1 extends X, LT2 extends X, LT3 extends X, LT4 extends X, RT extends X> X visitAny(
106+
Consumer<? super X> c, Either5<LT1, LT2, LT3, LT4, RT> either) {
107+
Function<? super X, X> fn = x -> {
81108
c.accept(x);
82109
return x;
83110
};
84-
return visitAny(either,fn);
111+
return visitAny(either, fn);
85112
}
86-
87-
static <LT1,LT2,LT3,LT4,RT> Either5<LT1,LT2,LT3,LT4,RT> fromMonadicValue(MonadicValue<RT> mv5){
88-
if(mv5 instanceof Either5){
89-
return (Either5)mv5;
113+
114+
static <LT1, LT2, LT3, LT4, RT> Either5<LT1, LT2, LT3, LT4, RT> fromMonadicValue(MonadicValue<RT> mv5) {
115+
if (mv5 instanceof Either5) {
116+
return (Either5) mv5;
90117
}
91-
return mv5.toOptional().isPresent()? Either5.right(mv5.get()) : Either5.left1(null);
118+
return mv5.toOptional()
119+
.isPresent() ? Either5.right(mv5.get()) : Either5.left1(null);
92120

93121
}
94122
/**

cyclops-sum-types/src/test/java/com/aol/cyclops/sum/types/Either3Test.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,16 @@ public void mapFlatMapTest(){
7373
.flatMap(i->Either3.right(i*4))
7474
.get(),equalTo(80));
7575
}
76-
76+
static class Base{ }
77+
static class One extends Base{ }
78+
static class Two extends Base{}
7779
@Test
7880
public void visitAny(){
79-
just.to(e->Either3.visitAny(e,System.out::println));
81+
82+
Either3<One,Two,Two> test = Either3.right(new Two());
83+
test.to(Either3::applyAny).apply(b->b.toString());
84+
just.to(Either3::consumeAny).accept(System.out::println);
85+
just.to(e->Either3.visitAny(System.out::println,e));
8086
Object value = just.to(e->Either3.visitAny(e,x->x));
8187
assertThat(value,equalTo(10));
8288
}

0 commit comments

Comments
 (0)