This file contains 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
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) |
This file contains 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
<dependencies> | |
<dependency> | |
<groupId>org.graalvm.sdk</groupId> | |
<artifactId>graal-sdk</artifactId> | |
<version>1.0.0-rc14</version> | |
</dependency> | |
</dependencies> |
This file contains 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 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() |
This file contains 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
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") |
This file contains 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
# 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 |
This file contains 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
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. |
This file contains 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
// 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 file contains 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
// 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]) |
This file contains 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
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) |
This file contains 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
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 |
OlderNewer