Skip to content

Instantly share code, notes, and snippets.

View gvolpe's full-sized avatar
🤓
https://leanpub.com/u/gvolpe

Gabriel Volpe gvolpe

🤓
https://leanpub.com/u/gvolpe
View GitHub Profile
@SystemFw
SystemFw / Ex.scala
Last active November 3, 2018 17:41
Encoding existentials via abstract types (1) and higher-ranks (2)
// This is the traditional encoding, using abstract type members
// to encode existentials
object AbstractExistentials {
trait E {
type X
val x: X
def f(p: X): String
}

Preliminaries

type Void <: Nothing
type ¬[-A] = A => Void
type ¬¬[+A] = ¬[¬[A]]
type [+A, +B] = Either[A, B]
type [+A, +B] = Tuple2[A, B]
@wogan
wogan / Backoff.scala
Created December 19, 2017 17:28
Retry helper methods & DSL for Monix Tasks.
package retry
import scala.concurrent.duration.{Duration, FiniteDuration}
import scala.util.Random
import scala.concurrent.duration._
object Backoff {
val none: Backoff = _ => Duration.Zero
@neko-kai
neko-kai / quantified.scala
Last active April 2, 2019 13:53
Tagless final for ZIO via quantified constraints
package quantified
import cats.Monad
import scala.language.implicitConversions
/**
* C[_] constraint applied to type F[_, _] quantified in first parameter, i.e.
*
* {{{
@SystemFw
SystemFw / RankN shift-and-shiftback.md
Last active June 1, 2019 03:15
Cats-effect, blocking, RankN-types.

cats-effect

The cats-effect project defines a purely functional effect type (IO[A]), and associated typeclasses defining its behaviour. The ones we care about for this example are:

trait Sync[F[_]] extends MonadError[F, Throwable] {
   def delay[A](a: => A): F[A]
   ...
}
import cats.data.Kleisli
import cats.effect.{ Concurrent, Sync }
import cats.effect.concurrent.MVar
import cats.implicits._
import cats.{ Applicative, Functor, Monad }
// Let's start with our dsl
// First we need to interact with a console
trait Console[F[_]] {
@ChristopherDavenport
ChristopherDavenport / raceN.scala
Last active December 13, 2019 13:54
An Implementation of a Safe Multi-value Race
object RaceN {
import cats.implicits._
import cats.effect._
import cats.effect.concurrent._
import cats.data.NonEmptyList
// Use with atleast 3, otherwise you should prefer Concurrent.race
def raceN[F[_]: Concurrent, A](x1: F[A], x2: F[A], x3: F[A], xs: F[A]*): F[Either[NonEmptyList[Throwable], A]] = {
for {
deferred <- Deferred[F, A]
@alexandru
alexandru / ExecutorServiceWrapper.scala
Last active March 17, 2021 23:52
Wrapping a scala.concurrent.ExecutionContext into java.concurrent.ExecutorService
import concurrent.{Future, Await, Promise, ExecutionContext}
import java.util.concurrent.{Future => JavaFuture, TimeUnit, Callable, CancellationException, ExecutorService}
import concurrent.duration._
import scala.util.Success
import scala.util.Failure
import scala.util.control.NonFatal
import scala.collection.JavaConverters._
import concurrent.duration.TimeUnit
import java.{util => jutil}
@non
non / laws.md
Last active February 20, 2022 00:26
I feel like conversations around laws and lawfulness in Scala are often not productive, due to a lack of rigor involved. I wanted to try to be as clear and specific as possible about my views of lawful (and unlawful) behavior, and what I consider a correct and rigorous way to think about laws (and their limits) in Scala.

Laws

A law is a group of two or more expressions which are required to be the same. The expressions will usually involve one or more typed holes ("inputs") which vary.

Some examples:

x.map(id) === x
@sellout
sellout / OpClasses.idr
Last active August 20, 2022 01:18
Defining ad-hoc polymorphism using dependently-typed implicits.
-- This illustrates (most of) a new(?) encoding of ad-hoc polymorphism using
-- dependent types and implicits.
--
-- Benefits of this approach:
-- • can have multiple “ambiguous” instances without requiring things like
-- Haskell’s newtypes to distinguish them;
-- • can disambiguate instances with minimal information, rather than having to
-- know some arbitrary instance name as you would with a system like Scala’s
-- implicits;
-- • implementers don’t need to know/provide the full family of instances – they