Skip to content

Commit

Permalink
Merge pull request #71 from aol/fix-for-68
Browse files Browse the repository at this point in the history
return Stream rather than Generic T from unwrapOtherMonadTypes, depre…
  • Loading branch information
johnmcclean committed Nov 13, 2015
2 parents cb985b1 + 988c1f7 commit 9e1e5de
Show file tree
Hide file tree
Showing 36 changed files with 1,318 additions and 1,008 deletions.
22 changes: 15 additions & 7 deletions cyclops-for-comprehensions/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ Two supported formats
```java
List<Integer> list= Arrays.asList(1,2,3);

Stream<Integer> stream = Do.add(list)
.yield((Integer i)-> i +2);
List<Integer> list = Do.add(list)
.yield((Integer i)-> i +2)
.unwrap();



assertThat(Arrays.asList(3,4,5),equalTo(stream.collect(Collectors.toList())));
assertThat(Arrays.asList(3,4,5),equalTo(list));
```

Yield, Filter and 'and' take curried functions
Expand All @@ -60,7 +61,8 @@ Yield, Filter and 'and' take curried functions
```java
Stream<Integer> stream = Do.add(asList(20,30))
.with( i->asList(1,2,3))
.yield(i-> j -> i + j+2);
.yield(i-> j -> i + j+2)
.asSequence();
```

Parameters are stack based, the parameter to the first function is an index into the first Collection or Monad, the parameter to the second function is an index into the second Collection or Monad and so on.
Expand All @@ -70,15 +72,17 @@ The above code could be rewritten as
```java
Stream<Integer> stream = Do.add(asList(20,30))
.with(any->asList(1,2,3))
.yield(x-> y -> x + y+2);
.yield(x-> y -> x + y+2)
.asSequence();
```
And it would work in exactly the same way

```java
List<Integer> list= Arrays.asList(1,2,3);
Stream<Integer> stream = Do.add(list)
.filter(a -> a>2)
.yield(a-> a +2);
.yield(a-> a +2)
.asSequence();



Expand Down Expand Up @@ -129,6 +133,7 @@ Things start to become a little unwieldy, but a little less so with for comprehe
Do.add(list)
.with(element -> numbers)
.yield( element -> num -> element + num )
.unwrap()
.forEach(System.out::println);
```
Expand All @@ -154,6 +159,7 @@ Let's add a third level of nesting
.add(numbers)
.add(dates)
.yield( element -> num -> date -> element + num+":"+date )
.unwrap()
.forEach(System.out::println);
```

Expand Down Expand Up @@ -184,6 +190,7 @@ Can be written as
Do.add(strs)
.add(opt)
.yield(v1->v2 -> v1 + v2)
.unwrap()
.forEach(System.out::println);
```
Expand Down Expand Up @@ -226,7 +233,8 @@ Guards (filter commands) can be placed at any stage of a for comprehension. E.g.
Stream<Double> s = Do.with(Arrays.asList(10.00,5.00,100.30))
.and((Double d)->Arrays.asList(2.0))
.filter((Double d)-> (Double e) -> e*d>10.00)
.yield((Double base)->(Double bonus)-> base*(1.0+bonus));
.yield((Double base)->(Double bonus)-> base*(1.0+bonus))
.asSequence();

double total = s.collect(Collectors.summingDouble(t->t));
assertThat(total,equalTo(330.9));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
final class BaseComprehensionData {

private ContextualExecutor delegate;

private ContextualExecutor currentContext;


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.aol.cyclops.comprehensions.donotation.typed;


import java.util.List;
import java.util.function.Function;

import lombok.AllArgsConstructor;
Expand All @@ -18,6 +19,7 @@
public abstract class DoComp {

PStack<Entry> assigned;
final Class orgType;

protected PStack<Entry> addToAssigned(Function f){
return assigned.plus(assigned.size(),createEntry(f));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
Expand All @@ -20,21 +22,21 @@
import com.aol.cyclops.sequence.SequenceM;
public class DoComp0 extends DoComp{
public DoComp0(PStack<Entry> assigned) {
super(assigned);
super(assigned,null);

}

public <T1> DoComp1<T1> addValues(T1... values){
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),Stream.of(values))));
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),Stream.of(values))),orgType);

}

