Skip to content

Instantly share code, notes, and snippets.

View Centaur's full-sized avatar

oldpig Centaur

  • singerdream.com
  • Shanghai
View GitHub Profile
@Centaur
Centaur / MinifiedService.scala
Created December 22, 2013 07:19
spray customized authentication demo
import spray.routing.{AuthenticationFailedRejection, HttpService}
import akka.actor.{Props, ActorSystem, Actor, ActorRefFactory}
import spray.routing.authentication._
import scala.concurrent.Promise
import spray.routing.AuthenticationFailedRejection.CredentialsMissing
import akka.io.IO
import spray.can.Http
class MinifiedService extends Actor with HttpService {
val identity_cookie_name = "session_id"
@Centaur
Centaur / fromScalaConsole.scala
Created December 24, 2013 06:21
MyType example
import reflect._
class Father[T <: Father[T] : ClassTag]{
def createInstance: T = implicitly[ClassTag[T]].runtimeClass.newInstance.asInstanceOf[T]
}
class Child1 extends Father[Child1]
class Child2 extends Father[Child2]
@Centaur
Centaur / it.scala
Last active January 2, 2016 08:49
import scala.annotation.tailrec
@tailrec
def common[T: Ordering](l1: List[T], l2: List[T], accu: List[(T, T)]): List[(T, T)] = {
(l1, l2) match {
case (Nil, _) | (_, Nil) => accu.reverse
case (h1 :: t1, h2 :: t2) =>
val cmp = implicitly[Ordering[T]].compare(h1, h2)
if (cmp < 0) common(t1, l2, accu)
else if (cmp > 0) common(l1, t2, accu)
@Centaur
Centaur / template.clj
Last active August 29, 2015 13:56
use httl in clojure
(ns clojure-scripts.template
(:import (httl Engine)))
(def model {:single_string "Hello, world!"
:items [{:name "item1"}
{:name "item2"}
{:name "item3"}]})
(defn keyword-as-str [model]
@Centaur
Centaur / fromScalaConsole.scala
Last active September 3, 2017 11:01
get case class properties using reflection
import scala.reflect.runtime.{universe => ru}
import ru._
def getProperties[T: TypeTag]: Iterable[String] = {
val tpe = ru.typeTag[T].tpe
tpe.declarations.collect {
case m: MethodSymbol if m.isCaseAccessor => m.name.toString
}
}

Each of these commands will run an ad hoc http static server in your current (or specified) directory, available at http://localhost:8000. Use this power wisely.

Discussion on reddit.

Python 2.x

$ python -m SimpleHTTPServer 8000
@Centaur
Centaur / TypeInstance.scala
Created March 25, 2014 09:21
test a type has some type class instance in scope
trait TypeClass1[T]
trait TypeClass2[V]
implicit object StringIsTypeClass1 extends TypeClass1[String]
def test[X, M[_]](implicit ev: M[X] = null.asInstanceOf[M[X]]): Boolean = {
ev != null
}
assert(test[String, TypeClass1] == true)
// You won't be able to compile this with:
// scala -language:dynamics NoDynamics.scala
// These two ambiguous implicits disable the `dynamics` feature.
implicit def ambDynamics1: language.dynamics.type = ???
implicit def ambDynamics2: language.dynamics.type = ???
object Woo extends Dynamic {
def selectDynamic(name: String) = s"$name a mess of a language"
@Centaur
Centaur / fromScalaConsole.scala
Last active August 29, 2015 13:59
extract pair as expected type
trait CanFromString[T] {
def fromString(s: String): T
}
implicit object intCanFromString extends CanFromString[Int] {
override def fromString(s: String): Int = s.toInt
}
implicit object SymbolCanFromString extends CanFromString[Symbol] {
override def fromString(s: String): Symbol = Symbol(s)
}
def pair(s: String, key: Int, value: Int): (String,String) = {
val arr = s.split(",")
arr(key) -> arr(value)
}
def pair[V](s: String, key: Int, value: (Int, String => V)): (String,V) = {
val arr = s.split(",")
arr(key) -> value._2.apply(arr(value._1))
}
def pair[K](s: String, key: (Int, String => K), value: Int): (K, String) = {
val arr = s.split(",")