Skip to content

Instantly share code, notes, and snippets.

View earldouglas's full-sized avatar

James Earl Douglas earldouglas

View GitHub Profile
type Graph[A] = Map[A, Set[A]]
def bfs[A](graph: Graph[A], start: A, end: A): Option[List[A]] =
import scala.collection.immutable.ListSet
@annotation.tailrec
def bfsTR(remaining: List[A], visited: ListSet[A]): Option[List[A]] =
remaining match
case Nil => None
case h :: t if visited(h) => bfsTR(t, visited)
case h :: _ if h == end => Some((visited + h).toList)
{-# LANGUAGE GADTs #-}
class IOEffect x where
runIOEffect :: x a -> IO a
data M a where
Pure :: a -> M a
Ap :: M (a -> b) -> M a -> M b
Bind :: M a -> (a -> M b) -> M b
M :: IOEffect x => x a -> M a
@earldouglas
earldouglas / _README.md
Last active October 3, 2015 22:21
APIcon 2014: Real-World Functional Programming

Real-World Functional Programming

May 28, 2014

This code is a companion to the APIcon presentation Real-World Functional Programming by @jearldouglas and @kelleyrobinson. The slides are available here.

In this code, we compare imperative and functional implementations of a withdraw function that might be used in a banking ATM.

Imperative transactions

trait Container[A] {
def has(a: A): Boolean
}
implicit def listContainer[A](xs: List[A]): Container[A] =
new Container[A] {
override def has(a: A): Boolean = xs.contains(a)
}
// -*- mode: Scala;-*-
// Filename: k.scala
// Authors: lgm
// Creation: Wed Dec 28 14:52:27 2011
// Copyright: Not supplied
// Description:
// ------------------------------------------------------------------------
import scala.collection.mutable.HashMap
import scala.util.continuations._
package await
import java.util.concurrent.Callable
import java.util.concurrent.Executors
object BlockingDemo extends App {
val execSvc = Executors.newSingleThreadExecutor()
val worker = new MeaningOfLife()
@earldouglas
earldouglas / RemoteAkka.scala
Created March 20, 2011 02:26
Remotely load and register Akka Actor classes at runtime (see https://github.com/JamesEarlDouglas/akka-remote-class-loading)
package com.earldouglas.remoteakka
import akka.actor.Actor
import akka.actor.Actor._
case object Greeting
case class Jar(val bytes: Array[Byte])
case class RegisterRemote(val name: String, val className: String)
object Runner {