Skip to content

Instantly share code, notes, and snippets.

View L3viathan's full-sized avatar
🦊

Jonathan Oberländer L3viathan

🦊
View GitHub Profile
@L3viathan
L3viathan / wurstfact.js
Last active August 29, 2015 14:00
daily wurst fun fact, js edition (v1)
function pickRandomProperty(obj) {
// from http://stackoverflow.com/questions/2532218/pick-random-property-from-a-javascript-object
var result;
var count = 0;
for (var prop in obj)
if (Math.random() < 1/++count)
result = prop;
return result;
}
@L3viathan
L3viathan / SML quine
Last active August 29, 2015 14:01
Shortest SML quine I could come up with
(fn y=>(fn x=>(print(x^y^x^y))))(implode[chr(34)])"(fn y=>(fn x=>(print(x^y^x^y))))(implode[chr(34)])"
@L3viathan
L3viathan / abdomination.tex
Created January 28, 2015 18:37
Subtly increase the length of a document by 15–20%
\clubpenalty = 10000·
\widowpenalty = 10000·
\displaywidowpenalty = 10000
\setlength\parindent{2.5em}
\setlength\parskip{0.3\baselineskip}·
\usepackage{geometry}
\addtolength{\oddsidemargin}{.3in}
@L3viathan
L3viathan / multichat
Created May 24, 2015 17:37
Request for Comments: Rules and Operators for multi-level chat communication
[version 1.1]
Chatting happens in different levels, which allows for multi-threaded conversations.
The different conversation levels are marked with a number of parantheses.
Example 1:
A: How are you?
A: (And how is your family?)
B: Fine (and fine, too).
@L3viathan
L3viathan / nummagic.py
Created January 5, 2016 14:51
Shenanigans abusing magic methods
import random
import math
class Num(object):
def __init__(self, val):
self.value = val
self.bit = False
def __repr__(self):
return str(self.value)
def __abs__(self):
return self
@L3viathan
L3viathan / gettermagic.py
Last active August 31, 2016 07:02
Horrible abuse of Python's magic methods to make a more magic version of itemgetter/attrgetter
import operator as op
from functools import partial
class _getter(object):
def __init__(self, *, is_original=True):
self.is_original=is_original
self.__ops = []
def __call__(self, something):
''' Maybe eventually extend to act like methodcaller, but problems exist'''
@L3viathan
L3viathan / .block
Created September 18, 2017 11:47 — forked from mbostock/.block
Labeled Force Layout
license: gpl-3.0
@L3viathan
L3viathan / weighted_edit_distance.py
Created September 21, 2017 14:55
Super-simple length-independent word-based Levenshtein distance
def weighted_distance(x, y):
x, y = x.split(), y.split()
length = len(x+y)/2
return editdistance.eval(x, y)/length
@L3viathan
L3viathan / retry.py
Created April 30, 2018 08:59
Decorator to retry a function if it fails
def retry(tries):
def decorator(fn):
@functools.wraps(fn)
def wrapper(*args, **kwargs):
ex = None
for i in range(tries):
try:
result = fn(*args, **kwargs)
except Exception as e:
print("Failed...")
@L3viathan
L3viathan / LICENSE
Last active May 7, 2018 06:23
sort-of constant time multi-string matching
Copyright (c) 2018 L3viathan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all