Skip to content

Extensible For Comprehensions for Java 8

johnmcclean-aol edited this page Feb 24, 2016 · 9 revisions

Cyclops has merged with simple-react. Please update your bookmarks (stars :) ) to https://github.com/aol/cyclops-react

All new develpoment on cyclops occurs in cyclops-react. Older modules are still available in maven central.

screen shot 2016-02-22 at 8 44 42 pm

Cyclops for comprehensions

Two supported formats

  1. Typed 'do notation' builder
  2. Untyped do notation builder

To extend the available functionality see How to extend for Comprehension support to other types

For more details on Cyclops For Comprehensions see For Comprehensions explained

Do Notation

List<Integer> list= Arrays.asList(1,2,3);

Stream<Integer> stream = Do.add(list)
							.yield(i-> i +2).unwrap();
			
									
	
assertThat(Arrays.asList(3,4,5),equalTo(stream.collect(Collectors.toList())));

Yield, Filter and 'and' take curried functions

(That is a chain of single input parameter functions)

	Stream<Integer> stream = Do.add(asList(20,30))
							   .with(i->asList(1*i,2+i,3-i))
							   .yield(i-> j -> i + j+2);

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.

Filtering

	List<Integer> list= Arrays.asList(1,2,3);
	Stream<Integer> stream = Do.add(list)
							.filter(a -> a>2)
							.yield(a -> a +2);
			
									
	
	assertThat(Arrays.asList(5),equalTo(stream.collect(Collectors.toList())));
Clone this wiki locally