Skip to content

Instantly share code, notes, and snippets.

View loicdescotte's full-sized avatar

Loïc Descotte loicdescotte

View GitHub Profile
@loicdescotte
loicdescotte / PlayAkkaSourceQueue.scala
Last active April 26, 2024 07:30
Play and Akka Streams source queue
class Application @Inject() (implicit actorSystem: ActorSystem, exec: ExecutionContext) extends Controller {
implicit val materializer = ActorMaterializer()
val Tick = "tick"
class TickActor(queue: SourceQueue[String]) extends Actor {
def receive = {
case Tick => queue.offer("tack")
}
@loicdescotte
loicdescotte / zquery-caching.scala
Last active January 19, 2024 09:03
#ZIO Query #ZQuery caching
import zio._
import zio.query._
case class DbDepartment(id: Int, name: String)
case class DbEmployee(id: Int, name: String, departmentId: Int)
case class Employee(name: String, departmentName: String)
case class GetDepartement(id: Int) extends Request[Nothing, DbDepartment]
object ZqueryDemo extends ZIOAppDefault {
@loicdescotte
loicdescotte / genericws.scala
Last active January 19, 2024 09:00
Process web services and transform data with type classes #ZIO #Generic #Scala
object TestApp extends App :
import zio.json._
case class Input[A](hits: List[A])
trait Transformer[A,B] :
extension(a: A) def transform: B
object Input :
implicit def decoder[A: JsonDecoder]: JsonDecoder[Input[A]] = DeriveJsonDecoder.gen[Input[A]]
@loicdescotte
loicdescotte / ResultUsage.kt
Last active August 30, 2023 09:25
Kittinunf Result usage example
import com.github.kittinunf.result.*
open class AppError(message: String) : Throwable(message)
class ReadError(message: String) : AppError("Read error " + message)
class WriteError(message: String) : AppError("Write error " + message)
fun main(args: Array<String>) {
val result: Result<Unit, AppError> = readArgs(args).flatMap { writeToConsole(it) }
@loicdescotte
loicdescotte / Forcomptran.md
Last active May 27, 2023 06:27
Scala for comprehension translation helper

Scala for comprehension translation helper

"For comprehension" is a another syntaxe to use map, flatMap and withFilter (or filter) methods.

yield keyword is used to aggregate values in the resulting structure.

This composition can be used on any type implementing this methods, like List, Option, Future...

class PrintService {
def print = println("I'm a real service")
}
trait Services {
val printService = new PrintService()
// put your other services here
}
//for example, a Play controller
@loicdescotte
loicdescotte / PlayDynamicSource.scala
Last active November 6, 2021 00:41
Play Framework Dynamic Akka Streams Source
def streamWithQueue = Action {
implicit val myExecutionContext: ExecutionContext = actorSystem.dispatchers.lookup("queue-context")
val (queue: SourceQueueWithComplete[String], source: Source[String, NotUsed]) = {
Source.queue[String](100, OverflowStrategy.backpressure).preMaterialize
}
// simulate a background process feeding the queue
Future.traverse((1 to 1000).toList) { i =>
@loicdescotte
loicdescotte / extract_mbox_attachments.py
Created August 5, 2021 15:16 — forked from georgy7/extract_mbox_attachments.py
Extract attachments from mbox file.
# Modified.
# Original script source:
# http://blog.marcbelmont.com/2012/10/script-to-extract-email-attachments.html
# Usage:
# Run the script from a folder with file "all.mbox"
# Attachments will be extracted into subfolder "attachments"
# with prefix "n " where "n" is an order of attachment in mbox file.
# ---------------
@loicdescotte
loicdescotte / HelloZioHttp.scala
Last active March 18, 2021 21:28
ZIO HTTP basic example
import zio._
import zhttp.http._
import zhttp.http.HttpError.InternalServerError
import zhttp.service.Server
import zio.clock.Clock
import java.time.DateTimeException
object HelloWorld extends App {
@loicdescotte
loicdescotte / ZIOPreludeValidation.scala
Last active December 20, 2020 19:00
ZIO Prelude validation
case class Name(value: String)
case class Age(value: Int)
case class Person(name: Name, age: Age)
trait Error
case object ParsingError extends Error
case class DataValidationError(message:String) extends Error
import zio._
import zio.prelude._