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.collection.Map | |
import scala.sys.error | |
import java.util.TimeZone | |
import scala.reflect.Manifest | |
class FieldMapping[V, P: Manifest](val code: Int, val toPrimitive: V => P, val fromPrimitive: P => V) { | |
type PrimitiveMap = Map[Int, Any] | |
def this(code: Int) = this(code, _.asInstanceOf[P], _.asInstanceOf[V]) |
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
// Sample build.sbt. | |
// Note: | |
// Blank lines need to separate the statements. | |
// := means you are setting the value for that key | |
// += means you are adding to the values for that key | |
name := "MyProject" | |
version := "0.1" |
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
object Async { | |
def async[A](thunk: => A)(whenDone: A => Unit) { | |
val swingWorker = new javax.swing.SwingWorker[A, Any] { | |
override def doInBackground(): A = thunk | |
override def done(): Unit = whenDone(get) | |
} | |
swingWorker.execute() | |
} | |
} |
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
// Can it really be this hard??? | |
sealed trait BinTree | |
case class Node(left: BinTree, right: BinTree) extends BinTree | |
case class Leaf(n: Int) extends BinTree | |
def generateTree(ints: Iterable[Int]): BinTree = { | |
type BinTreePair = (BinTree, BinTree) | |
def pairOffTrees(trees: List[BinTree]): List[BinTreePair] = { |
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 fpexample | |
object ListReverser { | |
// I don't see how this can be done in anything less than linear time | |
// (ie proportional to the length of the list). | |
// | |
// Space usage shouldn't vary, since foldLeft is tail recursive, | |
// and the size of the accumulated list grows at the same rate |
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 fpexample | |
import scala.util.Try | |
import scala.io.Source | |
case class Record[K,+A](key: K, fields: A) { | |
def map[B](f: A => B): Record[K, B] = Record(key, f(fields)) | |
} | |
object DataMunger { |
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
case class has[Owner, A](get: Owner => A) | |
implicit def ownerHasPairs[Owner, A, B](implicit a: Owner has A, b: Owner has B): Owner has (A,B) = | |
has(owner => (a.get(owner), b.get(owner))) | |
implicit def pairHas1st[A,B]: (A,B) has A = has(_._1) | |
implicit def pairHas2nd[A,B]: (A,B) has B = has(_._2) | |
implicit def widenReaderToOwner[Owner, O, A](reader: Reader[O, A])(implicit ev: Owner has O): Reader[Owner, A] = | |
reader.local[Owner](implicitly[Owner has O].get) |
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
// Category | |
// | |
trait Category[Arrow[_,_]] { | |
def compose[A,B,C](c1: Arrow[B,C], c2: Arrow[A,B]): Arrow[A,C] | |
def id[A]: Arrow[A,A] | |
} | |
object Category { | |
implicit object FunctionCat extends Category[Function1] { | |
def compose[A,B,C](f: B => C, g: A => B): A => C = 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
package kenbot.yowcat | |
/** | |
* Lets start with this representation of a category. | |
* | |
* Ordinarily we'd make better use of the type system here, | |
* but we want to manipulate all these concepts at the value level. | |
* | |
* Throughout, we'll use scala.Stream to represent sets or collections in the mathematical sense, even though they don't guarantee uniqueness. |
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 cat | |
object Classes { | |
// Convenient subtype syntax: Banana < Fruit | |
implicit class ClassOps(thisClass: Class[_]) { | |
def <:<(other: Class[_]): Boolean = | |
other isAssignableFrom thisClass | |
} |
OlderNewer