Skip to content

Instantly share code, notes, and snippets.

Loïc Descotte loicdescotte

Block or report user

Report or block loicdescotte

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
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 / 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 / zioSplitLines.scala
Last active Nov 7, 2019
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)))
@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 / App.scala
Last active Sep 3, 2019
Slinky React Hooks example
View App.scala
package hello.world
import slinky.web.html._
import slinky.core.facade.Hooks._
import slinky.core.FunctionalComponent
import scala.scalajs.js
import scala.scalajs.js.annotation.JSImport
@JSImport("resources/App.css", JSImport.Default)
@js.native
@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
@loicdescotte
loicdescotte / fpmax.scala
Created Mar 3, 2019 — forked from jdegoes/fpmax.scala
FP to the Max — Code Examples
View fpmax.scala
package fpmax
import scala.util.Try
import scala.io.StdIn.readLine
object App0 {
def main: Unit = {
println("What is your name?")
val name = readLine()
@loicdescotte
loicdescotte / dropElementInList.re
Created Jan 31, 2019
ReasonML : drop element in list
View dropElementInList.re
let dropListElement = (n, list) => {
let rec aux = (i, list) =>
switch list {
| [] => []
| [x, ...xs] => i == n ? xs : [x, ...aux(i + 1, xs)]
};
aux(0, list);
};
@loicdescotte
loicdescotte / compose.kt
Last active Jan 15, 2019
Compose errors (or nullables) and async functions in Scala, Kotlin, Typescript
View compose.kt
import arrow.core.Either
import arrow.core.Either.Companion.left
import arrow.core.Either.Companion.right
import arrow.core.Option
import arrow.core.Some
import arrow.core.flatMap
import arrow.instances.either.monad.binding
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
@loicdescotte
loicdescotte / EitherOps.scala
Last active Dec 17, 2018
Either ops to accumumate errors (and symmetric right method)
View EitherOps.scala
import scala.util._
implicit class EitherLeftOps[L](eithers: Seq[Either[L,_]]){
def collectLefts: Seq[L] = eithers.collect {
case Left(l) => l
}
}
implicit class EitherRightOps[R](eithers: Seq[Either[_,R]]){
def collectRights: Seq[R] = eithers.collect {
You can’t perform that action at this time.