Skip to content

Instantly share code, notes, and snippets.

View luciferous's full-sized avatar
🏠
Working from home

Neuman Vong luciferous

🏠
Working from home
View GitHub Profile
@luciferous
luciferous / AsyncStream.scala
Last active March 27, 2017 17:16
Too Cool for Spool
package com.twitter.concurrent
import com.twitter.util.{Await, Future}
/**
* Value class to wrap the Cons cell.
*/
protected case class Cons[A](tuple: (A, AsyncStream[A])) extends AnyVal {
def head: A = tuple._1
def tail: AsyncStream[A] = tuple._2
@luciferous
luciferous / mono
Last active August 29, 2015 14:15
Comparison of a polymorphic and monomorphic value class
scala> class Bar(val a: Object) extends AnyVal
defined class Bar
scala> val x = new Bar(new Object)
x: Bar = Bar@5d0b5bcb
scala> :javap -v x
Size 599 bytes
MD5 checksum cc127130545aa10c64d151abbffcae83
Compiled from "<console>"
@luciferous
luciferous / Op.scala
Last active January 20, 2019 15:32
Church-encoded free and operational monad
sealed trait ~>[F[_], G[_]] {
def apply[A](f: F[A]): G[A]
}
trait Monad[M[_]] {
def pure[A](a: A): M[A]
def flatMap[A, B](a: M[A])(f: A => M[B]): M[B]
}
object Monad {
@luciferous
luciferous / README.md
Created June 27, 2014 08:15
Church encoding of Option

Regular Option

sealed trait Option[+A]
case class Some[+A](a: A) extends Option[A]
case object None extends Option[Nothing]

implicit def optionMonad[A](o: Option[A]) =
  new MonadOps[A] {
    def flatMap[B](f: A => Option[B]) = o match {
@luciferous
luciferous / 1.md
Last active August 29, 2015 14:01
Pure functional memoization based on FunWithTypeFuns.

Port of memoization example from FunWithTypeFuns.

scala> memo.MemoExamples.b2i(true)
b=true
res1: Int = 1

scala> memo.MemoExamples.b2i(false)
b=false
res2: Int = 0
@luciferous
luciferous / imonad.scala
Created April 28, 2014 23:26
correct by construction
// Correct by construction.
// Adapted from https://gist.github.com/gelisam/9845116.
trait Opened
trait Closed
sealed trait Transition[S,T,A]
case object Open extends Transition[Closed,Opened,Unit]
case object Read extends Transition[Opened,Opened,String]
case object Close extends Transition[Opened,Closed,Unit]
@luciferous
luciferous / Rec.hs
Last active August 29, 2015 13:56
Recursive problems
{-# LANGUAGE GADTs, InstanceSigs #-}
newtype Service a b = Service { unService :: a -> IO b }
newtype SF a b = SF { unSF :: Bool -> IO (Service a b) }
newtype Filter a b c d = Filter { unFilter :: c -> Service a b -> IO d }
class Profunctor p where
dimap :: Filter a b c d -> p a b -> p c d
@luciferous
luciferous / Profunctor.scala
Last active August 29, 2015 13:56
Generalized profunctor composition
trait Profunctor[F[_, _, _, _], P[_, _]] {
def lmap[A, B, C](f: F[C, A, B, B])(p: P[A, B]): P[C, B]
def rmap[A, B, C](g: F[A, A, B, C])(p: P[A, B]): P[A, C]
def dimap[A, B, C, D](h: F[C, A, B, D])(p: P[A, B]): P[C, D]
}
object Profunctor {
type FilterLike[A, B, C, D] = Filter[A, D, B, C]
private trait FilterPartial[A, B] { type Apply[C, D] = Filter[C, D, A, B] }
@luciferous
luciferous / stack.scala
Created February 20, 2014 09:06
Based on Eric Merten's solution to "A practical Haskell puzzle". http://www.haskell.org/pipermail/haskell-cafe/2011-March/089802.html
trait Category[Cat[-_,+_]] {
def id[A]: Cat[A, A]
def andThen[A, B, C](f: Cat[A, B], g: Cat[B, C]): Cat[A, C]
}
object Category {
implicit object CategoryFunction1 extends Category[Function1] {
def id[A]: A => A = a => a
def andThen[A, B, C](f: A => B, g: B => C): A => C = f andThen g
}
package com.twitter.finagle.util
//import language.{ higherKinds, implicitConversions }
trait Unapply[TC[_[+_]], FA] {
type F[+_]
type A
def TC: TC[F]
def apply(fa: FA): F[A]
}