Skip to content

Instantly share code, notes, and snippets.

@qerub
qerub / gist:5845073
Created June 23, 2013 13:42
Making gethostbyaddr accessible in PostgreSQL via Python
CREATE PROCEDURAL LANGUAGE 'plpythonu';
CREATE OR REPLACE FUNCTION gethostbyaddr(address text)
RETURNS text
LANGUAGE plpythonu
AS $$
import socket
try:
return socket.gethostbyaddr(address)[1][0]
@qerub
qerub / ThriftUnixDomainSocketServer.java
Created June 23, 2013 22:03
Thrift + Unix domain socket server
import java.io.File;
import java.io.IOException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFastFramedTransport;
import org.apache.thrift.transport.TIOStreamTransport;
import org.apache.thrift.transport.TTransport;
import org.newsclub.net.unix.AFUNIXServerSocket;
import org.newsclub.net.unix.AFUNIXSocket;
@qerub
qerub / gist:5901842
Created July 1, 2013 15:27
Clojure functions for Java enum ordinals
(defn enum-ordinal [^Enum x]
(.ordinal x))
(defn enum-cmp [cmp & xs]
(apply cmp (map enum-ordinal xs)))
(defmacro defenumop [cmp]
`(def ~(symbol (str "enum" cmp))
(partial enum-cmp ~cmp)))
@qerub
qerub / exception_with_a_cause.rb
Created November 26, 2013 11:10
[Ruby] Exception with a cause :)
class ExceptionWithACause < StandardError
attr_reader :cause
def initialize(message = nil, cause = nil)
@message = message
@cause = cause
super(message)
end
@qerub
qerub / jruby-with-bundler.sh
Created November 27, 2013 12:40
Augmenting JRuby Complete with Bundler: a full JRuby installation in a single JAR file
#!/bin/sh
set -ex
version=1.7.8
jar=jruby-complete-$version-with-bundler.jar
curl -o $jar http://jruby.org.s3.amazonaws.com/downloads/$version/jruby-complete-$version.jar
@qerub
qerub / for-map-benchmarks.clj
Created November 27, 2013 17:25
[Clojure] Benchmarks for different implementations of `for-map`
;; Benchmarks for different implementations of `for-map`
;; Can be run with `lein try proteus criterium`
;; See http://www.lexicallyscoped.com/2013/04/17/transients-in-clojure.html
;; for some relevant discussion.
(defn pairs-to-map-without-transient [pairs]
(reduce (fn [m [k v]] (assoc m k v)) {} pairs))
@qerub
qerub / StringExtensions.scala
Created December 18, 2013 12:43
Bringing Perl's (main) regex operators to [Scala] (just for fun)
implicit class StringExtensions(val _value: String) extends AnyVal {
def =~(r: Regex): Boolean = _value match {
case `r`() => true
case `r`(xs @ _*) => throw new IllegalArgumentException("Regex must not contain captures")
case _ => false
}
def !~(r: Regex): Boolean = !(this =~ r)
}
@qerub
qerub / ScalazLight.scala
Created December 18, 2013 23:07
Scalaz Light (The Good[^W]Indispensable Parts)
import scalaz._
trait ScalazLight
extends std.AllInstances
with syntax.std.ToOptionIdOps
with syntax.std.ToOptionOps
with syntax.ToEqualOps
with syntax.ToIdOps
@qerub
qerub / string-split-benchmarks-result.txt
Last active December 31, 2015 19:09
[Clojure/Java] Benchmarks of different ways of splitting a string by a char
# Benchmarking (.split test-input "\\.")
Evaluation count : 72101040 in 60 samples of 1201684 calls.
Execution time mean : 828.458928 ns
Execution time std-deviation : 29.055494 ns
Execution time lower quantile : 800.771006 ns ( 2.5%)
Execution time upper quantile : 895.752028 ns (97.5%)
Overhead used : 2.778847 ns
Found 5 outliers in 60 samples (8.3333 %)
@qerub
qerub / spiped-ssh.plist
Created December 19, 2013 01:30
Piping sshd through spiped running as an OS X launch daemon
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>spiped-ssh</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/spiped</string>
<string>-d</string>