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
@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`
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 / 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
@debasishg
debasishg / cloudsql-proxy.ts
Created May 17, 2021 15:17 — forked from ddunkin/cloudsql-proxy.ts
Pulumi functions to setup and add a Google Cloud SQL Proxy sidecar to a Kubernetes deployment
import * as pulumi from '@pulumi/pulumi';
import * as gcp from '@pulumi/gcp';
import * as k8s from '@pulumi/kubernetes';
import * as k8sInputApi from '@pulumi/kubernetes/types/input';
const serviceAccountKeys = new Map<string, gcp.serviceAccount.Key>();
/**
* Creates a service account and key, and sets the cloudsql.client role in IAM.
*/
import java.net.URI
import cats.effect._
object Main extends IOApp {
override def run(args: List[String]): IO[ExitCode] =
RedisClient.makeWithURI[IO](new URI("http://localhost:6379")).use { cmd =>
import cmd._
val result = for {
@debasishg
debasishg / refined_runtime_validation.scala
Created July 23, 2020 15:26
Incorporating compile time and runtime validation using refinement types in Scala
import cats.data.ValidatedNec
import cats.implicits._
import eu.timepit.refined._
import eu.timepit.refined.api.Refined
import eu.timepit.refined.auto._
import eu.timepit.refined.numeric._
import eu.timepit.refined.collection._
object person {

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.