Skip to content

Instantly share code, notes, and snippets.

Joe Barnes joescii

Block or report user

Report or block joescii

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@joescii
joescii / build.sbt
Created Oct 14, 2016
Conditional sbt Settings
View build.sbt
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 },
View 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 Oct 1, 2016
Terse syntax for creating an unapply
View UnapplySyntax.scala
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 Jul 14, 2016
Modified reorderLocally for my purposes
View Reorder.scala
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 Jul 13, 2016
mergeSorted for the same type of Process
View merge.scala
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 Jul 13, 2016
SO `chunkWhen` updated for scalaz streams 0.7.3a
View ChunkWhen.scala
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))
View PalindromeChecks.scala
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 Mar 12, 2016
Non-exhaustive pattern match warning
View Browser.scala
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 Mar 8, 2016
Scala dependency injection with traits
View package.scala
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 Dec 17, 2015
ListMap.keys should be a List
View repl.scala
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
You can’t perform that action at this time.