Skip to content

Instantly share code, notes, and snippets.

View filosganga's full-sized avatar

Filippo De Luca filosganga

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

Applied Functional Programming with Scala - Notes

Copyright © 2016-2018 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x
@klaaspieter
klaaspieter / ASS.md
Created June 22, 2017 07:59 — forked from anonymous/ASS.md
Acronyms Seriously Suck - Elon Musk

From time to time, Musk will send out an e-mail to the entire company to enforce a new policy or let them know about something that's bothering him. One of the more famous e-mails arrived in May 2010 with the subject line: Acronyms Seriously Suck:

There is a creeping tendency to use made up acronyms at SpaceX. Excessive use of made up acronyms is a significant impediment to communication and keeping communication good as we grow is incredibly important. Individually, a few acronyms here and there may not seem so bad, but if a thousand people are making these up, over time the result will be a huge glossary that we have to issue to new employees. No one can actually remember all these acronyms and people don't want to seem dumb in a meeting, so they just sit there in ignorance. This is particularly tough on new employees.

That needs to stop immediately or I will take drastic action - I have given enough warning over the years. Unless an acronym is approved by me, it should not enter the SpaceX glossary.

@avakhrenev
avakhrenev / runConcat.scala
Last active January 22, 2019 14:35
fs2 runConcat. Evaluate two streams asynchronously, concating result.
import cats.effect.{Effect, IO}
import fs2._
import scala.concurrent.ExecutionContext
def runConcat[F[_], A](first: Stream[F, A], second: Stream[F, A])(
implicit F: Effect[F],
ec: ExecutionContext): Stream[F, A] = {
type Step = AsyncPull[F, Option[(Segment[A, Unit], Stream[F, A])]]
def readFull(s: Step): Pull[F, A, Unit] =
@johnynek
johnynek / flatMapFromTailRec.scala
Created October 7, 2017 01:55
I hadn't realized (maybe the original paper mentioned) but tailRecM + map is enough to do flatMap (and of course logically flatMap + pure are enough to do tailRecM).
object Monad {
trait Monad[F[_]] {
def pure[A](a: A): F[A]
/**
* We can have a default implementation in terms of tailRecM
* and map
*/
def flatMap[A, B](fa: F[A])(fn: A => F[B]): F[B] = {
def step(first: Option[A]): F[Either[Option[A], B]] =