Skip to content

Instantly share code, notes, and snippets.

@yllan
yllan / gist:4226239
Created December 6, 2012 17:20
Reduce the slug size
heroku config:add BUILDPACK_URL=http://github.com/heroku/heroku-buildpack-scala.git
@yllan
yllan / hash_key_trap.js
Created December 25, 2012 05:04
javascript 小嫩嫩自婊記
var STATE_CASE_1 = 0,
STATE_CASE_2 = 1,
STATE_CASE_3 = 2,
DEFAULT_CASE = 99999;
var dict = {
STATE_CASE_1: function (s) { /**/ }, // The key is not its Number value! It's String "STATE_CASE_1"!
STATE_CASE_2: function (s) { /**/ },
STATE_CASE_3: function (s) { /**/ },
DEFAULT_CASE: function (s) { /**/ }
@yllan
yllan / fastest.scala
Last active December 11, 2015 12:49
JSON reformat
def reformat(json: String) = {
val tab = " "
var inString = false
var indentLevel = 0
var index = 0;
val stringLength = json.length
val formatted = new StringBuilder
while (index < stringLength) {
@yllan
yllan / nginx.conf
Created February 5, 2013 07:55
設定 nginx 使用 server sent event
location xxxx {
proxy_pass http://localhost:9000;
proxy_buffering off;
proxy_cache off;
proxy_redirect off;
proxy_set_header Connection '';
proxy_http_version 1.1;
chunked_transfer_encoding off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
@yllan
yllan / BodyParser.scala
Last active December 15, 2015 11:29
How to calculate the sha-1 hash when receiving uploaded files using BodyParser in PlayFramework so that you don't need to open the file and read the content again. Also an example of how to process stream data in Play using Iteratee.
// import java.io._
// import java.security._
// import play.api.libs.iteratee._
def sha1FileParser(to: File): BodyParser[(String, File)] = BodyParser((header: RequestHeader) ⇒ {
Iteratee.fold[Array[Byte], (MessageDigest, FileOutputStream)](MessageDigest.getInstance("SHA-1"), new FileOutputStream(to)) { case ((md, os), data) ⇒
md.update(data)
os.write(data)
(md, os)
} mapDone { case (md, os) ⇒
@yllan
yllan / GO63Channel.scala
Last active December 15, 2015 16:49
Rewrite the Go tutorial of goroutine with scala.concurrent.
// Rewrite http://tour.golang.org/#63 to scala using Channel
import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global
object GO63Channel extends App {
def sum(a: List[Int], c: Channel[Int]) = future {
c write a.sum
}
val c = new Channel[Int]()
@yllan
yllan / build.sbt
Created April 10, 2013 13:07
SBT setup for scalaz 7 with scala 2.10
scalaVersion := "2.10.1"
resolvers ++= Seq(
"Typesafe Snapshots" at "http://repo.typesafe.com/typesafe/snapshots/",
"OSS" at "https://oss.sonatype.org/content/repositories/snapshots/")
libraryDependencies ++= Seq(
"org.scalaz" %% "scalaz-core" % "7.0-SNAPSHOT"
)
@yllan
yllan / qsort_like.scala
Last active December 17, 2015 03:39 — forked from itszero/gist:5543218
/* render a tweet using entities: for example
* val e = Entity(10, 21, """<a href="https://twitter.com/search/?q=%23ScalaIntro">#ScalaIntro</a>""")
* val entities = Seq(e)
* render("I love my #ScalaIntro exercises!", entities)
* should result in
* """I love my <a href="https://twitter.com/search/?q=%23ScalaIntro">#ScalaIntro</a> exercises!"""
* The "start" and "end" fields of an Entity refer to the indices in the
* original string to be substituted with the html field; you may assume
* that these indices are all non-overlapping.
*/
@yllan
yllan / simple_list.scala
Created May 9, 2013 08:48
Demonstrate how to implement append
sealed abstract class L[A] {
def append(l: L[A]): L[A]
}
case class LNil[A]() extends L[A] {
def append(l: L[A]) = l
override def toString = "Nil"
}
case class LCons[A](head: A, tail: L[A]) extends L[A] {