Skip to content

Instantly share code, notes, and snippets.

View shelajev's full-sized avatar

Oleg Šelajev shelajev

View GitHub Profile
@shelajev
shelajev / Person.java
Created June 26, 2018 12:39
Person.java
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;
@shelajev
shelajev / generatePeople.java
Created June 26, 2018 12:40
generatePeople.java
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;
@shelajev
shelajev / shortHairYoungsterHeight.java
Created June 26, 2018 12:41
shortHairYoungsterHeight.java
@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)
@shelajev
shelajev / volleyballStars.java
Created June 26, 2018 12:42
volleyballStars.java
@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();
}
@shelajev
shelajev / app.js
Created August 15, 2018 08:59
Running tests in a container started with test-containers.
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); });
@shelajev
shelajev / native-scalac.sh
Last active August 30, 2018 10:16
native-scalac.sh
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
@shelajev
shelajev / random-numbers.py
Created October 10, 2018 18:35
random-numbers.py
from random import random
from math import log
rate = 3
result = []
for i in range(1000):
result += [-1/rate * log(random())]
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)
# 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)
svg()
print(histogram(...))
svgCode <- svg.string()
png('/path/to/image.png')
print(histogram(...))
dev.off()