Skip to content

Instantly share code, notes, and snippets.

View pchiusano's full-sized avatar

Paul Chiusano pchiusano

View GitHub Profile
@pchiusano
pchiusano / Machine.scala
Created November 16, 2012 04:25
Machines with upstream communication
package fpinscala.streamingio
/**
Machine takes binary type constructor now.
Requests are an F[X,Y] and an X, for some X and Y.
*/
trait Machine[F[_,_],A] {
def vary[F0[X,Y] >: F[X,Y], A0 >: A] =
this.asInstanceOf[Machine[F0,A0]]
@pchiusano
pchiusano / Machines.hs
Created December 5, 2012 06:14
Machines are monads
data Machine k a where
Emit :: a -> Machine k a -> Machine k a
Await :: k r -> (r -> Machine k a) -> Machine k a -- ignoring fallback for now, not relevant
Halt :: Machine k a
(++) :: Machine k a -> Machine k a -> Machine k a
Halt ++ m2 = m2
(Emit h t) ++ t = Emit h (t ++ m2)
(Await req recv) ++ t = Await req (\res -> recv res ++ t)
@pchiusano
pchiusano / Future.scala
Created January 31, 2013 23:11
Trampolined Future type.
package scalaz.concurrent
import Future._
trait Future[+A] {
def flatMap[B](f: A => Future[B]): Future[B] = this match {
case Now(a) => More(() => f(a))
case Later(listen) => BindLater(listen, f)
case More(force) => BindMore(force, f)
case BindLater(listen,g) => More(() =>
@pchiusano
pchiusano / Process.hs
Created February 2, 2013 02:03
Stream transducer library
{-# Language ExistentialQuantification, GADTs #-}
module Control.Process where
import Data.Monoid
import Prelude hiding (zip, zipWith)
import Control.Applicative
import System.IO
data Process f a = Halt
@pchiusano
pchiusano / Throw.scala
Last active December 13, 2015 16:48
Trampoline data type implemented using exceptions
package fpinscala.iomonad
trait Throw[+A] {
import Throw._
@annotation.tailrec
final def run: A = this match {
case Done(a) => a
case More(thunk) => force(thunk).run
}
-- Some edits to the F type
data F :: Timing -> * -> * where
...
{- For each unique `a` value taken on by the first series, call the
given function, passing it the `a` wrapped in an `F`, and the
subset of issues for each date that have that `a`.
sector :: F (P Day) String
-- could very well do something using the k
@pchiusano
pchiusano / Future.scala
Created July 16, 2013 03:57
Experimental, nonblocking implementation of Future.chooseAny. First thread to finish notifies listener.
def chooseAny[A](h: Future[A], t: Seq[Future[A]]): Future[(A, Seq[Future[A]])] = {
Async { cb =>
// The details of this implementation are a bit tricky, but the general
// idea is to run all `fs` in parallel, returning whichever result
// becomes available first.
// To account for the fact that the losing computations are still
// running, we construct special 'residual' Futures for the losers
// that will first return from the already running computation,
// then revert back to running the original Future.
@pchiusano
pchiusano / profile.scala
Created August 1, 2013 01:04
Some informal scalaz-stream performance testing code.
package scalaz.stream
import scalaz.concurrent.Task
import scalaz.syntax.traverse._
import scalaz.std.list._
import Process.Process1
import collection.immutable.{IndexedSeq, Vector}
object profile extends App {
@pchiusano
pchiusano / Actors.scala
Created September 3, 2013 18:12
Actor memory consistency
package woot
import scalaz.concurrent._
/*
Test to see if `@volatile` is required for mutable fields closed over by an `Actor`
using `Strategy.Sequential`. As a test, we have an actor that closes over a integer
counter, which it increments for each message it receives. If memory effects are
visible to subsequent messages, the count will be equal to the number of messages
sent to the actor.
@pchiusano
pchiusano / Window.hs
Created October 28, 2013 19:14
O(1) sliding window with just a Monoid, not a Group. Via Edward Kmett, copied from http://lpaste.net/88182 for posterity.
module Window where
import Data.Monoid
data Window a = Window [a] a [a] deriving (Show,Read)
null :: Window a -> Bool
null (Window [] _ []) = True
null _ = False