This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Set(Set, fromDistinctAscList, member) | |
import System.IO.Unsafe(unsafePerformIO) | |
import System.Environment (getArgs) | |
import System.Exit (exitFailure) | |
import Control.Monad (when) | |
import Data.Tree | |
oneStep :: String -> [String] | |
oneStep = filter isValidWord . generateCandidates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Set(Set, fromDistinctAscList, member) | |
import System.IO.Unsafe(unsafePerformIO) | |
import System.Environment (getArgs) | |
import System.Exit (exitFailure) | |
import Control.Monad (when) | |
import Data.Tree | |
oneStep :: String -> [String] | |
oneStep = filter isValidWord . generateCandidates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package reactive.event | |
import scala.collection.mutable.{Buffer,ListBuffer} | |
import Event.Callback | |
trait Event[A] { | |
protected[event] val callbacks: Buffer[Callback[A]] = ListBuffer() | |
def register(cb: Callback[A]): Event[A] = { callbacks += cb; this } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Reader { | |
def pure[C,A](a: A): Reader[C,A] = (_: C) => a | |
def sequence[C,A](rs: List[Reader[C,A]]): Reader[C,List[A]] = (c: C) => | |
rs.foldLeft(List[A]()) { (accu,reader) => accu :+ reader(c) } | |
def filter[C,A](xs: List[A])(p: A => Reader[C,Boolean]): Reader[C,List[A]] = (c: C) => { | |
val bs: List[Reader[C,Boolean]] = xs.map(p) | |
xs.foldLeft(List[A]()) { (accu,x) => if (p(x)(c)) accu :+ x else accu } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.language.implicitConversions | |
case class State[S,R](f: S => (R,S)) { | |
def apply(s: S): (R,S) = f(s) | |
def calculate(s: S): R = f(s)._1 | |
def perform(s: S): S = f(s)._2 | |
def map[Q](g: R => Q): State[S,Q] = State(s => { | |
val (res,_) = f(s) | |
(g(res),s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scalaz._ | |
import Scalaz._ | |
object CVersion { | |
def wc(cs: TraversableOnce[Char]): (Int,Int,Int) = { | |
var numLines,numWords,numChars = 0 | |
var inWord = false | |
cs.foreach { char => | |
numChars += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scalaz.std.stream._ | |
import scalaz.std.tuple._ | |
import scalaz.syntax.bifunctor._ | |
def romanize(number: Int) = { | |
val numerals = Seq( ( "M", 1000), ("CM", 900) | |
, ( "D", 500) , ("CD", 400) | |
, ( "C", 100) , ("XC", 90) | |
, ( "L", 50) , ("XL", 40) | |
, ( "X", 10) , ("IX", 9) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Arrow | |
import Data.Monoid | |
-- An implementation of FizzBuzz using monoids | |
main :: IO () | |
main = mapM_ print $ take 10 . map (show &&& fizzbuzz) $ [0..] | |
-- the actucal fizzbuzz function constructed by 2 helpers below | |
fizzbuzz :: Int -> String | |
fizzbuzz = makeFizzBuzz [ 3 %?? "Fizz", 5 %?? "Buzz", 7 %?? "Bazz"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// An implementation of FizzBuzz using monoids | |
import scalaz._, Scalaz._ | |
def makeFizzBuzz[A: Monoid](ps: Iterable[Int => A]): Int => A = | |
(i: Int) => ps.toList.foldMap(_.apply(i)) | |
def moduloMatch[A:Monoid](m: Int)(v: A): Int => A = | |
(i: Int) => (i % m == 0) ?? v | |
val fizzbuzz = makeFizzBuzz { Seq( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Maybe | |
import System.Environment | |
import Control.Applicative | |
import Control.Monad | |
import Control.Monad.Logic | |
type Queen = (Int,Int) | |
type Board = [Queen] | |
queens :: MonadPlus m => Int -> m Board |
OlderNewer