Skip to content

Instantly share code, notes, and snippets.

@rnorth
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save rnorth/7756c8b5897d0b63beef to your computer and use it in GitHub Desktop.

Select an option

Save rnorth/7756c8b5897d0b63beef to your computer and use it in GitHub Desktop.
Java 8 functional play
import java.math.BigInteger;
import java.util.stream.IntStream;
public class Factorial {
public static void main(String[] args) {
System.out.println(Factorial.factorial(1));
System.out.println(Factorial.factorial(5));
System.out.println(Factorial.factorial(120000));
}
public static BigInteger factorial(final int num) {
return IntStream
.rangeClosed(1, num)
.mapToObj(intValue -> new BigInteger(String.valueOf(intValue)))
.reduce(BigInteger::multiply)
.get();
}
}
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Map;
public class ServerSentEvents {
public static void main(String[] args) throws IOException {
InputStream inputStream = new URL("http://tiny-chat.herokuapp.com/subscribe/everyone").openStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
bufferedReader.lines()
.filter(line -> line.startsWith("data: "))
.map(line -> line.substring(6))
.map(ServerSentEvents::jsonAsMap)
.map(body -> body.get("message"))
.forEach(System.out::println);
}
private static Map jsonAsMap(String json) {
Gson gson = new Gson();
return gson.fromJson(json, Map.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment