Skip to content

Instantly share code, notes, and snippets.

View kailuowang's full-sized avatar

Kai(luo) Wang kailuowang

View GitHub Profile
trait IsoK[F[_], G[_]] { self =>
def from[A](fa: F[A]): G[A]
def to[A](ga: G[A]): F[A]
def flip: IsoK[G, F] = new IsoK[G, F] {
def from[A](ga: G[A]): F[A] = self.to(ga)
def to[A](fa: F[A]): G[A] = self.from(fa)
}
@kailuowang
kailuowang / googleClient.scala
Last active March 27, 2021 03:08
Create Google Cloud Client from key file stored on S3
import cats.effect.{Resource, Sync}
import cats.implicits._
import com.google.api.gax.core.FixedCredentialsProvider
import com.google.auth.oauth2.GoogleCredentials
import com.google.cloud.language.v1.{LanguageServiceClient, LanguageServiceSettings}
object GoogleClient {
case object S3ObjectNotFound extends RuntimeException

Keybase proof

I hereby claim:

  • I am kailuowang on github.
  • I am kailuowang (https://keybase.io/kailuowang) on keybase.
  • I have a public key ASAwfpUUT9j_iHqjCdCz_cyjnswRisAinmoqwPo0YpMgzQo

To claim this, I am signing this object:

@kailuowang
kailuowang / poc_scalacheck_free_laws.scala
Created April 19, 2019 18:15
PoC of scalacheck free definition of laws
trait RuleSetDSL[P] {
type Check1[A1, A2] = Checkable1[A1, A2, P]
type Check2[A1, A2, A3] = Checkable2[A1, A2, A3, P]
implicit def law[A1, A2](s : String, f: A1 => IsEq[A2])(implicit toP1: Check1[A1, A2]): (String, P) =
(s, toP1(f))
@kailuowang
kailuowang / build.sbt
Created April 2, 2019 17:19
min example of failed ++2.11.12 commandAlias
import Dependencies._
ThisBuild / scalaVersion := "2.12.8"
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "com.example"
ThisBuild / organizationName := "example"
lazy val root = (project in file("."))
.aggregate(mA, mB)
.settings(
import com.stripe.rainier.repl._
import com.stripe.rainier.core._
val data = Gamma(0.5, 3).param.sample()
val ap = for {
a <- Normal(0.5, 0.05).param
b <- Normal(3, 0.1).param
d <- Gamma(a, b).fit(data)
import cats.implicits._
import implicits._
import java.time.Instant
import cats.Functor
import org.openjdk.jmh.annotations.{Benchmark, Scope, State}
case class Foo[A](a: A)
@kailuowang
kailuowang / blog.MD
Last active July 4, 2017 14:14
How functional programming enable composition.

Divide and conquer has been a core strategy in software engineering. We decompose our systems into smaller modules with focused, simple functionalities and compose them back to provide richer features. In a complex modular system, developers regularly have to dedicate a significant portion of code and effort to such composition.

Needless to say, it provides major benefits to be able to compose and decompose computation with ease. This is where FP, i.e. Functional Programming mainly focuses on. For developers who wonder what difference FP makes, this blog post aims to give a taste of how it enables more natural composition. I am going to focus on one particular and yet common factor that makes computation composition cumbersome in imperative programming - effects.

Simple function composition

Let's start with two functions with the return type of one matching the input type of the other. That is, given

@kailuowang
kailuowang / HFunctors.md
Last active May 12, 2017 01:15
Two tales of high order functors.

There are two possible definitions of a somewhat higher order Functors: HFunctorA:

trait HFunctorA[H[_[_],_]] {
  def map[F[_], G[_], A](h: H[F, A])(f: F ~> G): H[G, A]
}

and HFunctorB:

trait HFunctorB[H[_[_]]] {
import cats.{Applicative, Unapply}
import cats.instances.all._
import shapeless._
object Test {
trait Foo[L <: HList]
object Foo extends MkFoo