Skip to content

Instantly share code, notes, and snippets.

View marcinwyszynski's full-sized avatar

Marcin Wyszynski marcinwyszynski

View GitHub Profile
class Enum
include Enumerable
def initialize(input)
@hash = {}
method = input.is_a?(Array) ? :each_with_index : :each
input.send(method) do |key, value|
@hash[key] = value
self.class.send(:define_method, key.to_sym) { value }
@marcinwyszynski
marcinwyszynski / serve.go
Last active December 20, 2015 18:09
Statically serve the content of a directory. By default it picks up the current working directory but you can also pass the path using the --path command-line flag.
// Statically serve the content of a directory. By default it picks up the
// current working directory but you can also pass the path using the --path
// command-line flag.
package main
import (
"flag"
"fmt"
"log"
@marcinwyszynski
marcinwyszynski / synchonously.js
Created September 28, 2013 15:17
Because callbacks are hard.
// Because callbacks are hard.
synchronously = {};
synchronously.Mutex = function() {
this.locked_ = false;
};
synchronously.Mutex.prototype.lock = function() {
this.locked_ = true;
@marcinwyszynski
marcinwyszynski / metronome.js
Last active December 24, 2015 16:09
Timer for JavaScript.
function Metronome(interval, executions, func, context) {
if (interval < 1) {
throw "Interval must be a integer";
}
if (executions == 0) {
throw "Number of executions must be a positive integer";
}
// Initialize mutable private members.
var executionsLeft = executions;
@marcinwyszynski
marcinwyszynski / selfref.go
Created March 6, 2014 00:12
Golang: self-referential functions
// Original idea:
// http://commandcenter.blogspot.nl/2014/01/self-referential-functions-and-design.html
package main
import "fmt"
type Cat struct {
Name string
}
@marcinwyszynski
marcinwyszynski / trie.go
Last active August 29, 2015 13:57
Generic Trie for storing bytes
package trie
const (
// Number of possible values represented by a single byte.
byteValues = 1 << 8
)
type Trie struct {
culDeSac bool
children []*Trie
@marcinwyszynski
marcinwyszynski / chunked_reader.go
Created April 23, 2014 14:21
ChunkedReader is an io.Reader which runs a callback after each read chunk
package chunked_reader
import "io"
// ChunkedReader implements io.Reader interface while allowing the source to be
// read in set increments and running a callback function after each increment.
type ChunkedReader struct {
readSoFar int64
Reader io.Reader // The underlying io.Reader.
ChunkSize int // Maximum bytes to read on each Read call.
@marcinwyszynski
marcinwyszynski / wagnerfisher.go
Created July 29, 2014 21:42
Wagner-Fisher algorithm to calculate edit distance of two strings
// Wagner–Fischer a dynamic programming algorithm that computes the edit distance between
// two strings of characters.
//
// Source:
// http://en.wikipedia.org/wiki/Wagner%E2%80%93Fischer_algorithm
package main
import (
"fmt"
)
import os
import time
def open_unless_older_than(age_s, path):
meta = os.stat(path) # will throw IOError if path does not exist
if time.time() - meta.st_mtime > age_s:
raise IOError('File older than %d seconds' % age_s)
return open(path, r)
#!/usr/bin/env python
import sys
def parse_arglist(args):
arglist = []
for arg in args:
if not arg.startswith('-'):
arglist.append(arg)