Skip to content

Instantly share code, notes, and snippets.

View seanparsons's full-sized avatar
💭
Setting A Status Message

Sean Parsons seanparsons

💭
Setting A Status Message
View GitHub Profile
@seanparsons
seanparsons / gist:1150420
Created August 16, 2011 23:13
Parser combinators experimentation
scala> Parser8086.instructions(reader)
res1: com.github.assgui.Parser8086.ParseResult[List[com.github.assgui.Parser8086.~[com.github.assgui.Parser8086.~[com.github.assgui.Parser8086.~[String,String],String],String]]] = [1.17] parsed: List((((mov~dx)~,)~1000))
@seanparsons
seanparsons / gist:1176554
Created August 28, 2011 11:19
Quick and dirty CyclicIterator.
case class CyclicIterator[T](traversable: Traversable[T]) extends Iterator[T] {
var currentIterator = traversable.toIterator
def next() = {
if (!currentIterator.hasNext) currentIterator = traversable.toIterator
currentIterator.next()
}
def hasNext = !traversable.isEmpty
}
@seanparsons
seanparsons / gist:1226150
Created September 19, 2011 08:26
A* implementation.
trait AStarAlgorithm[T] {
def heuristic(currentPath: Vector[T], from: T , to: T, goal: T): Double
def getReachable(node: T): Set[T]
}
object AStar {
def find[T](nodes: Set[T], start: T, goal: T)(implicit algorithm: AStarAlgorithm[T]): Option[Vector[T]] = {
find(start, goal, Set(start), Vector(start), 0)
}
def find[T](start: T, goal: T, closed: Set[T], navigated: Vector[T], currentCost: Double)(implicit algorithm: AStarAlgorithm[T]): Option[Vector[T]] = {
@seanparsons
seanparsons / gist:1228930
Created September 20, 2011 11:53
Asset dependency thoughts.
import scalaz._
import Scalaz._
case class Asset(id: Int, path: String)
case class AssetDependencyChain(assetToDeps: Map[Asset, Set[Asset]]) {
def getResolvedDependencies(startingAsset: Asset): ValidationNEL[String, Set[Asset]] = {
def resolve(workingAsset: Asset, workingPath: Vector[Asset]): ValidationNEL[String, Set[Asset]] = {
if (workingPath.contains(workingAsset)) "Circular dependency discovered with %s and along path %s.".format(workingAsset, workingPath).failNel[Set[Asset]]
else {
val workingSet = Set(workingAsset)
@seanparsons
seanparsons / RebuildJavaFX.scala
Created October 16, 2011 10:38
C#'s using method implemented in Scala, with lazy creation of the Closeable instance.
def rebuildJavaFX(runtimeRoot: Path, destinationFolder: Path): Unit = {
val binRoot = runtimeRoot.resolve("bin")
val bundlePath = destinationFolder.resolve("javafxnativebundle.jar")
bundlePath.toFile.delete()
using(FileSystems.newFileSystem(createNewZipFile(bundlePath), null)){zipFileSystem =>
toPathStream(binRoot)
.filter(isLibrary)
.filterNot(shouldBeIgnored)
.foreach{libraryItem =>
val zipPath = zipFileSystem.getPath(libraryItem.getFileName.toString)
val lineSeperator = ", " + System.getProperty("line.separator")
Array("First Line", null, "Second Line", "Third Line").filter(_ != null).mkString(lineSeperator)
@seanparsons
seanparsons / gist:1391524
Created November 24, 2011 14:59
Beware of implicit conversions (in both C# and Scala it would appear).
case class Thing(text: String)
implicit def convertToThing(text: String): Thing = new Thing(text)
def jazzify(text: String): Unit = println("JAZZ: " + text)
def jazzify(thing: Thing): Unit = jazzify(thing.text)
// Brings about the end of days by effectively calling jazzify(thing: Thing) recursively.
jazzify(new Thing("Sean"))
@seanparsons
seanparsons / gist:1550765
Created January 2, 2012 13:54
Puzzling Scalaz Equal issue
println(expectedParseResult ≟ actualParseResult)
println(expectedParseResult.fail ≟ actualParseResult.fail)
// Output is:
// false
// true
@seanparsons
seanparsons / gist:1579662
Created January 8, 2012 20:57
IO monad example
def write(toStore: T): IO[Unit] = {
val streamIO = io(new ObjectOutputStream(new FileOutputStream(file)))
streamIO.bracket(closeCloseable)(stream => io(stream.writeObject(toStore)))
}
@seanparsons
seanparsons / gist:1585616
Created January 9, 2012 23:26
IO and Promise from Scalaz used to encapsulate prompting for input.
def requestOption(message: String, title: String): IO[Option[String]] = {
io(Promise(
Dialog.showInput[String](
message = message,
title = title,
messageType = Dialog.Message.Question,
initial = "")
).get)
}