-
Notifications
You must be signed in to change notification settings - Fork 51
Power Tuples
Cyclops has merged with simple-react. Please update your bookmarks (stars :) ) to https://github.com/aol/cyclops-react
Most recent version of cyclops-tuple is
compile group: 'com.aol.cyclops', name: 'cyclops-tuples', version: '7.2.3'
<dependency>
<groupId>com.aol.cyclops</groupId>
<artifactId>cyclops-tuples</artifactId>
<version>7.2.3</version>
</dependency>
- Wrap any Tuple type / Object (mapping fields to elements and back)
- Method call chaining support
- Asyncrhonous method call chaining support
- Inheritance relationship between Tuples
- Lazy and Strict map methods
- Lazy reordering
- Pattern matching
- For comprehensions
- Useful utility methods (asStreamOfStrings, asTwoNumbers etc)
- Concatonation
- LazySwap (reverse)
- Memoization
- asCollector
- asReducer
com.aol.cyclops.lambda.tuple.PowerTuples
Has static creational methods for PTuples1..8 as well as Tuple Concatenation and Lazily Swapping (reversing) values.
e.g.
lazySwap(PowerTuples.tuple(1, 2, 3))
@Test
public void cons(){
Three three = new Three(1,"hello",new Date());
assertThat(three.a,equalTo(PTuple3.ofTuple(three).v1()));
}
@AllArgsConstructor
static class Three{
int a;
String b;
Date c;
}
@AllArgsConstructor
static class TwoParams{
int num;
String value;
}
TwoParams p = PowerTuples.tuple(10,"hello").convert().to(TwoParams.class);
assertThat(p.num,equalTo(10));
assertThat(p.value,equalTo("hello"));
With filtering
method1().<PTuple1<Integer>>filter(t->t.v1()==0).call(this::method3);
Async method chaining
method1().<PTuple1<Integer>>filter(t->t.v1()==0).callAsync(this::method3).join();
Tuples can also be converted to flattened or unflattened Stream of Streams. asStreams will attempt to create a Stream from each element (via Collection::stream for example). BufferedReaders, Files, URLs, Arrays, Collections, CharSequences will all be turned into Streams.
A tuple of Collectors can be coerced to a single Collector
e.g. Collecting as a List and Set simultaneously
PTuple2<Set<Integer>,List<Integer>> res = Stream.of(1, 2, 2)
.collect(tuple(Collectors.toSet(),Collectors.toList()).asCollector());
See rich set of Collectors here java.util.stream.Collectors
Convert a tuple into a single Monoid (or Reducer) that can perform multiple reduce operations on a single Stream.
Monoid<Integer> sum = Monoid.of(0,(a,b)->a+b);
Monoid<Integer> mult = Monoid.of(1,(a,b)->a*b);
val result = tuple(sum,mult).<PTuple2<Integer,Integer>>asReducer()
.mapReduce(Stream.of(1,2,3,4));
Or alternatively
Monoid<Integer> sum = Monoid.of(0,(a,b)->a+b);
Monoid<Integer> mult = Monoid.of(1,(a,b)->a*b);
val result = tuple(sum,mult).<PTuple2<Integer,Integer>>asReducer()
.mapReduce(Stream.of(1,2,3,4));
assertThat(result,equalTo(tuple(10,24)));
String result = PowerTuples.tuple(1,2,3).matchValues(c -> cases(c) );
private <I,T> _MembersMatchBuilder<Object, T> cases(_MembersMatchBuilder<I, T> c) {
return c.with(1,2,3).then(i->"hello")
.with(4,5,6).then(i->"goodbye");
}
Result is "hello"