Skip to content

Instantly share code, notes, and snippets.

@NRBPerdijk
NRBPerdijk / EventSerializerRegistry.scala
Last active September 29, 2017 13:07
EventUpcasting in Scala with Akka Persistence and Lagon Play-Json
object EventSerializerRegistry extends JsonSerializerRegistry {
override def serializers: immutable.Seq[JsonSerializer[_]] = Vector(
JsonSerializer(Json.format[ActualItemPurchasedEvent])
)
private val addDefaultDiscount = JsPath.json.update((JsPath \ "item" \ "discount").json.put(JsNumber(0)))
private val convertCustomerToClient =
JsPath.json.update(
//add the path client \ clientId to the json object, then copy the value of customerId into it
(JsPath \ "client" \ "clientId").json.copyFrom((JsPath \ "customerId").json.pick)
<dependencies>
<dependency>
<groupId>org.graalvm.sdk</groupId>
<artifactId>graal-sdk</artifactId>
<version>1.0.0-rc14</version>
</dependency>
</dependencies>
@NRBPerdijk
NRBPerdijk / Main.scala
Last active March 22, 2019 13:31
This Main object uses GraalVM to run an R file from Scala
import org.graalvm.polyglot.{Context, Source}
object Main extends App {
// We need to initialise a GraalContext that will do the mediation between the JVM languages and R
val context: Context = Context.newBuilder("R").allowAllAccess(true).build()
// Next, we need to create a Source, which needs to know what language it features and where to find the code.
val source: Source = Source.newBuilder("R", Main.getClass.getResource("funHelloWorld.R")).build()
printHelloWorld <- function() {
# assigning a string to the variable
helloWorld <- "Hello, World!"
#printing the variable
print(helloWorld)
# here we concatenate the created variable with some extra information
# R automatically returns the last statement, so we're done here!
paste(helloWorld, "executed in R")
# Some very impressive R stuff happens here.
# (Well, we're pretending it does, anyway! It's really just a mock...)
# This function returns a data.frame containing a number of
# weather forecasts that we need to bring back to the JVM
magicHappensHere <- function() {
# Omitting mocking code here
(...)
# Like in Scala, the result of the last statement in an R function is its return.
weatherForecasts
generateWeatherForecasts <- function(pathToMagicFile) {
# We're bringing the function contained in the file at the given location into scope
source(paste(pathToMagicFile))
# This returns a dataframe, a way for R to store large quantities of data
# in an ordered manner (kind of like a Database Table...)
weatherForecast <- magicHappensHere()
# Like in Scala, the result of the last statement in an R function is its return.
// We need to initialise a GraalContext that will do the mediation between
// the JVM languages and R
val context: Context = Context.newBuilder("R").allowAllAccess(true).build()
// Next, we need to create a Source which needs to know what language it features
// and where to find the code.
val sourceNoBindings: Source =
Source
.newBuilder("R", Main.getClass.getResource("fun_NoBindingsWeatherForecasts.R"))
.build()
// This function signature is a lot cleaner than the one that doesn't use bindings.
// It is also completely Scala, meaning we do not have to do ANY parsing.
val rMagicWithBindings: String => WeatherForecastList =
context.eval(sourceWithBindings).as(classOf[String => WeatherForecastList])
case class WeatherForecast(
humidity: Percentage,
windForecast: WindForecast,
sunshine: Percentage,
temperature: Temperature,
chanceOfRain: ChanceOfRain
)
case class Temperature(degrees: Int, temperatureScale: String)
case class Percentage(percent: Double)
case class WindForecast(windSpeed: WindSpeed, direction: String)
generateWeatherForecasts <- function(pathToMagicFile) {
# We're bringing the function contained in the file at the given location into scope
source(paste(pathToMagicFile))
# This returns a dataframe, a way for R to store large quantities of data in an ordered
# manner (kind of like a Database Table...)
weatherForecast <- magicHappensHere()
# We use the Scala Domain object provided through GraalVM bindings to get ourselves an
# instance of the Scala wrapper containing a List of WeatherForecast