Skip to content

Instantly share code, notes, and snippets.

import ObjectMapper
enum Event {
case ringing
case incomingSound(instant: Int64, samples: Data)
case outgoingSound(instant: Int64, samples: Data)
case hangUp
}
extension Event : ImmutableMappable {
@hisui
hisui / EntryPoint.kt
Last active January 5, 2018 00:08
JAX-WS on Jetty + Google Guice in Kotlin
package jp.segfault.playground.aswj
import com.google.inject.Inject
import com.google.inject.Singleton
import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.Produces
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.Response
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say voice="alice">Enjoy! Enjoy! Enjoy!</Say>
<Play>http://demo.twilio.com/docs/classic.mp3</Play>
</Response>
import shapeless.CNil
import shapeless.Coproduct
import shapeless.PolyDefns.Case1
import shapeless.ops.coproduct.Basis
import shapeless.ops.coproduct.Remove
trait Unfold[F, A, C <: Coproduct] {
type Out = Option[C]
def apply(a: A): Out
}
@hisui
hisui / Mutex.scala
Created February 6, 2017 04:29
Inter-process mutex in Scala
object Mutex {
def lock[T](path: String)(f: => T): T = {
val out = new java.io.FileOutputStream(path)
try {
val lock = out.getChannel().lock()
try f
finally lock.release()
}
finally out.close()
}
@hisui
hisui / Main.scala
Last active February 1, 2017 04:22
object Main extends App {
val a = Seq("a", "b", "c")
// syntax #1
{
import MyCoproductSyntax1._
for (username <- a; password <- a) {
UserService.authenticate(username, password).foldTo[Unit]
.caseOf[Authenticated] (_ => println("You just logged in!"))
import scala.concurrent.Future
import scala.concurrent.Promise
import scala.concurrent.ExecutionContext.Implicits.global
trait RecursiveFuture[+A] { lhs =>
def get: Future[Option[(A, RecursiveFuture[A])]]
def flatMap[B](f: A => RecursiveFuture[B]): RecursiveFuture[B] =
new RecursiveFuture[B] {
@hisui
hisui / build.sbt
Last active December 14, 2016 10:04
Adds system properties visible from test cases in both case that "fork" is ture or false.
import com.google.cloud.datastore.testing.LocalDatastoreHelper
commands += Command.command("datastoreRun") { state =>
val helper = LocalDatastoreHelper.create(1.0)
helper.start()
atExit {
helper.stop()
}
addSystemPropertiesTo(state)(Seq(
"datastore.host" -> helper.getOptions.getHost
@hisui
hisui / Main.scala
Last active December 2, 2016 07:41
import com.google.cloud.datastore._
import com.google.cloud.datastore.testing._
object Main extends App {
def benchmark[A](title: String)(f: => A): A = {
val t0 = System.currentTimeMillis()
val o: A = f
val t1 = System.currentTimeMillis()
println(title)
git fetch --all --prune
git branch -r --merged | grep feature | ruby -pane'gsub /^\*origin\//, ""' | xargs git push origin --delete
git branch -r --merged | grep fix | ruby -pane'gsub /^\*origin\//, ""' | xargs git push origin --delete