public DoComp1<Character> add(CharSequence seq){
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),seq)));
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),seq)),orgType);

}
public DoComp1<Integer> times(int times){
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),times)));
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),times)),orgType);

}
/**
Expand All @@ -53,7 +55,12 @@ public DoComp1<Integer> times(int times){
* @return Next stage in for comprehension builder
*/
public <T1> DoComp1<T1> add(Iterable<T1> o){
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)));
Class orgType =null;
if(o instanceof List)
orgType = List.class;
else if(o instanceof Set)
orgType = Set.class;
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)),orgType);

}

Expand All @@ -75,7 +82,7 @@ public <T1> DoComp1<T1> add(Iterable<T1> o){
* @return Next stage in for comprehension builder
*/
public <T1> DoComp1<T1> add(Iterator<T1> o){
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)));
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)),orgType);

}

Expand All @@ -97,7 +104,7 @@ public <T1> DoComp1<T1> add(Iterator<T1> o){
* @return Next stage in for comprehension builder
*/
public <T1> DoComp1<T1> addStream(Stream<T1> o){
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)));
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)),orgType);

}
/**
Expand All @@ -116,7 +123,7 @@ public <T1> DoComp1<T1> addStream(Stream<T1> o){
* @return Next stage in for comprehension builder
*/
public <T1> DoComp1<T1> addBaseStream(BaseStream<T1,?> o){
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)));
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)),orgType);

}

Expand All @@ -138,7 +145,7 @@ public <T1> DoComp1<T1> addBaseStream(BaseStream<T1,?> o){
* @return Next stage in for comprehension builder
*/
public <T1> DoComp1<T1> add(Optional<T1> o){
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)));
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)),orgType);

}

Expand All @@ -160,7 +167,7 @@ public <T1> DoComp1<T1> add(Optional<T1> o){
* @return Next stage in for comprehension builder
*/
public <T1> DoComp1<T1> add(CompletableFuture<T1> o){
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)));
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)),orgType);

}

Expand All @@ -182,7 +189,7 @@ public <T1> DoComp1<T1> add(CompletableFuture<T1> o){
* @return Next stage in for comprehension builder
*/
public <T1> DoComp1<T1> add(AnyM<T1> o){
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)));
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)),orgType);

}

Expand All @@ -204,7 +211,7 @@ public <T1> DoComp1<T1> add(AnyM<T1> o){
* @return Next stage in for comprehension builder
*/
public <T1> DoComp1<T1> add(SequenceM<T1> o){
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)));
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)),orgType);

}

Expand All @@ -226,7 +233,7 @@ public <T1> DoComp1<T1> add(SequenceM<T1> o){
* @return Next stage in for comprehension builder
*/
public <T1> DoComp1<T1> add(Callable<T1> o){
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)));
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)),orgType);

}

Expand All @@ -249,7 +256,7 @@ public <T1> DoComp1<T1> add(Callable<T1> o){
* @return Next stage in for comprehension builder
*/
public <T1> DoComp1<T1> add(Supplier<T1> o){
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),(Supplier)()->o)));
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),(Supplier)()->o)),orgType);

}

Expand All @@ -271,7 +278,12 @@ public <T1> DoComp1<T1> add(Supplier<T1> o){
* @return Next stage in for comprehension builder
*/
public <T1> DoComp1<T1> add(Collection<T1> o){
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)));
Class orgType =null;
if(o instanceof List)
orgType = List.class;
else if(o instanceof Set)
orgType = Set.class;
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)),orgType);

}

Expand All @@ -296,7 +308,7 @@ public <T1> DoComp1<T1> add(Collection<T1> o){
* @return Next stage in for comprehension builder
*/
public <T1 extends String> DoComp1<T1> add(File o){
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)));
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)),orgType);

}

Expand All @@ -318,7 +330,7 @@ public <T1 extends String> DoComp1<T1> add(File o){
* @return Next stage in for comprehension builder
*/
public <T1 extends String> DoComp1<T1> add(URL o){
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)));
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)),orgType);

}

Expand All @@ -340,7 +352,7 @@ public <T1 extends String> DoComp1<T1> add(URL o){
* @return Next stage in for comprehension builder
*/
public <T1 extends String> DoComp1<T1> add(BufferedReader o){
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)));
return new DoComp1(assigned.plus(assigned.size(),new Entry("$$monad"+assigned.size(),o)),orgType);

}

Expand Down
Loading

0 comments on commit 9e1e5de

Please sign in to comment.