Skip to content

Instantly share code, notes, and snippets.

@jooyunghan
Created May 12, 2015 20:51
Show Gist options
  • Save jooyunghan/4d03ecc0f22464d47bb0 to your computer and use it in GitHub Desktop.
Save jooyunghan/4d03ecc0f22464d47bb0 to your computer and use it in GitHub Desktop.
Some examples using FunctionalJava
package com.jooyunghan;
import fj.P2;
import fj.data.List;
import fj.data.Stream;
import static fj.P.p;
import static fj.Show.*;
import static fj.data.List.list;
import static fj.data.Stream.iterate;
import static fj.data.Stream.stream;
/**
* User: jooyunghan
* Date: 5/13/15 4:54 AM
*/
public class FunctionalJavaExample {
public static void main(String[] args) {
final List<Integer> a = list(1, 2, 3).map(i -> i + 42);
listShow(intShow).println(a); // [43,44,45]
// combined into a single line
listShow(intShow).println(list(1, 2, 3).map(i -> i + 42)); // [43,44,45]
List<Integer> mappedList = list(1, 2, 3).map(n -> {
System.out.println(n);
return n + 1;
});
Stream<Integer> mappedStream = stream(1, 2, 3).map(n -> {
System.out.println(n);
return n + 1;
});
Stream<Integer> fibonacci = iterate(p2 -> p(p2._2(), p2._1() + p2._2()), p(0, 1)).map(P2.__1());
streamShow(intShow).println(fibonacci.take(10));
fibonacci.take(10).foreachDoEffect(System.out::println);
int sum = fibonacci.takeWhile(n -> n < 4000000).filter(n -> n % 2 == 0).foldLeft1((x, y) -> x + y);
System.out.println(sum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment