Skip to content

Instantly share code, notes, and snippets.

Seth Tisue SethTisue

Block or report user

Report or block SethTisue

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@SethTisue
SethTisue / solve.scala
Created May 6, 2011
Google Code Jam: solves goo.gl/NyPoz in O(n log n)
View solve.scala
// solves goo.gl/NyPoz in O(n log n)
def solveAll(credit: Int, items: Seq[Int]): (Int, Int) = {
// build a map from items to their indices in the input (lists of length 1 or 2)
val indices: Map[Int, Seq[Int]] =
items.zipWithIndex
.groupBy(_._1)
.mapValues(_.map(_._2 + 1))
def solve(item: Int): Option[(Int, Int)] =
if(item * 2 == credit)
@SethTisue
SethTisue / StateMachine.scala
Created May 6, 2011
sample DFA (deterministic finite automaton) implementation in Scala
View StateMachine.scala
// implements this DFA:
// en.wikipedia.org/wiki/File:DFAexample.svg
// as described here:
// en.wikipedia.org/wiki/Finite-state_machine#Acceptors_and_recognizers
// We have a few representational choices here:
// - We can have an explicit EOF, or not.
// (I chose yes.)
// - We can represent success and failure as states, or not.
@SethTisue
SethTisue / 99problems.scala
Created May 6, 2011
99 Scala problems — a few solutions
View 99problems.scala
// 1
def last[A](xs: List[A]): A =
if(xs.tail.isEmpty) xs.head
else last(xs.tail)
// 2
def penultimate[A](xs: List[A]): A =
xs match {
@SethTisue
SethTisue / ch19.scala
Created May 6, 2011
The Seasoned Schemer, chapter 19, page 176
View ch19.scala
// 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)
@SethTisue
SethTisue / ch13.scala
Created May 6, 2011
Seasoned Schemer, chapter 13
View ch13.scala
// page 37
/*
def intersect[T](set1: List[T], set2: List[T]): List[T] =
set1 match {
case Nil => Nil
case car :: cdr =>
if (set2.contains(car))
car :: intersect(cdr, set2)
else
@SethTisue
SethTisue / .gitignore
Last active Dec 7, 2016
parse iTunes library XML, compile stats. shows how to use Scala's XML pull API, by putting some DSL-y goodness on top of it
@SethTisue
SethTisue / io.scala
Created May 6, 2011
abandoned attempt at a Haskell-style IO monad in Scala
View io.scala
// I started with code from:
// http://apocalisp.wordpress.com/2011/01/10/functional-programming-for-beginners/
// the filter stuff doesn't really make sense, since IO has no zero, but I was
// interested in fooling with it anyway and seeing what worked and what didn't.
// since I did this Apocalisp has done an official version and added it to Scalaz
object io {
@SethTisue
SethTisue / koch.hs
Created May 6, 2011
Koch snowflake code in Haskell. an exercise from Hudak's book The Haskell School of Expression
View koch.hs
-- how to run: ghci -i Koch.hs Picture Region Draw Shape Animation SOE EnableGUI
module Koch where
-- infrastructure
import SOE hiding (Region)
import Picture
import Animation (turn)
import EnableGUI
@SethTisue
SethTisue / buffer.scala
Created May 24, 2011
eliminating a mutable buffer
View buffer.scala
import java.io._
def userStream =
new BufferedReader(
new InputStreamReader(
new FileInputStream("/etc/passwd")))
// imperative style
def linesI(s: BufferedReader): List[String] = {
val buf = new collection.mutable.ListBuffer[String]
View Dynamic.scala
import scala.util.DynamicVariable
class Workspace {
def run(s: String) { println(s) }
def report(s: String) = 10
def dispose() { println("disposed") }
}
def expect[T](expected: T)(actual: T) {
assert(expected == actual, actual.toString)
You can’t perform that action at this time.