Skip to content
johnmcclean-aol edited this page Nov 27, 2015 · 2 revisions

cyclops-javaslang features

Support for more Javaslang monads

(via AnyM and for-comprehensions)

  • List
  • Array
  • Vector
  • Stack
  • Queue
  • HashSet
  • Stream
  • Option
  • Lazy
  • Either
  • Try
  • Future
     AnyM<String> upperCase =  Javaslang.anyMonad(Array.of("hello world"))
		                        .map(String::toUpperCase)

Javaslang native for-comprehensions

		Future<String> future = Future.of(this::loadData);
		Future<List<String>> results1 = Do.add(future)
 									      .monad(List.ofAll("first","second"))
 									      .yield( loadedData -> localData-> loadedData + ":" + localData )
 									     .unwrap();
		
		System.out.println(results1.get());

//List(loaded:first, loaded:second)

Memoization backed by pluggable caches

               Cache<Object, Integer> cache = CacheBuilder.newBuilder()
			       .maximumSize(1000)
			       .expireAfterWrite(10, TimeUnit.MINUTES)
			       .build();
	
		Cacheable<Integer> cacheable = (key,fn)->  { 
					try {
						return cache.get(key,()->fn.apply(key));
					} catch (ExecutionException e) {
						 throw ExceptionSoftener.throwSoftenedException(e);
					}
		};
		
		Function2<Integer,Integer,Integer> s = memoizeBiFunction( (a,b)->a + ++called,
										cacheable);
		assertThat(s.apply(0,1),equalTo(1));
		assertThat(s.apply(0,1),equalTo(1));
		assertThat(s.apply(0,1),equalTo(1));
		assertThat(s.apply(1,1),equalTo(3));
		assertThat(s.apply(1,1),equalTo(3));

StreamUtils and asynchronous streaming

The JDK StreamUtils functionality has bee ported for Javaslang Streams

  • batching & windowing
  • Stream manipulation
  • time based operators (onePer, xPer)
  • Zipping
  • HotStreams
  • Async Streams
  • Recover / failure handing
StreamUtils.hotStream(Stream.range(0,Integer.MAX_VALUE)
					.take(100)
					.peek(v->value=v)
					.peek(v->latch.countDown())
					.peek(System.out::println)
					,exec)
					.connect()
					.take(100)
					.forEach(System.out::println);

Clone this wiki locally