Skip to content

Instantly share code, notes, and snippets.

View guersam's full-sized avatar

Jisoo Park guersam

  • Seoul, South Korea
View GitHub Profile
@guersam
guersam / AutoProductFormat.scala
Last active March 8, 2017 19:26
Macro-based json format generator for spray-json (https://github.com/spray/spray-json)
package spray.json
import scala.language.experimental.macros
trait AutoProductFormat extends DefaultJsonProtocol {
implicit def jsonFormat[T <: Product]: RootJsonFormat[T] = macro AutoProductFormatMacro.autoProductFormatMacro[T]
}
object AutoProductFormat extends AutoProductFormat
object RedisClient {
def apply(host: String, port: Int = 6379, name: String = defaultName,
settings: RedisClientSettings = RedisClientSettings())(implicit refFactory: ActorRefFactory): RedisClient =
apply(new InetSocketAddress(host, port), name, settings)
// More independent setting classes might be introduced for client, connection pool or cluster setup,
// but not sure of actual interface yet
def apply(remote: InetSocketAddress, name: String, settings: RedisClientSettings)
(implicit refFactory: ActorRefFactory): RedisClient =
@guersam
guersam / gist:24bd61849ad93244b6eb
Last active August 29, 2015 14:01
Generating unique random token using async Redis driver (https://github.com/debasishg/scala-redis-nb)
def generateUniqueRandomToken(): Future[String] = {
val token = generateRandomToken(TokenLength)
redis.exists(tokenKey(token)) flatMap {
case true => generateUniqueRandomToken()
case false => Future.successful(token)
}
}
// ...
@guersam
guersam / 0 - so.py
Last active August 29, 2015 14:02
맥소날드
#!/usr/bin/env python
import itertools
NR_OF_MARIOS = 4
def mario_test(n):
marios = range(NR_OF_MARIOS)
all_cases = [p for p in itertools.product(marios, repeat = n)]
@guersam
guersam / gist:2a3d61d6429f62a43ee6
Last active August 29, 2015 14:03
Changes from RFC2616 to RFC7230 ~ RFC7235

Changes from RFC2616 to RFC7230 ~ RFC7235

This document aggregates Changes from RFC(s) 2616 (and 2617) sections from RFC7230 to RFC7235, which supersede RFC2616. See Mark Nottingham's blog post for further background.

  • HTTP's approach to error handling has been explained. (Section 2.5)

  • The HTTP-version ABNF production has been clarified to be case-sensitive. Additionally, version numbers have been restricted to single digits, due to the fact that implementations are known to handle multi-digit version numbers incorrectly. (Section 2.6)

@guersam
guersam / gist:49c4dca01ee19e62700f
Created July 11, 2014 02:34
SBT: MaxPermSize or MaxMetaspaceSize according to the current Java version
javaOptions in (Test, test) := {
val versionParse = """(\d+)\.(\d+)""".r
val permOrMetaspace =
sys.props ("java.specification.version") match {
case versionParse (maj, min) =>
val major = maj.toInt
val minor = min.toInt
if (major > 1 || major == 1 && minor >= 8) "Metaspace" else "Perm"
case _ =>
sys.error ("Unable to determine Java version")
@guersam
guersam / GenericQueryStringBindable.scala
Last active October 6, 2015 17:28
Generic QueryStringBindable for Play 2.4
package util
import play.api.mvc.QueryStringBindable
import shapeless._
import shapeless.labelled.{ FieldType, field }
trait GenericQueryStringBindable {
implicit def hNilBindable: QueryStringBindable[HNil] =
new QueryStringBindable[HNil] {
@guersam
guersam / Hittable.scala
Last active October 19, 2015 17:56
Generic update without `.copy`
sealed trait Character
case class Player(hp: Int) extends Character
case class Civilian(name: String, hp: Int) extends Character
case class Monster(hp: Int, weakness: String) extends Character
object HittableDemo extends App {
import Hittable.ops._
val c1: Character = Player(1)
assert(c1.hit == Player(0))
object Main extends App {
import scala.io._
import java.io._
def parseLine(s: String) = s.split(' ').map(_.trim).filter(_.nonEmpty).map(_.toInt).toList
// val filename = "example.in"
// val filename = "C-small-practice.in"
val filename = "C-large-practice.in"
@guersam
guersam / BasicMsgpackSerialize.scala
Last active November 26, 2015 08:16
Generic `Serialize` derivation for scodec-msgpack
import scodec._
import scodec.bits.ByteVector
import scodec.msgpack._
import scala.collection.generic.{CanBuildFrom, IsTraversableOnce}
import scala.language.higherKinds
trait BasicMsgpackSerialize {
implicit def serializeBinary: Serialize[ByteVector] = Serialize.binary