Skip to content

Instantly share code, notes, and snippets.

@szoio
szoio / dependent-types.scala
Created June 9, 2020 04:50
Dependent Types
trait DependentValue{
type V
val value: V
}
//object DependentValue {
// def apply[A](a: A): DependentValue { type V = A } = new DependentValue {
// type V = A
// val value = a
// }
@szoio
szoio / hlist.scala
Created June 9, 2020 04:47
Shapeless Intro
import shapeless._
val oneHello = 1 :: "hello" :: Nil
val oneHello = 1 :: "hello" :: HNil
final case class User(name: String, age: Int)
val genericUser = Generic[User]
type GenericUserType = genericUser.Repr
@szoio
szoio / shapeless-typeclass.scala
Created June 9, 2020 04:46
Shapeless Typeclass Example
import shapeless._
import scala.{:: => :::}
import cats.implicits._
trait CsvDecoder[A] {
def decode(value: List[String]): Option[A]
}
val columnSplitter = """,(?=([^\"]*\"[^\"]*\")*[^\"]*$)""".r
@szoio
szoio / magnolia.scala
Created June 9, 2020 04:44
Magnolia Sample
import magnolia._
import scala.language.experimental.macros
import scala.util.Try
final case class User(name: String, age: Int, admin: Boolean)
trait CsvDecoder[A] {
def decode(value: List[String]): Either[Throwable, A]
}
@szoio
szoio / MonadT.scala
Last active May 25, 2020 10:16
Generic Monad Transformer
import cats._
import cats.effect.IO
import cats.implicits._
case class MonadT[F[_], G[_], A](value: F[G[A]])
object MonadT {
implicit def monad[F[_], G[_]](implicit MF: Monad[F], MG: Monad[G], TG: Traverse[G]): Monad[({ type T[A] = MonadT[F, G, A]})#T] = {
type T[A] = MonadT[F, G, A]
new Monad[T] {
@szoio
szoio / immutableUpdate.js
Created November 11, 2018 11:39
Immutable JavaScript object updater (simple implementation)
import equal from 'deep-equal'
export function immutableUpdate( obj, change ) {
const keys = Object.keys( change )
if ( keys.length === 0 ) {
return obj
}
const key = keys[ 0 ]
const value = change[ key ]
if ( keys.length === 1 ) {
@szoio
szoio / EventSourcable.scala
Last active September 17, 2018 04:18
Event Sourcing Interpreter
package stephenzoio.kafka_tests.shared.eventsrc
import java.util.UUID
import cats._
import cats.implicits._
import cats.data.{EitherK, WriterT}
import cats.free.Free
import stephenzoio.kafka_tests.shared.free.FreeOp
@szoio
szoio / EventSourcing.scala
Created February 23, 2017 11:17
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