Skip to content

Instantly share code, notes, and snippets.

View rockymadden's full-sized avatar
:octocat:
Setting status

Rocky Madden rockymadden

:octocat:
Setting status
  • UiPath
  • Pale Blue Dot
View GitHub Profile
@miikka
miikka / Simple.hs
Created August 24, 2010 19:07
Simple Haskell Twitter OAuth example
{-# 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
@SethTisue
SethTisue / ch19.scala
Created May 6, 2011 23:39
The Seasoned Schemer, chapter 19, page 176
// 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)
@gclaramunt
gclaramunt / Functor.scala
Created August 11, 2011 18:11
Functor, Applicative Functor, Monad typeclasses in Haskell and their (simplistic) Scala translation
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]{
@jedws
jedws / conwaydef.scala
Created September 5, 2011 22:58
alternate Functional Conway with def instead of function vals
/**
* 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)
@mjg123
mjg123 / truth-table.clj
Created September 11, 2011 21:27
Truth-table macro in clojure
(defn bool-combinations [vs]
(loop [vs vs
a '({})]
(if (empty? vs)
a
(recur
(rest vs)
(flatten
(map
#(list
@robertgreiner
robertgreiner / spellcheck.py
Created December 28, 2011 03:19
Peter Norvig's spell check example
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
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)
}
@lstoll
lstoll / JettyLauncher.scala
Created September 11, 2011 07:13
Scalatra on Heroku
/*
* 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]) {
@akisaarinen
akisaarinen / FunctionalPong.scala
Created September 22, 2012 14:29
Functional Pong
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
}
@akisaarinen
akisaarinen / ImperativePong.scala
Created September 22, 2012 14:27
Imperative Pong
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")