Skip to content

Instantly share code, notes, and snippets.

View Baccata's full-sized avatar

Olivier Mélois Baccata

View GitHub Profile

Explaining Miles's Magic

Miles Sabin recently opened a pull request fixing the infamous SI-2712. First off, this is remarkable and, if merged, will make everyone's life enormously easier. This is a bug that a lot of people hit often without even realizing it, and they just assume that either they did something wrong or the compiler is broken in some weird way. It is especially common for users of scalaz or cats.

But that's not what I wanted to write about. What I want to write about is the exact semantics of Miles's fix, because it does impose some very specific assumptions about the way that type constructors work, and understanding those assumptions is the key to getting the most of it his fix.

For starters, here is the sort of thing that SI-2712 affects:

def foo[F[_], A](fa: F[A]): String = fa.toString
@Baccata
Baccata / hylo.sc
Last active October 14, 2017 22:12
Stack safe hylomorphism using cats' lazy eval
// Look up ammonite if you're wondering what this syntax is
import $ivy.`org.typelevel::cats-core:1.0.0-MF`
import cats._
import cats.syntax.functor._
import cats.syntax.traverse._
sealed trait Nat[+A]
case object Zero extends Nat[Nothing]
case class Succ[A](a : A) extends Nat[A]
@Baccata
Baccata / Presentation.scala
Created November 20, 2017 10:03
Some toy examples of matryoshka use
package presentation
import matryoshka.data.Fix
import scalaz.Functor
object Presentation extends App {
// 1 : abstract recursion away
@Baccata
Baccata / mill_completion.bash
Created March 2, 2018 08:07
Very basic bash autocompletion support for mill
# See https://debian-administration.org/article/317/An_introduction_to_bash_completion_part_2
__mill()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="$(echo $(mill resolve __ 2>/dev/null ) | sed 's/ / /g')"
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
@Baccata
Baccata / pipo.sc
Last active March 15, 2018 13:11
ammonite | semanticdb experiments
import $ivy.`org.typelevel::cats-core:1.0.1`
import cats.data.NonEmptyList
case class ABC(a : Int, b : Long, c : String)
case class NelWrapper(nel: NonEmptyList[Int])
val w = NelWrapper(NonEmptyList.of(1,2,3,4))
@Baccata
Baccata / after.scala
Last active March 18, 2018 15:01
ammonite wrapping code
package ammonite
package $file
import _root_.ammonite.interp.InterpBridge.{
value => interp
}
import _root_.ammonite.interp.InterpBridge.value.{
exit
}
import _root_.ammonite.main.Router.{
doc,
@Baccata
Baccata / ValueDiscard.scala
Created April 19, 2018 12:31
An option that forces the developer to ascribe the type to discard it
/**
* Function that allows values to be discarded in a visible way
*
* Thanks https://github.com/tabdulradi for the Trick
*/
object ValueDiscard {
/**
@Baccata
Baccata / FoldableFromTraverse.scala
Last active September 11, 2018 12:56
Attempt at implementing foldLeft/foldRight from traverse
abstract class FoldableFromTraverse[F[_]] extends Traverse[F] {
override def foldMap[A, B: Monoid](fa: F[A])(f: A => B): B =
traverse[Const[B, ?], A, B](fa)(a => Const(f(a))).getConst
private def andThenMonoid[A]: Monoid[A => A] = new Monoid[A => A] {
def combine(f: A => A, g: A => A) = f andThen g
def empty: A => A = (a: A) => a
}
@Baccata
Baccata / JavaFuture.scala
Created July 5, 2018 15:24 — forked from Daenyth/JavaFuture.scala
Convert Java futures to cats-effect IO
package teikametrics
import cats.effect.{Sync, Timer}
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.blocking
import cats.syntax.all._
object JavaFuture {
// Alias for more friendly imports
@Baccata
Baccata / vpnconnect.sh
Last active March 25, 2020 00:35
Script to query pwd from lastpass and feed it to the vpn
#!/usr/bin/env bash
HOST="???" # address of the vpn
LASTPASS_ENTRY="???" # entry under which the password is saved in last pass
USERNAME=$(lpass show --sync=auto --username $LASTPASS_ENTRY)
PASSWORD=$(lpass show --sync=auto --password $LASTPASS_ENTRY)
# escaping the special chars in the password to allow sed-ing it
ESC_PWD=$(echo $PASSWORD | sed -e 's/[]\/$*.^[]/\\&/g')