Skip to content

Instantly share code, notes, and snippets.

View mikemckibben's full-sized avatar

Michael McKibben mikemckibben

View GitHub Profile
@mikemckibben
mikemckibben / hackerrank.hsfiles
Last active October 30, 2017 16:23
Stack Template for hackerrank project stub
{-# START_FILE {{name}}.cabal #-}
name: {{name}}
version: 0.1.0.0
build-type: Simple
cabal-version: >=1.10
executable solution
hs-source-dirs: .
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N
@mikemckibben
mikemckibben / AnyParamSpec.scala
Last active August 29, 2015 14:11
Spray AnyParam with default value Bug
import org.specs2.mutable.Specification
import spray.http._
import spray.routing._
import spray.testkit.Specs2RouteTest
class AnyParamSpec extends Specification with Specs2RouteTest with Directives {
val route = anyParam('p ? "default") { p => complete(StatusCodes.OK, p) }
// replace with the following workaround
//val route = (anyParam('p) | provide("default")) { p => complete(StatusCodes.OK, p) }
@mikemckibben
mikemckibben / EitherExampleSpec.scala
Last active June 28, 2017 17:19
Example Unmarshalling an Either[A, B] with spray-json
import org.specs2.mutable.Specification
import spray.http.{HttpResponse, StatusCodes}
import spray.httpx.SprayJsonSupport
import spray.httpx.marshalling._
import spray.httpx.unmarshalling._
import spray.json._
/***
* scalaVersion := "2.11.2"
*
@mikemckibben
mikemckibben / ex-regex-pattern-matching.scala
Created January 11, 2013 16:46
Example of regex extractor with pattern matching in scala.
object Parser {
val Result = """mac: (\S+) sites: \[([^]]+)\]""".r
def parseLine(line: String): Option[(String,Seq[String])] = line match {
case Result(mac,sites) => Some((mac, sites.split(""",\s*""").toSeq))
case _ => None
}
}
val input = io.Source.fromFile("input.txt")
val lines = input.getLines()
@mikemckibben
mikemckibben / ex-copy-plugin-dependencies-task.sbt
Created January 9, 2013 20:35
An example of how to write a task that copies project dependencies to a directory.
TaskKey[Unit]("copy-plugin-dependencies") <<= (update) map {
(updateReport) =>
updateReport.select(Set("compile", "runtime")) map { jar =>
import java.util.jar.{JarFile, Manifest => JarManifest}
val jarFile = new JarFile(jar)
val jarAttributes = jarFile.getManifest().getMainAttributes()
val targetdir = file("plugins")
if (!targetdir.exists) targetdir.mkdir()
else if (!targetdir.isDirectory) error("%s is not a directory".format(targetdir))
for (bundleSymName <- Option(jarAttributes.getValue("Bundle-SymbolicName"));
@mikemckibben
mikemckibben / ManagedReference.scala
Created December 18, 2012 17:59
lazy, resetable atomic reference
class ManagedReference[T](private[this] val createF: => T, private[this] val closeF: Option[T => Unit] = None) {
import java.util.concurrent.atomic.{ AtomicReference }
private[this] val ref = new AtomicReference[Option[T]](None)
def apply(): T = {
val opt = ref.get
val value = opt.getOrElse(createF)
// test if ref was set by another thread between ref.get and opt.getOrElse