Skip to content

Instantly share code, notes, and snippets.

View joescii's full-sized avatar

Joe Daniel Barnes joescii

  • Open to work
  • United States
View GitHub Profile
@joescii
joescii / build.sbt
Created October 14, 2016 01:03
Conditional sbt Settings
scalaVersion := "2.11.7"
crossScalaVersions := Seq("2.9.2", "2.10.5", "2.11.7")
publishTo := Some(Resolver.file("Unused transient repository", file("target/unusedrepo")))
def conditionalSettings[P](conditionalKey: SettingKey[P])(predicate: P => Boolean): Seq[Def.Setting[_]] = {
// How can I make this suck less?
Seq(
publishArtifact := { if(predicate(conditionalKey.value)) false else publishArtifact.value },
publishLocal := { if(predicate(conditionalKey.value)) {} else publishLocal.value },
@joescii
joescii / git-r-done
Created October 11, 2016 15:14
Git r done
scala> val done = "GIT R DONE!!!"
done: String = GIT R DONE!!!
scala> object git {
| def r(s: String): Unit = println(s)
| }
defined object git
scala> git r done
GIT R DONE!!!
@joescii
joescii / UnapplySyntax.scala
Last active October 1, 2016 20:46
Terse syntax for creating an unapply
object UnapplySyntax {
trait Unapplier[A, B] { def unapply(a: A): Option[B] }
class <= [B, A](f: A => Option[B]) extends Unapplier[A, B] {
def unapply(a: A): Option[B] = f(a)
}
object <= {
def apply[B, A](f: A => Option[B]): B <= A = new <=(f)
}
@joescii
joescii / Reorder.scala
Created July 14, 2016 17:54
Modified reorderLocally for my purposes
def reorderLocally[I](f: I => Long, range:Long):Process1[I, I] = { // TODO: Can we replace Long with something like A: Order ?
import scala.collection.immutable.SortedMap
def emitMapValues(m: SortedMap[Long, Vector[I]]) =
emitAll(m.foldLeft(Vector.empty[I]) { case (acc, (_, tss)) => acc ++ tss })
def go(buffered: SortedMap[Long, Vector[I]]): Process1[I, I] = {
receive1Or[I, I](emitMapValues(buffered)) { t =>
val until = f(t) - range
val (toEmit, toBuffer) = buffered span { case (x, _) => x <= until }
@joescii
joescii / merge.scala
Created July 13, 2016 20:30
mergeSorted for the same type of Process
def mergeSorted[F[_], I, A: Order](sourceLeft: Process[F, I], sourceRight: Process[F, I])(f: I => A): Process[F, I] = {
mergeSorted(sourceLeft, f)(sourceRight, f).flatMap {
case Both(a, b) => Process.emit(a) ++ Process.emit(b)
case This(a) => Process.emit(a)
case That(b) => Process.emit(b)
}
}
@joescii
joescii / ChunkWhen.scala
Created July 13, 2016 17:28
SO `chunkWhen` updated for scalaz streams 0.7.3a
import scalaz.stream.Process._
import scalaz.stream._
import tee._
object ChunkWhen {
def chunkWhen[I](f: (I, I) => Boolean): Process1[I, Vector[I]] = {
def go(acc: Vector[I]): Process1[I,Vector[I]] =
receive1Or[I, Vector[I]](emit(acc)){ i =>
acc.lastOption match {
case Some(last) if ! f(last, i) => emit(acc) ++ go(Vector(i))
package com.joescii
import org.scalacheck._
import Gen._
import Prop._
object PalindromeChecks extends Properties("palindrome") {
val randBoolean:Gen[Boolean] = for {
b <- choose(0,1)
} yield {
@joescii
joescii / Browser.scala
Created March 12, 2016 23:56
Non-exhaustive pattern match warning
package com.joescii.sbtjs
import com.gargoylesoftware.htmlunit. { BrowserVersion => HUBrowserVersion }
import HUBrowserVersion._
sealed trait Browser
object Browsers extends Browsers
trait Browsers {
case object Firefox38 extends Browser
@joescii
joescii / package.scala
Last active March 8, 2016 14:50
Scala dependency injection with traits
package com.joescii
package object code {
val DbThing = new DbThingImpl()
val HttpThing = new HttpThingImpl()
val BizThing = new BizThingImpl(DbThing, HttpThing)
trait DbThing {
def getDbStuff(params):Future[DbStuff]
}
@joescii
joescii / repl.scala
Created December 17, 2015 16:38
ListMap.keys should be a List
scala> import scala.collection.immutable.ListMap
import scala.collection.immutable.ListMap
scala> val m = ListMap("a" -> "1", "b" -> "2", "c" -> "3", "d" -> "4", "e" -> "5")
m: scala.collection.immutable.ListMap[String,String] = Map(a -> 1, b -> 2, c -> 3, d -> 4, e -> 5)
scala> val ks = m.keys.toList
ks: List[String] = List(a, b, c, d, e)
scala> val upper = m.keys.map(_.toUpperCase).toList