Skip to content

Instantly share code, notes, and snippets.

Avatar

Loïc Descotte loicdescotte

View GitHub Profile
@loicdescotte
loicdescotte / Forcomptran.md
Last active Jul 19, 2021
Scala for comprehension translation helper
View Forcomptran.md

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...

@loicdescotte
loicdescotte / HelloZioHttp.scala
Last active Mar 18, 2021
ZIO HTTP basic example
View HelloZioHttp.scala
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 {
View ZIOPreludeValidation.scala
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._
View node_redis.md

Node - Redis

Redis is a simple key, value pair database. The common commands include:

Data StructureCommands
StringsSET, GET, DEL, APPEND, DECR, INCR...
HashsHSET, HGET, HDEL, HGETALL...
ListsLPUSH, LREM, LTRIM, RPOP, LINSERT...
SetsSADD, SREM, SMOVE, SMEMBERS...
View deno-files-example.ts
const config = {
typeDirs: [
{ type: "pdf", directory: "documents" },
{ type: "png", directory: "images" },
{ type: "mp3", directory: "music" },
],
};
type Config = typeof config;
type TypeDirs = Config["typeDirs"];
@loicdescotte
loicdescotte / zioSplitLines.scala
Last active May 2, 2020
ZIO change stream framing. Example : delimit by lines
View zioSplitLines.scala
import zio._
import zio.stream._
import zio.console._
val streamIn = Stream.fromIterable(Set("first line\nsecond line\nthird ", "line"))
// or ZSink.splitDelimiter("\n")
val streamOut = streamIn.transduce(ZSink.splitLines).flatMap(chunk => Stream.fromChunk(chunk))
val runtime = new DefaultRuntime {}
runtime.unsafeRun(streamOut.map(_.replaceAll("\n","")).foreach(i => putStrLn(i)))
View ZioQueuePullPush.scala
import zio._
import zio.console._
import zio.stream._
import zio.duration._
object ZioQueuePullPush extends zio.App {
// c.f. ZIO type aliases https://zio.dev/docs/overview/overview_index#type-aliases
val result: URIO[Clock with Console, Unit] = for {
queue <- Queue.bounded[Int](100)
@loicdescotte
loicdescotte / PlayDynamicSource.scala
Last active Jan 20, 2020
Play Framework Dynamic Akka Streams Source
View PlayDynamicSource.scala
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 / RT.scala
Last active Nov 20, 2019
Referential Transparency example with Try and IO
View RT.scala
import scala.util._
import cats.effect._
//See https://impurepics.com/posts/2018-04-01-referential-transparency.html for RT definition
println("-----------Try--------------")
println("-----------This will print only 1 line : --------------")
val t = Try(println(1+1))
for {
@loicdescotte
loicdescotte / accumulateErrors.scala
Last active Sep 30, 2019
Accumulate errors with Either and Cats (based on Cats documentation but with Error ADT and NonEmptyChain)
View accumulateErrors.scala
import cats.implicits._
import cats.data._
import NonEmptyChain._
case class Name(value: String)
case class Age(value: Int)
case class Person(name: Name, age: Age)
trait Error
case object ParsingError extends Error