This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| enum Hairstyle { | |
| LONG, | |
| SHORT | |
| } | |
| class Person { | |
| static final double LONG_RATIO = 0.4; | |
| static final int MAX_AGE = 100; | |
| static final int MAX_HEIGHT = 200; | |
| public final Hairstyle hair; | |
| public final int age; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static Person[] generatePeople(int total) { | |
| Random r = new Random(10); | |
| Person[] people = new Person[total]; | |
| for (int i = 0; i < total; i++) { | |
| people[i] = new Person( | |
| r.nextDouble()<LONG_RATIO ? Hairstyle.LONG : Hairstyle.SHORT, | |
| (int)(r.nextDouble() * MAX_HEIGHT), | |
| (int)(r.nextDouble() * MAX_AGE)); | |
| } | |
| return people; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Benchmark | |
| public double shortHairYoungsterHeight() { | |
| double averageAge = Arrays.stream(people) | |
| .filter(p -> p.hair == Hairstyle.SHORT) | |
| .mapToInt(p -> p.age) | |
| .average().getAsDouble(); | |
| return Arrays.stream(people) | |
| .filter(p -> p.hair == Hairstyle.SHORT) | |
| .filter(p -> p.age < averageAge) | |
| .mapToInt(p -> p.height) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Benchmark | |
| public double volleyballStars() { | |
| return Arrays.stream(people) | |
| .map(p -> | |
| new Person(p.hair, p.age + 1, p.height)) | |
| .filter(p -> p.height > 198) | |
| .filter(p -> p.age >= 18 && p.age <= 21) | |
| .mapToInt(p -> p.age) | |
| .average().getAsDouble(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const http = require("http"); | |
| const span = require("ansispan"); | |
| require("colors"); | |
| http.createServer(function (request, response) { | |
| response.writeHead(200, {"Content-Type": "text/html"}); | |
| response.end(span("Hello Graal.js!".green)); | |
| }).listen(8000, function() { console.log("Graal.js server running at http://127.0.0.1:8000/".red); }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| git clone https://github.com/graalvm/graalvm-demos | |
| cd graalvm-demos/scala-days-2018/scalac-native/scala-substitutions | |
| sbt package | |
| cd ../ | |
| $GRAALVM_HOME/bin/native-image -cp $SCALA_HOME/lib/scala-compiler.jar:$SCALA_HOME/lib/scala-library.jar:$SCALA_HOME/lib/scala-reflect.jar:$PWD/scalac-substitutions/target/scala-2.12/scalac-substitutions_2.12-0.1.0-SNAPSHOT.jar \ | |
| -H:SubstitutionResources=substitutions.json,substitutions-2.12.json \ | |
| -H:ReflectionConfigurationFiles=scalac-substitutions/reflection-config.json \ | |
| -H:Class=scala.tools.nsc.Main \ | |
| -H:Name=scalac |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from random import random | |
| from math import log | |
| rate = 3 | |
| result = [] | |
| for i in range(1000): | |
| result += [-1/rate * log(random())] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import polyglot | |
| # Create an R function and save it to python variable | |
| rcode = "function(x, l) ks.test(as.vector(x), 'pexp', l)$p.value" | |
| kstest = polyglot.eval(string=rcode, language="R") | |
| # invoke the R function with arguments that are Python objects | |
| pvalue = kstest(result, rate) | |
| print("The p-value of the test is ", pvalue) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # load the R package lattice and open a window for plotting | |
| polyglot.eval(string="library(lattice); awt()", language="R") | |
| # Create R function that draws a histogram | |
| plot = polyglot.eval(string="function(y) {print(histogram(~as.vector(y), main='Hello from Python to R', xlab='Python List')) }", language="R") | |
| # invoke the R function from Python | |
| plot(result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| svg() | |
| print(histogram(...)) | |
| svgCode <- svg.string() | |
| png('/path/to/image.png') | |
| print(histogram(...)) | |
| dev.off() |