Skip to content

Instantly share code, notes, and snippets.

@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

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)
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)
@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(
@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))

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 / 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
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)
}