Skip to content

Instantly share code, notes, and snippets.

View longshorej's full-sized avatar
🏠
Working from home

Jason Longshore longshorej

🏠
Working from home
View GitHub Profile
// scala task magnet for akka http
implicit def taskMagnet[T](task: => Task[T])
(implicit tupler: Tupler[T]) = new OnSuccessMagnet {
type Out = tupler.Out
val directive = OnSuccessMagnet(task.runStdFuture()).directive
}
@longshorej
longshorej / start.sh
Created June 9, 2016 23:57
docker containers access docker host via /etc/hosts
#!/bin/bash
set -e
HPN_DOCKER_HOST=`/sbin/ip route | awk '/default/ { print $3 }'`
echo "$HPN_DOCKER_HOST docker" >> /etc/hosts
# exec ...
@longshorej
longshorej / nl2br.scala
Created April 20, 2016 17:05
scala nl2br with scalatags
import scalatags.Text.all._
object nl2br {
def apply(s: String): Modifier = {
val lines = s.replaceAllLiterally("\r\n", "\n").replaceAllLiterally("\r", "\n").split('\n')
lines.foldLeft(None: Option[Modifier]) { case (step, line) =>
Some(step.fold[Modifier](line)(previous => Vector[Modifier](previous, br, line)))
}.getOrElse("")
}
@longshorej
longshorej / akkabug.scala
Last active October 29, 2015 17:33
akka bug?
/*
name := "bug"
version := "1.0"
organization := "com.github.longshorej"
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-http-experimental" % "1.0"
)
@longshorej
longshorej / findObjects.scala
Created October 28, 2015 17:03
scala find all objects that subclass a case class or trait
// sbt dependencies: shapeless and org.reflections.reflections
val reflections = new Reflections("org.example")
def findAllObjects[T](cl: Class[T])(implicit t: Typeable[T]): Vector[T] = {
reflections.getSubTypesOf(cl).toVector.flatMap(cl => cl.getField("MODULE$").get(null).cast[T])
}
findAllObjects(classOf[com.hpn.wms2.core.common.ClientDef]).foreach(println)
@longshorej
longshorej / Statistics.scala
Last active May 31, 2016 16:04
akka http statistics
package com.hpn.libs.akka.http
// no time to properly open source, but maybe this someone finds this useful. It's used
// as part of our graceful shutdown logic for akka HTTP
import akka.http.scaladsl.model._
import akka.stream._
import akka.stream.stage._
import java.util.concurrent.atomic.AtomicLong
@longshorej
longshorej / gist:c7f1b5195b069d18130f
Created September 22, 2014 15:27
scala repl threads
def threaded(numThreads: Int)(block: => Unit) = {
val threads = for (n <- 1 to numThreads) yield new Thread(new Runnable {
def run() = block
})
threads foreach (_.start())
}
def time[A](f: => A) = {
val s = System.nanoTime
val ret = f
println("time: "+(System.nanoTime-s)/1e6+"ms")
ret
}
def timeAvg[A](avg: Int)(f: => A) = {
val s = System.nanoTime