Skip to content

Instantly share code, notes, and snippets.

@fsarradin
fsarradin / non_strict.scala
Created September 9, 2022 08:02
Non-strict evaluation in Scala
/**
* Try to solve this puzzle only by modifying the function [[doubleMessage]].
* All checks should succeed.
*
* This exercise must be run in a worksheet.
* - One is available on the Web with Scastie (https://scastie.scala-lang.org)
* - One is available in IntelliJ IDEA
* (https://www.jetbrains.com/help/idea/work-with-scala-worksheet-and-ammonite.html)
*
* Click on "> Run". See the results in the console. And try to solve this
import org.apache.kafka.common.serialization.Serde
import org.apache.kafka.streams.kstream.{Named, ValueTransformerWithKey}
import org.apache.kafka.streams.processor.ProcessorContext
import org.apache.kafka.streams.scala.StreamsBuilder
import org.apache.kafka.streams.scala.kstream.KStream
import org.apache.kafka.streams.state.{
KeyValueBytesStoreSupplier,
KeyValueStore,
Stores
}
@fsarradin
fsarradin / pseudobin.scala
Created January 24, 2022 13:11
pseudobin
import scala.util.{Failure, Success, Try}
/**
* Tools to manage pseudo-binary data.
*
* A pseudo-binary data, or ''pseudobin''. Is a (almost) readable binary
* format. It is not as optimized of pure binary format, but it is
* easier to analyse and more optimized than the fully readable formats
* like JSON, CSV, or XML. pseudobin is designed to be a predictable
* format.
import io.univalence.data.{Stock, StockKey, StockKeyBinarySerde, StockBinarySerde}
import java.time.Instant
import java.time.temporal.ChronoUnit
import org.openjdk.jmh.annotations._
import scala.util.Random
abstract class BaseKeyValueStoreBenchmark {
val products = List("café", "chocolat", "miel")
@fsarradin
fsarradin / TextMain.scala
Created April 2, 2021 10:21
Text exploration with Spark Core
package io.univalence.aboutfp
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.SparkSession
object TextMain {
def main(args: Array[String]): Unit = {
val spark = SparkSession.builder()
.master("local[*]")
.appName(getClass.getSimpleName)
@fsarradin
fsarradin / Currency.java
Last active December 20, 2020 15:53
Deep nested Avro schema
/**
* Autogenerated by Avro
*
* DO NOT EDIT DIRECTLY
*/
package test.entity;
@org.apache.avro.specific.AvroGenerated
public enum Currency implements org.apache.avro.generic.GenericEnumSymbol<Currency> {
EUR, USD ;
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"enum\",\"name\":\"Currency\",\"namespace\":\"test.entity\",\"symbols\":[\"EUR\",\"USD\"]}");
@fsarradin
fsarradin / ServiceConfigurationLoader.scala
Created December 4, 2020 17:12
A configuration loader to that is able to get data from different sources with different key case schemas (zio-config 1.0.0-RC28)
import java.lang.{Boolean => JBoolean}
import scala.util.{Failure, Success, Try}
import com.typesafe.config._
import zio.config.PropertyTree.{Leaf, Record, Sequence}
import zio.config.{ConfigSource, PropertyTree, ReadError}
import zio.{IO, config}
/**
* Load a configuration from different sources based on the given
* configuration schema.
@fsarradin
fsarradin / Expression.scala
Created November 13, 2020 09:51
Arithmetic expression
/**
* Arithmetic expression representation.
*
* In this expressions you can only have a unique variable.
* eg. Mult(Var, Add(Const(2), Var)) represents X * (2 + X)
*/
sealed trait Expression
object Expression {
// represent a variable
case object Var extends Expression
@fsarradin
fsarradin / build.sbt
Created February 26, 2020 20:44
Read a fixed length format file by using ZIO, ZStream, and Magnolia
name := "zkafka"
version := "0.1"
scalaVersion := "2.13.1"
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % "1.0.0-RC17",
"dev.zio" %% "zio-streams" % "1.0.0-RC17",
"com.propensive" %% "magnolia" % "0.12.6"
@fsarradin
fsarradin / DottyDerivation.scala
Created March 29, 2019 11:35
Typeclass derivation in Dotty
import scala.reflect._
import scala.compiletime._
import scala.compiletime.Shape._
enum Maybe[+A] derives ToJson {
case Just(value: A)
case Nope
}