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 / ml.rb
Created May 1, 2012 14:39
memory leak
class TalentsController < ApplicationController
PUBLICS = %w(screen presenter musical t_stage others)
layout :different_layout
# memory leak
def different_layout
if PUBLICS.tap { |a| a.concat(a.map { |s| s+'_preview' }) }.include?(action_name)
'talents'
else
'admin'
@Centaur
Centaur / gist:2978757
Created June 23, 2012 15:52
Indifferent Map for scala
class IndifferentMap[K >: String with Symbol ,V](original:Map[K,V]) extends collection.MapProxy[String, V] {
val beafed = original.map{
case (k:Symbol,v) => k.name->v
case (k:String, v) => k->v
}
override def self = beafed
def apply(s:Symbol):V = apply(s.name)
}
val m = new IndifferentMap(Map("abc"->1))
function template($file, $templateid = 0, $tpldir = '', $gettplfile = 0, $primaltpl='') {
global $_G;
static $_init_style = false;
if($_init_style === false) {
C::app()->_init_style();
$_init_style = true;
}
$oldfile = $file;
if(strpos($file, ':') !== false) {
val target = util.Random.nextInt(100)
val scanner = new java.util.Scanner(System.in)
var times = 0
def askAndCompare {
print("Guess a number: ")
val input = scanner.nextInt
times += 1
if(input < target){
@Centaur
Centaur / guess.hs
Created July 31, 2012 14:03
haskell guess
askAndCompare :: Int -> Int -> IO ()
askAndCompare target level = do
putStr "Input your guess:"
hFlush stdout
input <- getLine
case compare (read input) target of
LT -> do putStrLn "Too small! Try again."
askAndCompare target (level + 1)
GT -> do putStrLn "Too Big! Try again."
askAndCompare target (level + 1)
def maxseq(data: List[Int]): List[Int] = {
@annotation.tailrec
def maxAccum(remain: List[Int], found: List[Int], next: List[Int]): List[Int] = remain match {
case Nil => if(found.size == 1) Nil else found
case head :: tail => if(head > next.head) {
if(next.length == found.length) maxAccum(tail, head :: next, head :: next)
else maxAccum(tail, found, head :: next)
}
else maxAccum(tail, next, head :: Nil)
}
@Centaur
Centaur / gist:3891910
Created October 15, 2012 10:44
max sub seq clojure version
(defn get-max-sub [h & t]
(let [max-sub (fn [[head & tail] found next]
(if head
(if (> head (last next))
(let [next2 (conj next head)]
(if (= (count next) (count found))
(recur tail next2 next2)
(recur tail found next2)
)
)
@Centaur
Centaur / gist:4070883
Created November 14, 2012 07:52 — forked from non/gist:4064198
Add map + flatMap to Function1-3 with no overhead
object RichF {
implicit class RichF1[A, Z](val f: A => Z) extends AnyVal {
def map[Y](g: Z => Y): A => Y =
(a: A) => g(f(a))
def flatMap[Y](g: Z => A => Y): A => Y =
(a: A) => g(f(a))(a)
}
implicit class RichF2[A, B, Z](val f: (A, B) => Z) extends AnyVal {
def map[Y](g: Z => Y): (A, B) => Y =
sealed trait CompletionMagnet[T] {
type Result
def complete(v: T): Result
}
implicit def statudObjectHasACompletionMagnet[T : Marshaller](tupl: (StatusCode, T)) = new CompletionMagnet[(StatusCode, T)]{
override type Result = Unit
override def complete(v: (StatusCode, T)) = ???
}
implicit def httpResponseFutureHasACompletionMagnet(future: Future[HttpResponse]) = new CompletionMagnet[Future[HttpResponse]]{
private val _jqueryCssSelectRegex = """(^\S)+""".r
private val _matchChar = Set('*', '[', '^', '-')
def sel(exp: String, content: NodeSeq): CssSel = {
val selects = _jqueryCssSelectRegex.findAllMatchIn(exp).map(_.matched).toList
val (init, last) =
if (_matchChar.contains(selects.last.head))
selects.dropRight(2) -> selects.takeRight(2).mkString(" ")
else