Skip to content

Instantly share code, notes, and snippets.

from abc import abstractmethod
from itertools import chain, imap
from typing import Any, Iterable
from typing import Callable, Generic, List
from typing import TypeVar
A = TypeVar('A')
B = TypeVar('B')
C = TypeVar('C')
@unthingable
unthingable / real.scala
Last active October 20, 2015 22:54
structural variance in json objects
object lisMessages {
case class PayloadData(name: String, newValue: JsValue, oldValue: JsValue)
case class LisPayloadA[C:Format, D:Format] (
uuid : String,
timestamp : String,
source : String,
context : C,
model : String,
@unthingable
unthingable / tabbar.hs
Last active August 29, 2015 14:02
simple tabbed panel example
import Window
import Mouse
import Graphics.Input (Input, input, clickable)
import Dict
-- UTIL
enumerate : [a] -> [(Int, a)]
enumerate l = zip [1..length l] l
@unthingable
unthingable / euclidean.clj
Last active March 22, 2016 19:17
Euclidean Rhythm generator in Clojure
(defn split-seq [s]
"Extract a tail of all same elements: [1 1 0 0 0] -> [[1 1] [0 0 0]]"
(let [l (last s)]
(split-with #(not= l %) s)))
(defn recombine
"Distribute tail: [[1] [1] [1] [0] [0]] -> [[1 0] [1 0] [1]]"
([a b] [a b])
([a b c] [a b c])
([a b c & more]
@unthingable
unthingable / solution.py
Last active January 3, 2016 02:09
nuclear class splitting (https://github.com/nucleic/atom) Problem: Atom subclasses require all attributes to be Fields (subclasses of Member). In practice this means you can neither drop atomic fields into an existing class, not create new class attributes inside methods; quite annoying. Atomizer will split a class into a real Atom and a "deatom…
from atom.api import Atom, Int
# This is not OK:
class A(Atom):
x = Int()
y = 0
def f(self):
self.y = 1 # not OK
self.z = 5 # very not OK!
@unthingable
unthingable / init.el
Last active December 31, 2015 19:09
color-theme sensitive hl-line
;; (set-face-background hl-line-face "medium purple")
;; fix hl-line color dynamically:
(require 'hexrgb)
(defun my-fix-hl-line-color ()
"Adjust hl-line color relative to current color theme"
(interactive)
(let*
((color (face-attribute 'default :background))
(value (hexrgb-value color))
(threshold 0.32)
@unthingable
unthingable / mytemplate.json
Last active December 18, 2015 14:48
es dynamic templates
{
"mytemplate": {
"mappings": {
"_default_": {
"_all": {
"enabled": false
},
"_ttl": {
"default": "1d",
"enabled": true
@unthingable
unthingable / mapping.json
Last active December 18, 2015 01:59
elasticsearch ttl: why y no work?
{
"foobar" : {
"pwpage" : {
"dynamic_templates" : [ {
"string_template" : {
"mapping" : {
"index" : "not_analyzed",
"type" : "string"
},
"match" : "*",
@unthingable
unthingable / cache.py
Created May 21, 2013 01:00
local cache
Stamped = namedtuple('Stamped', 'stamp obj')
cache = {}
def cached(ttl, func, *args, **kw):
'Simple inline cache for function calls'
key = (func.__name__,) + args
entry = cache.get(key, None)
AbstractBase(models.Model):
foo = somefield
class Base(AbstractBase):
c1 = models.OneToOneField('C1', null=True, blank=True)
c2 = models.OneToOneField('C2', null=True, blank=True)
def c(self):
#only one additional query to get the base
if self.c1_id: