JShell - Java 9 finally gets a REPL environment

I did a short presetation on JShell - Java9's new REPL environment.

Here's the video (in Bulgarian)

Here's the code for nashorn:

import java.net.http.*;
import java.util.stream.*
import javax.script.*
 
// URL to fetch JSON for weather data for Pravets, Bulgaria
String url = "http://api.openweathermap.org/data/2.5//forecast/daily?q=Pravets,BG&appid=27fc9aa7cc0af902105a9c4c2d97845c&mode=json&units=metric&cnt=7";
 
//java 9 http api
String json = HttpRequest.create(new URI(url)).GET().response().body(HttpResponse.asString()) 
 
// create nashorn engine
ScriptEngine e = new ScriptEngineManager().getEngineByName("js")
 
// expose weather data JSON as global variable to nashorn engine
e.put("str", json)
 
// massage JSON using nashorn and get the max. temp values
double[] values = (double[]) e.eval("Java.to(JSON.parse(str).list.map(function(val) val.temp.max), Java.type('double[]'))")
 
printf(Arrays.toString(values)+"\n")
 
// stat on max. temp values
printf(DoubleStream.of(values).summaryStatistics().toString())

Leave a Reply

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.