Skip to content

Instantly share code, notes, and snippets.

@SystemFw
SystemFw / Fib.scala
Last active December 19, 2023 19:38
Deriving tail recursive Fibonacci
/*
Here's a derivation for a tail recursive fibonacci function.
Let's start from the Maths definition:
fib(0) = 0
fib(1) = 1
fib(n) = fib(n - 1) + fib(n - 2)
*/
// Let's translate the structure, leaving the recursive case unspecified:
@SystemFw
SystemFw / Free conversation.md
Last active October 17, 2023 09:57
Explaining some of the mechanics of interpretation of Free programs

Balaji Sivaraman @balajisivaraman_twitter

Hi all, I need some help understanding a piece of Doobie code from the examples. It is the StreamingCopy one: (https://github.com/tpolecat/doobie/blob/series/0.4.x/yax/example/src/main/scala/example/StreamingCopy.scala). I am using a modified version of the fuseMap2 example from that file. Here’s how I’ve modified it for my requirements:

  def fuseMap[F[_]: Catchable: Monad, A, B](
      source: Process[ConnectionIO, A],
      sink: Vector[A] => ConnectionIO[B],
      delete: ConnectionIO[Unit]
  )(
 sourceXA: Transactor[F],
@SystemFw
SystemFw / example.scala
Last active May 24, 2023 15:13
Running fs2 streams in parallel and collect their result in sequence, with queues
object Example {
import cats._, implicits._
import cats.effect._
import fs2._
import scala.concurrent.ExecutionContext
// start N streams concurrently, and stream their results in order
// e.g. download a file from a server in N parts concurrently, and stream it
abstract class Channel[F[_], A] {
@SystemFw
SystemFw / Example.elm
Created July 20, 2022 11:53
Can Stream I/O represent transformational programs?
-- The assumption here is that stream based I/O is a better fit for
-- distributed systems than monadic I/O, because there is a greater
-- separation between logic, which can be idempotent and distributed
-- via something like differential dataflow, and actions, which can
-- be represented like ports and actuators
-- We already know that stream based I/O is a good fit for reactive programs
-- but what about more "imperative" ones that fit monads well? I took an imperative
-- example and tried to express it via stream I/O
@SystemFw
SystemFw / Ex.scala
Last active December 23, 2021 12:34
Update fields of a case class with a Diff generically, with support for nesting
object Ex {
import Lib._
case class Person(name: String, age: Int)
case class PersonOpt(name: Option[String], age: Option[Int])
def mergePerson(p: Person, po: PersonOpt) =
Person(po.name.getOrElse(p.name), po.age.getOrElse(p.age))
case class Contact(person: Person, phone: String)
@SystemFw
SystemFw / Conversions.scala
Last active April 23, 2021 07:53
Typed schema conversion with shapeless
object Conversions {
import cats._, implicits._, data.ValidatedNel
import mouse._, string._, option._
import shapeless._, labelled._
private type Result[A] = ValidatedNel[ParseFailure, A]
case class ParseFailure(error: String)
trait Convert[V] {
@SystemFw
SystemFw / Example.scala
Last active October 29, 2020 21:47
Using shapeless to check exhaustiveness of Golden tests
// stemming from
// https://gitter.im/typelevel/general?at=5f987f8161007f7d1b9b4b92
// The idea is
// read the JSON files in a directory
// try to decode each of them as Event which could be either One or Two, we don't know this at compile time
// serialize the values we got, again could be either One or Two and compare it with the content of the JSON file
// verify that there was at least a JSON file for One and Two (exhaustivity check)
import shapeless._
@SystemFw
SystemFw / Ex.scala
Created October 26, 2018 20:39
Get instances of a typeclass `TC[_]` for all cases of a Coproduct [shapeless]
import shapeless._
@annotation.implicitNotFound("Make sure all cases of ${C} have an instance of ${TC}")
trait CopTCs[C <: Coproduct, TC[_]] {
type Out <: HList
def result: Out
}
object CopTCs {
type Aux[C <: Coproduct, TC[_], O <: HList] = CopTCs[C, TC] { type Out = O }
def apply[C <: Coproduct, TC[_]](implicit ev: CopTCs[C, TC]) = ev
@SystemFw
SystemFw / Test.scala
Created January 21, 2018 17:27
Recursively increment every Int in a case class by a given runtime quantity
object Test {
import inc._
case class Foo(i: Int, s: String)
case class Bar(f: Int, a: Foo, n: Int)
val res = Bar(0, Foo(1, "hello"), 2).incBy(2)
//res0: Test.Bar = Bar(2,Foo(3,hello),4)
}
@SystemFw
SystemFw / Lib.scala
Last active June 7, 2019 05:15
Shapeless: derive JDBC Results for arbitrary case classes
import shapeless._ // requires.shapeless
import cats._, implicits._, data.Kleisli // requires.cats
import cats.sequence._ //requires kittens
import cats.effect.IO //requires cats-effect
// ofc, uses "-Ypartial-unification" and kind-projector
case class Result() // replace with the JDBC equivalent
case class DB(val r: Result) {
def nextInt: IO[Int] = ??? //IO(g.nextInt)