Skip to content

Instantly share code, notes, and snippets.

View justinhj's full-sized avatar

Justin Heyes-Jones justinhj

View GitHub Profile

Fibers

Fibers are an abstraction over sequential computation, similar to threads but at a higher level. There are two ways to think about this model: by example, and abstractly from first principles. We'll start with the example.

(credit here is very much due to Fabio Labella, who's incredible Scala World talk describes these ideas far better than I can)

Callback Sequentialization

Consider the following three functions

@maurges
maurges / Main.purs
Created April 6, 2020 10:22
Mozilla example extension number 1 rewritten in purescript
module Main where
import Prelude
import Effect (Effect)
import Vanilla.Dom.Document (body)
import Vanilla.Dom.Element (setStyle)
main :: Effect Unit
main =
import $ivy.`org.typelevel::cats-effect:0.10-22efb61`, cats._, cats.implicits._, cats.effect._, scala.concurrent.duration._
def putStr(msg: String): IO[Unit] = IO(print(msg))
def putStrLn(msg: String): IO[Unit] = IO(println(msg))
def readLn: IO[String] = IO(Console.readLine)
val now: IO[Long] = IO(System.currentTimeMillis)
def time[A](ioa: IO[A]): IO[(FiniteDuration, A)] = for {
start <- now
@aduermael
aduermael / dockerTunnel.md
Created August 17, 2016 21:22
Connect to remote Docker engine through SSH tunnel

Bind remote docker.sock with SSH tunnel:

ssh -nNT -L /tmp/docker.sock:/var/run/docker.sock  <USER>@<IP> &

Set environment variable for local Docker client:

export DOCKER_HOST=unix:///tmp/docker.sock
{-# language KindSignatures #-}
{-# language PolyKinds #-}
{-# language DataKinds #-}
{-# language TypeFamilies #-}
{-# language RankNTypes #-}
{-# language NoImplicitPrelude #-}
{-# language FlexibleContexts #-}
{-# language MultiParamTypeClasses #-}
{-# language GADTs #-}
{-# language ConstraintKinds #-}
@barrucadu
barrucadu / coapplicative.hs
Created March 24, 2015 23:38
Coapplicative functors in Haskell
-- Because of the type of 'from', 'Coaplicative' must correspond to "non-empty containers", for the usual hand-wavy definition of "container".
class Functor f => Coapplicative f where
from :: f a -> a
separate :: f (Either a b) -> Either (f a) (f b)
instance Coapplicative Identity where
from (Identity a) = a
separate (Identity (Left a)) = Left (Identity a)
separate (Identity (Right b)) = Right (Identity b)
package scalax.collection
import scala.collection.mutable.ListBuffer
/** FoldTransformers and the views based on them are a Scala
* adaptation, and to some degree an extension, of Rich Hickey's
* transducers for Clojure. They show that the concepts can be
* implemented in a type-safe way, and that the implementation is
* quite beautiful.
*/
object FoldingViews {