Skip to content

Instantly share code, notes, and snippets.

// val skunkCore = "0.3.2"
// val zioCats = "3.3.0"
// val zioStreams = "2.0.0"
// val skunkCore = "org.tpolecat" %% "skunk-core" % V.skunkCore
// val zioCats = "dev.zio" %% "zio-interop-cats" % V.zioCats
// val zioStreams = "dev.zio" %% "zio-streams" % V.zioStreams
object Cats {
import skunk._
import skunk.implicits._
import skunk.codec.all._
def task(id: Int): Runnable = () =>
{
println(s"${Thread.currentThread().getName()} start-$id")
Thread.sleep(10000)
println(s"${Thread.currentThread().getName()} end-$id")
}
@dsebban
dsebban / fs2_blogpost.md
Created October 17, 2020 18:27
A Streaming library with a superpower: Functional Programming

A Streaming library with a superpower: Functional Programming

Scala has a very unique streaming library called FS2 (Functional Streams for Scala). This library embodies all the advantages of functional programming(FP). By understanding its design goals you will get exposure to the core ideas that makes FP so appealing.

FS2 has one central type:

Stream[Effect,Output]

@dsebban
dsebban / fizzbuzz-monoid.sc
Last active April 17, 2019 17:06
FizzBuzz using monoids
import $ivy.`org.typelevel::cats-core:1.6.0`, cats.implicits._
def rule[A](n: Int, default: A): Int => Option[A] = x => if ( x % n == 0) default.some else None
val fizz = rule(3,"Fizz")
val buzz = rule(5,"Buzz")
val boom = rule(7,"Boom")
def fizzBuzz(rules: List[Int => Option[String]]): List[Int] => List[String] = xs => {
val rule: Int => Option[String] = rules.foldMap(identity)
xs.map(x => rule(x).getOrElse(x.show))
@dsebban
dsebban / flip.sc
Last active February 18, 2019 07:59
haskell flip in Scala
def flip[A,B,C]: (A => B => C) => (B => A => C) = f => x => y => f(y)(x)
def f: Int => String => Int= a => b => a + b.toInt
flip(f)("1")(2)
def ++ : String => String => String = a => b => a + b
flip(++)("Hello")("World")
@dsebban
dsebban / makeSafe.sc
Last active January 23, 2019 09:46
Lift a non safe function to a safe one
def notSafeFunc: String => Int = _.toInt
notSafeFunc("1") // 1
notSafeFunc("a") // java.lang.NumberFormatException: For input string: "a"
import $ivy.`org.typelevel::cats-core:1.5.0`
import cats.implicits._
import cats.Applicative
def makeSafe[A,B,F[_]: Applicative](f: A => B)(implicit F: Applicative[F]): A => F[B] = a => F.lift(f)((F.pure(a)))
@dsebban
dsebban / Predicate.sc
Last active January 17, 2019 10:41
Example of Contravariant Predicate
import $ivy.`org.typelevel::cats-core:1.5.0`
import cats.Contravariant
import cats.implicits._
{
trait Predicate[A] {
def getPredicate(a:A): Boolean
}
object Predicate {
def apply[A](f: A => Boolean): Predicate[A] = new Predicate[A]{ def getPredicate(a: A) = f(a) }
@dsebban
dsebban / create-table-dynamo.sc
Last active August 26, 2018 13:20
create dynamo db table using akka-streams
import $ivy.`com.lightbend.akka::akka-stream-alpakka-dynamodb:0.20`
import akka.actor.ActorSystem
import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory
import akka.stream.ActorMaterializer
import akka.stream.alpakka.dynamodb.scaladsl._
import akka.stream.alpakka.dynamodb.impl.DynamoSettings
import akka.stream.scaladsl._
import scala.concurrent.{ Await , Future }
import scala.concurrent.duration._
@dsebban
dsebban / fs2_dedupe.sc
Created August 23, 2018 13:28
copy/paste into ammonite REPL to run : http://ammonite.io/#Ammonite-REPL
import $ivy.`co.fs2:fs2-core_2.12:0.10.5` , fs2._
def dedupeConsecutiveWhen[F[_], I](f: (I, I) => Boolean): Pipe[F, I, I] = in =>
in.zipWithNext.map {
case (curr, Some(next)) if f(curr, next) => (None: Option[I])
case (curr, Some(next)) => Some(curr)
case (curr, None) => Some(curr)
}.unNone
Stream(1,1,2,2,3,4,5,2).through(dedupeConsecutiveWhen(_ == _ )).toList
@dsebban
dsebban / fs2-pull.md
Last active April 11, 2024 20:48
Understanding fs2 `Pull`

Undertsanding the Pull type from fs2

From the scaladocs

class Stream[+F[_], +O] extends AnyVal
  • A stream producing output of type O
  • May evaluate F effects.