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
// 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) |
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
// 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. |
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
// 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 { |
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
// 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 |
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
/tunes.txt |
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
// 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 { |
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
-- 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 |
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 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] |
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.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) |
OlderNewer