Skip to content

Instantly share code, notes, and snippets.

View akahn's full-sized avatar

Alex Kahn akahn

View GitHub Profile
module Cellular where
import System.Random
import Data.List.Split
import Data.Maybe
isWall False = '.'
isWall True = '#'
mapWithIndex :: (Enum a, Num a) => (a -> b -> c) -> [b] -> [c]
@akahn
akahn / elasticsearch.sh
Created November 5, 2013 02:26
Trying to understand elasticsearch's schema and types
$ curl -XPOST http://localhost:9200/myindex/mytype -d '{"foo": ["bar"]}'
{"ok":true,"_index":"myindex","_type":"mytype","_id":"YYfPUL3TQLaL17zhXQ-ELw","_version":1}
$ curl -XPOST http://localhost:9200/myindex/mytype -d '{"foo": [{"foo": "bar"}]}'
{"error":"MapperParsingException[Failed to parse [foo]]; nested: ElasticSearchIllegalArgumentException[unknown property [foo]]; ","status":400}
$ curl -XDELETE http://localhost:9200/myindex/mytype
{"ok":true}
$ curl -XPOST http://localhost:9200/myindex/mytype -d '{"foo": [{"foo": "bar"}]}'
def case_wat
case number = 1
when number == 1
'expected'
else
'wat'
end
end
def find_value(key, hash)
result = hash
key.split('.').each do |segment|
segment = segment.to_i if segment =~ /\d+/
result = result[segment]
return unless result
end
result
end
def save_and_open_page
File.write('tmp/last_response.html', last_response.body)
`open tmp/last_response.html`
end
>> connection = PGconn.new(...)
>> connection.exec('update foo set bar=true') # long running query
^C^C^C^C^C^C
# Can't cancel the query
>> connection.async_exec('update foo set bar=true') # long running query
^C
>>
@akahn
akahn / zombie.c
Created August 13, 2012 20:48
Simulate a zombie process on Linux
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main ()
{
pid_t child_pid;
child_pid = fork ();
if (child_pid > 0) {
@akahn
akahn / gist:2703322
Created May 15, 2012 17:05
Currently running PostgreSQL queries sorted by duration
SELECT datname, procpid, usename, client_addr, current_query, (current_timestamp - query_start) AS duration
FROM pg_stat_activity
WHERE datname = 'your-database'
ORDER BY duration DESC;
class AdvancingTimeRange
def initialize(time = '2009-03-01')
@cursor = DateTime.parse(time)
end
def next(increment_by)
range = @cursor..@cursor + increment_by
@cursor = range.end
range
end
#while true; end
file = File.new('/Users/akahn/out', 'w')
loop do
begin
buf = ""
buf << STDIN.read_nonblock(1024)
if buf =~ /^.*?\n/
file.write_nonblock(buf)
end