====== Interesting code snippets ======
===== (C) Fibonacci One-liner =====
#include
int main(void)
{
long x = 1, y = 1;
do { printf("%d\\n%d\\n", x, y); } while ( (x += y) && (y += x) );
}
#include
int main(void)
{
for ( unsigned int x = 1, y = 1 ; (x += y) && (y < x) && (y += x) && (x < y) ; printf("%d\\n%d\\n", x, y) ) continue;
}
===== (Python) String Generator =====
def string_generator(chars, length):
positions = [0] * (length + 1)
while positions[0] == 0:
yield ''.join(chars[x] for x in positions[1:])
pos = length
positions[pos] = (positions[pos] + 1) % len(chars)
while positions[pos] == 0 and pos > 0:
pos -= 1
positions[pos] = (positions[pos] + 1) % len(chars)
===== (Python) Partial instantiation factory class decorator =====
import functools
def partial_inst_factory(cls):
return functools.partial(functools.partial, cls)
@partial_inst_factory
class Foo:
def __init__(self, *args, **kwargs):
print 'Foo(*%s, **%s)' % (args, kwargs)
>>> f = Foo(1, 2, a=3)
>>> inst = f(4, 5, b=6)
Foo(*(1, 2, 4, 5), **{'a': 3, 'b': 6})