Skip to content

Instantly share code, notes, and snippets.

@SystemFw
SystemFw / Logging.scala
Last active July 13, 2017 13:22
Logging failures on Task
import scalaz.{-\/, \/-}
import scalaz.concurrent.Task
import scalaz.syntax.monad._
/*
* Easily adaptable to Monix, cats, fs2, cats-effect and so on
*/
object Logger {
sealed trait Level
object Level {
@SystemFw
SystemFw / semiauto.scala
Created July 13, 2017 13:15
Shapeless semiautomatic derivation
object Module {
import shapeless._
sealed trait Decode[A] {
def decode(s: String): Option[A]
}
object Decode {
def dummy[A]: Decode[A] = new Decode[A] {
def decode(s: String) = Option.empty[A]
}
@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 / Diff.scala
Last active August 14, 2017 23:19
Shapeless: update some fields of a class with a Diff, generically
import shapeless._, labelled._, ops.hlist._
object Update extends Poly2 {
implicit def all[A, B, R1 <: HList, R2 <: HList](
implicit oldGen: LabelledGeneric.Aux[A, R1],
newGen: LabelledGeneric.Aux[B, R2],
z: ZipWith.Aux[R1, R2, Update.type, R1]) =
at[A, B] { (old, maybeNew) =>
val oldRepr = oldGen.to(old)
val newRepr = newGen.to(maybeNew)
@SystemFw
SystemFw / FT.scala
Created August 7, 2017 13:51
[DRAFT] Layered algebras in Free and Final Tagless
object FT {
import cats._, implicits._
trait MoveV[F[_]] {
def up(c: Int): F[Int]
def down(c: Int): F[Int]
}
object MoveV {
implicit def interpreter: MoveV[Id] = new MoveV[Id] {
def up(c: Int) = c + 1
@SystemFw
SystemFw / text.txt
Last active July 23, 2018 18:39
[DRAFT] Free vs Final tagless
So, Free vs Final Tagless.
The first consideration is that the reason why Free is so popular in
Scala, is that Final Tagless didn't quite work (due to ambiguous
implicits), in the current, subtype based encoding of typeclasses used
by cats and scalaz, whereas Free did. However, both libraries are now
addressing the problem, and will accommodate final tagless (also known
as finally tagless, tagless final, mtl style) better. Another
important point is that they are roughly equivalent in power, but some
things might be easier or harder with one of them than with the other.
The thing with Free is: you are reifying a monadic computation into a
@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 / PolyWithArgs.scala
Last active June 7, 2019 05:14
Shapeless Poly with explicit arguments
object Args {
// it requires the cats, shapeless, and kittens libraries
import cats._, implicits._
import cats.sequence._
import shapeless._, labelled._
import shapeless.syntax.singleton._
// Gitter question:
// it masks fields of a record according to an explicit argument
@SystemFw
SystemFw / Test.scala
Last active June 7, 2019 05:15
Shapeless: Convert between any two compatible case classes, selecting a subset of the fields
object Test {
case class User(name: String, age: Int)
case class UserDTO(name: Option[String], age: Option[Int])
import conversions._
def a = User("John", 24).convertTo[UserDTO](Set("name"))
// res0: Test.UserDTO = UserDTO(Some(John),None)
case class WrongFieldNames(surname: Option[String], age: Option[Int])
@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] {