Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env bash
# tested with bash 4.3
cd_into() {
(cd $1)
}
list_directory() {
(ls $1 > /dev/null)
@rik0
rik0 / metamadness.py
Last active December 27, 2015 18:09
Metaclass madness.
import functools as ft
class IAmAPig(type):
@classmethod
def _make_aux(cls, base, k, attribute):
super_meth = getattr(base, k, None)
if super_meth is not None:
@ft.wraps(attribute)
def aux(self, *args, **kwargs):
super_meth(self, *args, **kwargs)
import objc
import AddressBook as ab
import pprint as pp
def pythonize(objc_obj):
if isinstance(objc_obj, objc.pyobjc_unicode):
return unicode(objc_obj)
elif isinstance(objc_obj, ab.NSDate):
return objc_obj.description()
@rik0
rik0 / tree_make_mr.py
Last active December 21, 2015 15:39
Example of function to create some classes of methods to operate on trees. Doctests are annoying, but for now they suffice. Besides, this is not as tested as it should.
def make_mr(reduce_, f, base):
def mr(self):
if not self.children:
return base(self)
else:
mapped = map(f, self.children)
red = reduce_(mapped)
return red
# return reduce_(f(child) for child in self.children)
return mr
@rik0
rik0 / trees.cc
Last active December 16, 2015 06:49
Tree exercise
/*
* =====================================================================
*
* Filename: trees.cc
*
* Version: 1.0
* Created: 04/14/2013 11:20:59
* Revision: none
* Compiler: gcc
*
@rik0
rik0 / subplot_weekdata.py
Created April 24, 2012 12:20 — forked from zarch/subplot_weekdata.py
Plot week data as subplots
# -*- coding: utf-8 -*-
"""
____________________________________________________________
| +--------------------------------------------------------+ |
| ! ^ ___ --- ! |
| ! / \ _--_ / \ /\ / \ ! |
| ! / \__/ \ / \_-___-- \ / \ ! |
| ! / \___/ --__/ --__- ! |
| +-------+-------+-------+-------+-------+-------+--------+ |
| 04/02 04/03 04/04 04/02 04/03 04/04 04/04 |
@rik0
rik0 / test_graph.py
Created April 21, 2012 18:05 — forked from anonymous/test_graph.py
Simple test for a Graph class
import unittest
from graph import Graph
class TestGraph(unittest.TestCase):
def test_init(self):
@rik0
rik0 / rusty_method.py
Created April 15, 2012 15:27
Funny decorator
import functools
class rusty_method(object):
def __init__(self, hits, chain_effect):
self.hits = hits
self.chain_effect = chain_effect
self.hit_memory = []
def __call__(self, func):
def aux(that, *args):
self.hit_memory.append(args)
(ns recursive-tricks.recursive-tricks)
(defn uuid [] (str (java.util.UUID/randomUUID)))
(defn tail-recursive [f]
(let [state (ref {:func f :first-call true :continue (uuid)})]
(fn [& args]
(let [cont (:continue @state)]
(if (:first-call @state)
(let [fnc (:func @state)]
@rik0
rik0 / unfold-examples.clj
Created August 31, 2011 17:22
Unfold in Clojure
(ns unfold-examples
(:use [unfold-util]))
(defn unfold-tails [s]
(unfold empty? identity rest s))
(defn unfold-map [f s]
(unfold empty? #(f (first %)) rest s))