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
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
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
# 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
private val path = Main.getClass.getResource("fun_MagicHappensHere.R").getPath | |
Try(rNoBindingsWeatherForecasts(path)) match { | |
case Failure(f) => print(f) | |
case Success(s) => | |
// turning the Java Map into an immutable Scala Map, same for the Java List. | |
val resultAsScala: Map[String, List[_]] = | |
s.asScala.toMap.map(entry => entry._1 -> entry._2.asScala.toList) | |
// We need to do a bunch of nasty casting, because the returntype is not uniform | |
val humidities: List[Int] = resultAsScala("humidity").asInstanceOf[List[Int]] |
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
/* | |
* Exposing bindings is an interesting way to share functionality between languages. | |
* This command makes an instance of the Domain class available under the "Domain" | |
* accessor. | |
*/ | |
context.getBindings("R").putMember("Domain", new Domain) | |
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
/* | |
* Our Domain object functions as a factory for our domain-related classes. | |
* It has methods that create new instances of these classes, which can then safely be used from another Context. | |
*/ | |
class Domain { | |
def weatherForecastList(): WeatherForecastList = WeatherForecastList(List()) | |
def percentage(percent: Int): Percentage = Percentage(percent: Double) | |
def chanceOfRain(chance: Percentage): ChanceOfRain = ChanceOfRain(chance: Percentage) | |
def temperature(degrees: Int, temperatureScale: String): Temperature = |
OlderNewer