Skip to content

Instantly share code, notes, and snippets.

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

Debasish Ghosh debasishg

🏠
Working from home
View GitHub Profile
@ordnungswidrig
ordnungswidrig / state-is-a-fold.clj
Created June 16, 2011 15:50
State is a fold over events
(ns state-is-a-fold
(:use clojure.test))
;;; After all, state is a fold of events. For example let's say the events are a sequence of numbers
;;; and we are folding by addition:
(deftest simple
(let [events [1 5 2 4 3]
state (reduce + events)]
(is (= 15 state))))
@viktorklang
viktorklang / ScalaEnum.scala
Created June 30, 2011 23:12
DIY Scala Enums (with optional exhaustiveness checking)
trait Enum { //DIY enum type
import java.util.concurrent.atomic.AtomicReference //Concurrency paranoia
type EnumVal <: Value //This is a type that needs to be found in the implementing class
private val _values = new AtomicReference(Vector[EnumVal]()) //Stores our enum values
//Adds an EnumVal to our storage, uses CCAS to make sure it's thread safe, returns the ordinal
private final def addEnumVal(newVal: EnumVal): Int = { import _values.{get, compareAndSet => CAS}
val oldVec = get
@jedws
jedws / TreiberStack.scala
Last active July 4, 2016 12:53
Pattern vs Catamorphism? an exercise in different approaches to implementing a simple data-structure
import com.atlassian.util.scala.concurrent.Atomic //https://bitbucket.org/jwesleysmith/atlassian-util-scala/src/tip/src/main/scala/com/atlassian/util/scala/concurrent/Atomic.scala
package patterns {
final class TreiberStack[A] {
private[this] val head = Atomic[Node[A]](End)
@annotation.tailrec
def pop: Option[A] = head.value match {
case l @ Link(a, prev) => if (head.compareAndSet(l, prev)) Some(a) else pop
case _ => None
@krasserm
krasserm / MonadTransformerExamples.scala
Created July 14, 2011 10:32
Scalaz 7 monad transformer examples
import scalaz._
import Scalaz._
object MonadTransformerExamples {
def main(args: Array[String]) = run
def run {
// ------------------------------------------------------
// Combined Option/Option
// ------------------------------------------------------
@gkossakowski
gkossakowski / FactoryManifests.scala
Created August 11, 2011 11:19
Scala Manifests without reflection.
package scala.tools.nsc
package backend.jribble
import scala.collection.mutable
import scala.tools.nsc.transform.{Transform, TypingTransformers}
/**
* Implements 'factorymanifests' compiler phase that provides alternative implementation of
* Manifests that use static factories for Array creation.
*
* Canonical Manifest implementation in Scala uses reflection for generic Array creation.
@runarorama
runarorama / gist:1140578
Created August 11, 2011 19:51
ADTs in Scala
trait Maybe[A] {
def apply[B](just: (=> A) => B, nothing: => B): B
}
object Just {
def apply[A](a: => A): Maybe[A] = new Maybe[A] {
def apply[B](just: (=> A) => B, nothing: => B) = just(a)
}
def unapply[A](a: Maybe[A]): Option[A] = a(Some(_), None)
}
@beastaugh
beastaugh / installing-ghc7.2-osx10.7.md
Created August 24, 2011 21:41
Installing GHC 7.2 on Mac OS X 10.7 Lion

Installing GHC 7.2 on Mac OS X

This is a brief and bare-bones guide to getting GHC 7.2 and the cabal-install tool (the two basic things you'll need to do Haskell development) up and running on a new Mac OS X 10.7 install.

The instructions given here worked for me, but YMMV.

def unexpected : Nothing = sys.error("Unexpected invocation")
// Encoding for "A is not a subtype of B"
trait <:!<[A, B]
// Uses ambiguity to rule out the cases we're trying to exclude
implicit def nsub[A, B] : A <:!< B = null
implicit def nsubAmbig1[A, B >: A] : A <:!< B = unexpected
implicit def nsubAmbig2[A, B >: A] : A <:!< B = unexpected
@corruptmemory
corruptmemory / DBSkeisli.scala
Created October 17, 2011 02:43
Silly example of Kleisli composition of DB operations
/**
* A silly example using Kleisli composition of DB operations
* Based on an idea from Runar Bjarnason found here:
* https://groups.google.com/d/msg/scala-debate/xYlUlQAnkmE/FteqYKgo2zUJ
*
* Uses Scalaz7
*
* @author <a href="mailto:jim@corruptmemory.com">Jim Powers</a>
*/
object Monadic {
@runarorama
runarorama / gist:1292001
Created October 17, 2011 05:39
Regional database connections
object Regional {
case class IOM[M, A](unwrap: IO[A])
case class Q[M](c: Connection)
def iomMonad[M]: Monad[({type λ[α] = IOM[M, α]})#λ] =
new Monad[({type f[a] = IOM[M, a]})#f] {
def pure[A](a: => A) = IOM[M, A](a.point[IO])
def bind[A, B](m: IOM[M, A], f: A => IOM[M, B]) =
IOM(m.unwrap >>= (x => f(x).unwrap))