Skip to content

Instantly share code, notes, and snippets.

View loicdescotte's full-sized avatar

Loïc Descotte loicdescotte

View GitHub Profile
@loicdescotte
loicdescotte / node_redis.md
Last active September 8, 2020 09:45 — forked from leommoore/node_redis.md
Node - Redis

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...
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 03:50
ZIO change stream framing. Example : delimit by lines
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 / ZioQueuePullPush.scala
Last active February 20, 2020 10:20
ZIO queue example
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 November 20, 2019 12:54
Referential Transparency example with Try and IO
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 September 30, 2019 13:23
Accumulate errors with Either and Cats (based on Cats documentation but with Error ADT and NonEmptyChain)
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 / App.scala
Last active September 3, 2019 15:34
Slinky React Hooks example
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 / fpmax.scala
Created March 3, 2019 09:41 — forked from jdegoes/fpmax.scala
FP to the Max — Code Examples
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 January 31, 2019 15:46
ReasonML : drop element in list
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 January 15, 2019 09:55
Compose errors (or nullables) and async functions in Scala, Kotlin, Typescript
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