Skip to content

Instantly share code, notes, and snippets.

View debasishg's full-sized avatar
🏠
Working from home

Debasish Ghosh debasishg

🏠
Working from home
View GitHub Profile

Thinking differently between frameworks with concrete abstractions (ZIO in Scala) and those with algebras and typeclasses (Haskell / cats & cats-effects in Scala) :-

Using Haskell or Scala cats / cats-effect, which offers libraries based on algebras and typeclasses, we think in terms of the algebra and the appropriate typeclass to model the effect. It's always algebra first - in the following example, we fetch an entity to work with in a domain model. We try fetching from the cache first and if we get a cache miss we reach out for a database based query. With Haskell or cats in Scala, the mantra is using the algebra of an Alternative typeclass.

getAccount = runMaybeT $
      MaybeT (AC.fetchCachedAccount ano) 
  <|> MaybeT (AR.queryAccount ano)
@debasishg
debasishg / gist:d9b6d984df3cc4681eb1
Last active November 23, 2022 06:26
Useful links on Convex Optimization
@debasishg
debasishg / ZKleisli.scala
Created September 28, 2022 15:44
ZIO Kleisli
package zio
case class ZKleisli[-R, +E, -A, +B](
run: A => ZIO[R, E, B]
) { self =>
// compose with a ZIO not lifted into a Kleisli
def mapZIO[R1 <: R, E1 >: E, A1 <: A, C1](bc: B => ZIO[R1, E1, C1]): ZKleisli[R1, E1, A, C1] =
ZKleisli(self.run(_).flatMap(bc))
// Modify the output of the Kleisli function with `f`

Thread Pools

Thread pools on the JVM should usually be divided into the following three categories:

  1. CPU-bound
  2. Blocking IO
  3. Non-blocking IO polling

Each of these categories has a different optimal configuration and usage pattern.

import cats.{Applicative, Functor}
import cats.syntax.all._
/** Is this the proper Scala encoding for the Haskell snippet in comments ? */
/** from : https://doisinkidney.com/pdfs/algebras-for-weighted-search.pdf */
/** with help from @Odomontois */
// newtype Cayley f a = Cayley {runC :: ∀b. f b → f (a, b) }
trait Cayley[F[_], A] {
@debasishg
debasishg / cayley.scala
Last active September 28, 2021 13:28
Encoding for Cayley transform in Scala
import cats.{ Applicative, Functor }
import cats.syntax.all._
/** Is this the proper Scala encoding for the Haskell snippet in comments ? */
/** from : https://doisinkidney.com/pdfs/algebras-for-weighted-search.pdf */
// newtype Cayley f a = Cayley {runC :: ∀b. f b → f (a, b) }
sealed trait Cayley[F[_], A] {
def apply[B](fb: F[B]): F[(A, B)]
}
@debasishg
debasishg / phones.scala
Created September 16, 2021 17:38
Phone numbers out of chess moves
package interview
object DominoInterview extends App {
/**
|1|2|3|
|4|5|6|
|7|8|9|
|*|0|#|
Problem:
@debasishg
debasishg / GDP.scala
Created April 14, 2019 19:41 — forked from erdeszt/GDP.scala
Ghosts of departed proofs in scala (https://github.com/matt-noonan/gdp-paper/releases)
trait Coercible[A, B] {
def coerce(a: A): B
}
def coerce[A, B](a: A)(implicit coerceAB: Coercible[A, B]): B = {
coerceAB.coerce(a)
}
trait Newtype[+T] {
val value: T
@debasishg
debasishg / UnfoldPages.scala
Created June 14, 2021 18:36 — forked from rrodseth/UnfoldPages.scala
Create akka-stream Source from a pagination, using Source.unfoldAsync
// Inspired by a tweet from @trautonen 1/13/2016
// Use Source.unfoldAsync to turn paginated database results into an akka-streams Source
// unfold is the inverse of fold
case class Page[T](pageNumber:Long, totalPages:Long, contents:List[T])
case class Thing(id: Long, name: String = "foo")
val totalPages = 5 //
val pageSize = 3
import org.scalatest._
import org.scalatest.concurrent.ScalaFutures
import scala.concurrent.Future
// from https://github.com/d6y/scalatest-future-failure/blob/master/src/test/scala/example.scala
class ExampleSpec extends FlatSpec with Matchers with ScalaFutures {
sealed trait Boom extends Throwable
final case class ExampleFail() extends Boom