v2.0.0-MI7 of cyclops-react
2.0.0-MI7 of cyclops-react
What’s new cyclops-react v2 Milestone 7?
- Kotlin style sequence generators
- Bug fixes
Some Examples
#e.g. with suspend and yield operators - note providing a BooleanSupplier indicates the suspended block should be executed until false (in this case infinitely).
int i = 100;
ReactiveSeq.generate(suspend(infinitely(),s->s.yield(i++)))
.take(6)
.printOut();
/**
100
101§
102
103
104
105
106
**/
Execute the suspended block just once (itself meaningless unless we can also sequence actions)
int i = 100;
ReactiveSeq.generate(suspend(s->s.yield(i++)))
.take(6)
.printOut();
/**
100
**/
Yield a sequence of values
ReactiveSeq.generate(suspend(times(10),s-> {
System.out.println("Top level - should repeat after sequence completes!");
return s.yield(1,
() -> s.yield(2),
() -> s.yield(3),
() -> s.yield(4));
}))
.take(6)
.printOut();
Support method references
Generator<Integer> generator = suspendRef(times(10),this::next);
generator.stream()
.forEach(System.out::println);
List<Integer> list = generator.to()
.linkedListX(LAZY)
.type(VavrTypes.list())
.map(this::process)
.to(VavrConverters::LIST);
public Integer next(){
return i++;
}
int i = 100;
Method references directly during yielding
ReactiveSeq.generate(suspend(times(10),s-> {
System.out.println("Top level - should repeat after sequence completes!");
return s.yieldRef(1,
Generator::next,
Generator::next,
Generator::next,
Generator::next);
}
)).take(6)
.printOut();
public Integer next(){
return i++;
}
More managable / complex mutable state via Inner Classes (if really needed)
ReactiveSeq.<Integer>generate(suspend(times(10),new ContFunction<Integer>() {
int runningTotal =0;
@Override
public Generator<Integer> apply(Suspended<Integer> s) {
System.out.println("Top level - should repeat after sequence completes!");
return s.yield(1,
() -> {
runningTotal = runningTotal +5;
return s.yield(runningTotal+2);
},
() -> s.yield(runningTotal+3),
() -> s.yield(runningTotal+6));
}
}
)).take(6)
.printOut();
Control looping based on current value and support safe (s.maybe()) and unsafe (s.current()) to the current value.
ReactiveSeq.generate(suspend((Integer i)->i==4,s-> {
System.out.println("Top level - repeat infinetely after sequence (because we return 4!)");
return s.yield(1,
() -> s.yield(s.maybe()
.map(o->o+5)
.orElse(10)),
() -> s.yield(s.current()),
() -> s.yield(4));
}
)).take(120)
.printOut();
Supporting nested generators would allow very fine control over generating data sources..
ReactiveSeq.generate(suspend((Integer i)->i!=4, s-> {
Generator<Integer> gen1 = suspend(times(5),
s2->s2.yield(i++));
Generator<Integer> gen2 = suspend(times(2),
s2->s2.yield(k--));
return s.yieldAll(gen1.stream(),
gen2.stream());
}
)).take(12)
.printOut();
Changelog
Check out the features delivered and bugs fixed -
Dependency upgrades
None this time
Get cyclops-react
Gradle
compile 'com.aol.simplereact:cyclops-react:2.0.0-MI7’
Maven
<dependency>
<groupId>com.aol.simplereact</groupId>
<artifactId>cyclops-react</artifactId>
<version>2.0.0-MI7</version>
</dependency>
Documentation
Articles
- Reactive programming with Java 8 and simple-react: The Tutorial
- JDK Collection eXtensions
- Awesome Fluent Functions
- Straightforward Structural Pattern Matching
- Performant Functional Try
- Articles on medium
- Introducting the Cyclops Monad API
- Easier Try with Cyclops
- 4 flavors of Java 8 Functions
- Memoise Functions in Java 8
- Strategy Pattern in Java 8
- Functional Feature Toggling
- Dependency injection using the Reader Monad in Java8
- Scheduling a Stream
- Neophytes guide to Java 8 : Welcome to the Future
- Deterministic and Non-Deterministic Finite State Machines with Cyclops
License
cyclops-react is licensed under the Apache 2.0 license.