Skip to content

Instantly share code, notes, and snippets.

@nlinker
nlinker / sketch.scala
Created December 26, 2011 03:20 — forked from cho45/sketch.scala
Sandboxed Rhino, Scala
//#! scalac -classpath js.jar sketch.scala && java -classpath .:/opt/local/share/scala/lib/scala-library.jar:js.jar Main #
// http://codeutopia.net/blog/2009/01/02/sandboxing-rhino-in-java/
import java.lang.System
import org.mozilla.javascript._
class SandboxNativeJavaObject (scope: Scriptable, javaObject: Object, staticType: Class[_])
extends NativeJavaObject(scope, javaObject, staticType) {
override def get (name: String, start: Scriptable):Object = null
}
@nlinker
nlinker / gist:2224410
Created March 28, 2012 07:04
Typesafe thrift client wrapper that is reconnecting in case of a failure
package net.thumbtack.onepm.server;
import net.thumbtack.onepm.thrift.FbUser;
import net.thumbtack.onepm.thrift.User;
import net.thumbtack.onepm.thrift.UserNotFoundException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransportException;
@nlinker
nlinker / gist:2477809
Created April 24, 2012 08:19
Liquibase example
databaseChangeLog() {
changeSet(id: "my first changeset", author: "daniel") {
comment('A comment')
createTable(tableName: 'my_table') {
column(name: 'id', type: 'uuid', defaultValue: 'uuid_generate_v4()') {
constraints(primaryKey: true, nullable: false)
}
column(name: 'name', type: 'character varying(100)') {
constraints(nullable: false)
}
@nlinker
nlinker / ConversionUtils
Created September 18, 2012 06:17
How to serialize/deserialize json for generic types with Gson
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
public class ConversionUtils {
// both
private static Type typeB = new TypeToken<SpellData>() { }.getType();
private static Gson gson;
static {
@nlinker
nlinker / all-in-one example
Created September 24, 2012 09:01
Get the network speed with sigar
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.hyperic.sigar.NetFlags;
import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.Sigar;
@nlinker
nlinker / gist:3856304
Created October 9, 2012 02:53 — forked from bkimble/gist:1365005
List local memcached keys using Ruby
#!/usr/bin/env ruby
require 'net/telnet'
cache_dump_limit = 100
localhost = Net::Telnet::new("Host" => "localhost", "Port" => 11211, "Timeout" => 3)
slab_ids = []
localhost.cmd("String" => "stats items", "Match" => /^END/) do |c|
matches = c.scan(/STAT items:(\d+):/)
slab_ids = matches.flatten.uniq
end
@nlinker
nlinker / SCombinator.scala
Created October 12, 2012 03:44 — forked from folone/SCombinator.scala
Y-Combinator in Scala
/**
* <b>Fixed Point Combinator is:</b>
* Y = λf.(λx.f (x x)) (λx.f (x x))
*
* <b>Proof of correctness:</b>
* Y g = (λf . (λx . f (x x)) (λx . f (x x))) g (by definition of Y)
* = (λx . g (x x)) (λx . g (x x)) (β-reduction of λf: applied main function to g)
* = (λy . g (y y)) (λx . g (x x)) (α-conversion: renamed bound variable)
* = g ((λx . g (x x)) (λx . g (x x))) (β-reduction of λy: applied left function to right function)
* = g (Y g) (by second equality) [1]
@nlinker
nlinker / gist:3877185
Created October 12, 2012 03:46
test big or litte endian
int bigOrLitteEndian() {
short int word = 0x0001;
char *byte = (char *) &word;
return (byte[0] ? 1 : 0);
}
@nlinker
nlinker / apache.erl
Created October 12, 2012 03:47
Script to check how many connections can Apache server survive
#!/usr/bin/env escript
-mode(compile).
-compile(export_all).
main([]) -> io:format("Usage: ~s URL [Count=1000]~n", [escript:script_name()]);
main([URL]) -> main([URL,"100"]);
main([URL,C] ) -> manage_workers(URL, [start_worker(URL, N) || N <- lists:seq(1,list_to_integer(C))]), ok.
@nlinker
nlinker / gist:3877715
Created October 12, 2012 07:03
Scala vs Java Y combinator
//////////////////////////////////////////////////////////////
// Java version
// this is an interface with just one method, apply() from
// https://code.google.com/p/guava-libraries/
import com.google.common.base.Function;
import junit.framework.TestCase;
public class YCFun extends TestCase {