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
{-# LANGUAGE PackageImports #-} | |
{- | |
You need to register your Twitter application at <http://dev.twitter.com/> | |
to get the consumer key and secret needed for OAuth. When connecting to | |
Twitter for the first time, do this: | |
let consumer = Consumer "consumer key" "consumer secret" | |
token <- authenticate |
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
// page 176 | |
// recursive non-consing solution with var | |
def hasTwoInARow[T](lists: List[Any]) = { | |
var previous: Option[Any] = None | |
def recurse(rest: Any): Boolean = | |
rest match { | |
case xs: List[_] => | |
xs.exists(recurse) |
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
trait Functor[T[_]]{ | |
def fmap[A,B](f:A=>B)(ta:T[A]):T[B] | |
} | |
trait Applicative[T[_]] extends Functor[T]{ | |
def pure[A](a:A):T[A] | |
def <*>[A,B](tf:T[A=>B])(ta:T[A]):T[B] | |
} | |
trait Monad[M[_]] extends Applicative[M]{ |
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
/** | |
* A functional Conway's game of life. | |
*/ | |
package object conwaydef { | |
type Coord[A] = (Int, Int) => A | |
type Calculator = Coord[Coord[Boolean] => Boolean] | |
type Size = Int | |
def nextCell(old: Boolean)(mates: Int) = if (mates > 3) false else if (mates == 3) true else (old && mates == 2) |
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
(defn bool-combinations [vs] | |
(loop [vs vs | |
a '({})] | |
(if (empty? vs) | |
a | |
(recur | |
(rest vs) | |
(flatten | |
(map | |
#(list |
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 re, collections | |
def words(text): return re.findall('[a-z]+', text.lower()) | |
def train(features): | |
model = collections.defaultdict(lambda: 1) | |
for f in features: | |
model[f] += 1 | |
return model |
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._, Free._ | |
sealed trait Arithmetic[+A] | |
sealed case class Addition[A](a: Int, b: Int, f: Int => A) extends Arithmetic[A] | |
object Arithmetic { | |
implicit val ArithmeticFunctor: Functor[Arithmetic] = new Functor[Arithmetic] { | |
def map[A, B](fa: Arithmetic[A])(f: A => B) = fa match { | |
case z@Addition(_, _, g) => z.copy(f = f compose g) | |
} |
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
/* | |
* Starts jetty for scalatra programatically | |
* | |
* Replace YourApplicationEndpointFilter with the filter in your application | |
*/ | |
import org.eclipse.jetty.server.Server | |
import org.eclipse.jetty.servlet.{DefaultServlet, ServletContextHandler} | |
object JettyLauncher { | |
def main(args: Array[String]) { |
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 pong.functional | |
sealed trait Event | |
sealed trait Action | |
object Action { | |
case object MoveUp extends Action | |
case object MoveDown extends Action | |
case object ShootMissile extends Action | |
} |
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 pong.imperative | |
sealed trait Event | |
class PongConnection { | |
def isConnected(): Boolean = sys.error("not implemented") | |
def readEvent(): Event = sys.error("not implemented") | |
def moveUp(): Unit = sys.error("not implemented") | |
def moveDown(): Unit = sys.error("not implemented") | |
def shootMissile(): Unit = sys.error("not implemented") |
OlderNewer