Skip to content

Instantly share code, notes, and snippets.

@depp
depp / Enum.hs
Created April 1, 2012 23:07
Demonstration of incorrect (Enum x, Enum y) => Enum (x, y) instance
-- See http://stackoverflow.com/questions/9967790/instance-in-haskell
instance (Enum x, Enum y) => Enum (x, y) where
fromEnum (x,y) =
k^2 + 2*j + if permuted then 1 else 0
where
k = max (fromEnum x) (fromEnum y)
j = min (fromEnum x) (fromEnum y)
permuted = (fromEnum y) > (fromEnum x)
toEnum n =
import os, time
def list_modules(path):
for name in os.listdir(path):
base, ext = os.path.splitext(name)
if '.' in base or base.startswith('__'):
continue
if ext == '.py':
yield base
elif not ext:
if not os.path.exists(os.path.join(base, '__init__.py')):
@depp
depp / gtk.c
Created February 13, 2013 04:34
/* Copyright 2012 Dietrich Epp <depp@zdome.net> */
#if defined(__GNUC__) && \
((__GNUC__ == 4 && __GNUC_MINOR >= 6) || __GNUC__ > 4)
#define HAVE_DPUSH 1
#endif
#include "keycode/keycode.h"
#include "keycode/keytable.h"
#include "sg/audio_system.h"
#include "sg/clock.h"
@depp
depp / gist:8378663
Created January 12, 2014 00:05
Iterate over a string
fn main() {
let mut x = "hello".chars();
while (true) {
match x.next() {
Some('o') => return,
Some(c) => println!("Char: {}", c),
None => return
}
}
}
@depp
depp / test.cpp
Last active August 29, 2015 14:02
#include <iostream>
#include <vector>
#undef CUSTOM_BUFFER
#if defined CUSTOM_BUFFER
static const int BUFFER_SIZE = 1024 * 16;
#endif
int main()
@depp
depp / gist:87a5f1079556eb0ed07e
Created November 7, 2014 22:06
Why you shouldn't call super(self.__class__, self).__init__()
class A(object):
def __init__(self):
print('A')
class B(A):
def __init__(self):
print('B')
super(self.__class__, self).__init__()
class C(B):
@depp
depp / Makefile
Created February 12, 2015 22:59
Copy memory, reversing bits
run: test.o main.o
cc -o $@ $^
test.o: test.c
cc -Wall -Wextra -O3 -c $< -o $@
main.o: main.c
cc -Wall -Wextra -O2 -c $< -o $@
@depp
depp / test.cpp
Last active August 29, 2015 14:17
As you can see, only extern_func() is accessible from other translation units.
$ c++ -std=c++11 -c test.cpp
$ nm -g test.o | c++filt
0000000000000000 T extern_func()
$ nm test.o | c++filt
0000000000000000 T extern_func()
0000000000000006 t static_func()
000000000000000c t (anonymous namespace)::anonymous_namespace_func()
@depp
depp / info.txt
Created April 30, 2015 20:56
Race condition example
This is what happens when I run the program. Note that 2 and 4 appear twice, but 0 and 1 never appear. The output is non-deterministic, because there is a race condition, the output could be almost anything.
Arg = 2
Arg = 4
Arg = 3
Arg = 5
Arg = 6
Arg = 2
Arg = 7
Arg = 8
Notice how each number appears only once.
The order is still scrambled, but we don't care about the order.
Arg = 1
Arg = 4
Arg = 2
Arg = 6
Arg = 3
Arg = 7