Skip to content

Instantly share code, notes, and snippets.

View goseign's full-sized avatar

Xu Bin goseign

  • Home
  • Mountain view, CA
View GitHub Profile
@goseign
goseign / shared-state-in-fp.md
Created April 22, 2021 22:34 — forked from gvolpe/shared-state-in-fp.md
Shared State in pure Functional Programming

Shared State in pure Functional Programming

Newcomers to Functional Programming are often very confused about the proper way to share state without breaking purity and end up having a mix of pure and impure code that defeats the purpose of having pure FP code in the first place.

Reason why I decided to write up a beginner friendly guide :)

Use Case

We have a program that runs three computations at the same time and updates the internal state to keep track of the

@goseign
goseign / CE3.md
Created April 22, 2021 22:03 — forked from zmccoy/CE3.md

Cats-Effect 3 Guide to Migration Guides

Cats-Effect 3 Migration Links

Typelevel Library Versions latest on CE3

@goseign
goseign / Connect.scala
Created November 11, 2020 01:38 — forked from erangaeb/Connect.scala
spray-json handle more than 22 fields
import spray.json.{DefaultJsonProtocol, JsBoolean, JsObject, JsString, JsValue, RootJsonFormat}
case class Document(field1: String, field2: String, field3: String, field4: String, field5: String, field6: String,
field7: String, field8: String, field9: String, field10: String, field11: String, field12: String,
field13: String, field14: String, field15: String, field16: String, field17: String, field18: String,
field19: String, field20: String, field21: String, field22: String, field23: String, field24: String,
approved: Boolean)
object DocumentProtocol extends DefaultJsonProtocol {
@goseign
goseign / fibers.md
Created October 31, 2020 21:29 — forked from djspiewak/fibers.md

Fibers

Fibers are an abstraction over sequential computation, similar to threads but at a higher level. There are two ways to think about this model: by example, and abstractly from first principles. We'll start with the example.

(credit here is very much due to Fabio Labella, who's incredible Scala World talk describes these ideas far better than I can)

Callback Sequentialization

Consider the following three functions

type Callback<A> = (a: A) => void;
/**
* Delays stuff for ensuring fairness.
*/
export function yieldRunLoop(): Promise<void> {
const fn: (cb: (() => void)) => void = typeof setImmediate !== 'undefined'
? setImmediate
: cb => setTimeout(cb, 0)
return new Promise(fn)
@goseign
goseign / KafkaInZioQueuesFibers.scala
Created September 19, 2019 22:18 — forked from mtsokol/KafkaInZioQueuesFibers.scala
Build your own Kafka in ZIO - Queues & Fibers
import zio._
import zio.console._
import zio.duration.Duration
import scala.util.Random
import java.util.concurrent.TimeUnit
object Main extends App {
override def run(args: List[String]): ZIO[Main.Environment, Nothing, Int] = program.fold(_ => 1, _ => 0)
@goseign
goseign / ValidationResultLib.scala
Created April 11, 2019 06:11 — forked from dzikowski/ValidationResultLib.scala
A micro-library for data validation over cats.Monad and cats.data.EitherT
package com.softwaremill.monadvalidation.lib
import cats.Monad
import cats.data.EitherT
import scala.language.higherKinds
trait ValidationResultLib[M[_]] {
type ValidationResult[F, S] = EitherT[M, F, S]
@goseign
goseign / Scheduling.scala
Created February 22, 2019 18:15 — forked from hochgi/Scheduling.scala
scala scheduling with akka
import akka.actor.{ActorSystem, Scheduler}
import com.astoncap.util.concurrent.Async._
import scala.concurrent.{ExecutionContext, Future, Promise}
import scala.concurrent.duration.{Duration, FiniteDuration}
import scala.util.Try
trait Scheduling {
implicit val scheduling: Scheduling = this
@goseign
goseign / EventSourcing.scala
Created October 23, 2018 23:42 — forked from szoio/EventSourcing.scala
Event Sourcing with Free Monads
package eventsourcing
import java.time.Instant
import cats._
import cats.data.Coproduct
import cats.free.{Free, Inject}
import cats.implicits._
import doobie.imports._
import fs2.Stream
final case class User private[domain] (userId: UserId, createdAt: Instant, name: String, email: Email) {
def applyEvent(userEvent: UserEvent): Try[User] = userEvent match {
case _: UserCreated => Failure(new IllegalStateException("User already created. Event cannot be applied."))
case event: NameUpdated => Success(copy(name = event.newName))
case event: EmailUpdated => Success(copy(email = event.newEmail))
}
def process(userCommand: UserCommand): Try[List[UserEvent]] = userCommand match {
case _: CreateUser => Failure(new IllegalStateException("User already created. Command cannot be processed."))