Skip to content

Instantly share code, notes, and snippets.

View alexandru's full-sized avatar
😺
Having fun

Alexandru Nedelcu alexandru

😺
Having fun
View GitHub Profile
a, b = 0, 1
while still_has_lock():
time.sleep(a + b)
a, b = b, a+b
# Fibonacci WTF !
@alexandru
alexandru / gist:2044035
Created March 15, 2012 12:49 — forked from dx7/gist:1333785
Installing ruby-debug with ruby-1.9.3-p125
# Install with:
# bash < <(curl -L https://raw.github.com/gist/1333785)
#
# Reference: http://blog.wyeworks.com/2011/11/1/ruby-1-9-3-and-ruby-debug
echo "Installing ruby-debug with ruby-1.9.3-p125 ..."
curl -OL http://rubyforge.org/frs/download.php/75414/linecache19-0.5.13.gem
curl -OL http://rubyforge.org/frs/download.php/75415/ruby-debug-base19-0.11.26.gem
@alexandru
alexandru / latency.txt
Created May 31, 2012 14:32 — forked from jboner/latency.txt
Latency numbers every programmer should know
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns
Mutex lock/unlock 25 ns
Main memory reference 100 ns
Compress 1K bytes with Zippy 3,000 ns
Send 2K bytes over 1 Gbps network 20,000 ns
Read 1 MB sequentially from memory 250,000 ns
Round trip within same datacenter 500,000 ns
Disk seek 10,000,000 ns
@alexandru
alexandru / gist:5160312
Created March 14, 2013 10:30
Play 2.1 / Scala - NullPointerException
java.lang.NullPointerException
at play.core.server.netty.RequestBodyHandler$$anonfun$1.apply(RequestBodyHandler.scala:45)
at play.core.server.netty.RequestBodyHandler$$anonfun$1.apply(RequestBodyHandler.scala:43)
at scala.concurrent.stm.ccstm.InTxnImpl.runBlock(InTxnImpl.scala:538)
at scala.concurrent.stm.ccstm.InTxnImpl.topLevelAttempt(InTxnImpl.scala:494)
at scala.concurrent.stm.ccstm.InTxnImpl.topLevelAtomicImpl(InTxnImpl.scala:365)
at scala.concurrent.stm.ccstm.InTxnImpl.atomic(InTxnImpl.scala:244)
at scala.concurrent.stm.ccstm.CCSTMExecutor.apply(CCSTMExecutor.scala:24)
at play.core.server.netty.RequestBodyHandler$class.pushChunk$1(RequestBodyHandler.scala:43)
at play.core.server.netty.RequestBodyHandler$$anon$1.messageReceived(RequestBodyHandler.scala:89)
@alexandru
alexandru / CorsSupport.scala
Created May 9, 2013 06:06
For serving CORS headers in responses.
import play.api.mvc._
trait CorsSupport { self: Controller =>
def CorsAction(cb: => Result): Action[AnyContent] =
Action(cb.withHeaders(actionCorsHeaders: _*))
def CorsAction(cb: Request[AnyContent] => Result): Action[AnyContent] =
Action { request =>
cb(request).withHeaders(actionCorsHeaders: _*)
#!/usr/bin/env bash
# Install script adapted from:
# https://gist.github.com/dwayne/2983873
NODE_DIR="$HOME/.nodejs"
echo 'export PATH=$HOME/$NODE_DIR/bin:$PATH' >> ~/.bashrc
echo 'export NODE_PATH=$HOME/$NODE_DIR/lib/node_modules' >> ~/.bashrc
source ~/.bashrc
package object extensions {
import play.api.mvc._
implicit class ResultExtensions(val result: PlainResult)
extends AnyVal {
def withLangCookie(lang: Lang)(implicit app: Application): PlainResult =
result.withCookies(Cookie(Play.langCookieName, lang.code, httpOnly = false))
}
}
@alexandru
alexandru / Environment.scala
Last active December 27, 2015 13:49
Play / Subcut dependency injection in modules
// Helper for keeping track of the active project bindings
// (because we don't want to hard-code those inside singleton objects)
object Environment {
def currentBindings =
ref.get match {
case null =>
throw new NullPointerException("No BindingModule currently registered (app not running?)")
case value =>
value
import scalaz.concurrent.Task
def benchmark[T](repeat: Int)(callback: => T): Unit = {
var lastResult: Any = null
var total = Duration.Zero
for (i <- 0 until repeat) {
val time = System.nanoTime()
val result = callback
val measured = (System.nanoTime() - time).nanos
@alexandru
alexandru / Ref.scala
Created January 10, 2014 23:09
I'm implementing my own FSM Actors because akka.actor.FSM is disappointing. Part of the implementation is a new way of dealing with data mutation on events. I want Vars, but I want to enforce the scope in which reads or writes can happen. Following is an implementation for scoped, thread-safe Refs that also preserve their previous state before t…
package fsm
import scala.annotation.implicitNotFound
import scala.util.control._
import RefContext._
import Ref._
/**
* Ref is a scoped type-safe mutable reference.
*