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 / solve.scala
Created May 6, 2011
Google Code Jam: solves in O(n log n)
View solve.scala
// solves 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]] =
.mapValues( + 1))
def solve(item: Int): Option[(Int, Int)] =
if(item * 2 == credit)
SethTisue / StateMachine.scala
Created May 6, 2011
sample DFA (deterministic finite automaton) implementation in Scala
View StateMachine.scala
// implements this DFA:
// as described here:
// 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 / 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 / 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[_] =>
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)
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 / io.scala
Created May 6, 2011
abandoned attempt at a Haskell-style IO monad in Scala
View io.scala
// I started with code from:
// 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 / 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 / buffer.scala
Created May 24, 2011
eliminating a mutable buffer
View buffer.scala
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.