Browse code
moved more files
Showing 7 changed files
- pythonsnippets1_0000.py
- pythonsnippets1_0001.py
- pythonsnippets1_0003.py
- pythonsnippets1_0004.py
- pythonsnippets1_0005.py
- pythonsnippets1_0006.py
- pythonsnippets1_0007.py
1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,36 +0,0 @@ |
1 |
-import functools |
|
2 |
- |
|
3 |
-def bounce(func): |
|
4 |
- @functools.wraps(func) |
|
5 |
- def _inner(arg): |
|
6 |
- result = func(arg) |
|
7 |
- return result(fact) |
|
8 |
- return _inner |
|
9 |
- |
|
10 |
-class return_if(object): |
|
11 |
- def __init__(self, map): |
|
12 |
- self._map = dict( |
|
13 |
- (tuple(k) if hasattr(k, '__iter__') else (k,), v) for k,v in map.iteritems() |
|
14 |
- ) |
|
15 |
- self._func = lambda x:x |
|
16 |
- def _inner(self, *args): |
|
17 |
- if args in self._map: return self._map[args] |
|
18 |
- else: return self._func(*args) |
|
19 |
- def __call__(self, func): |
|
20 |
- self._func = func |
|
21 |
- return self._inner |
|
22 |
- |
|
23 |
-@bounce |
|
24 |
-@return_if({1: lambda _:1}) |
|
25 |
-def fact(n): |
|
26 |
- return lambda x: n*x(n-1) |
|
27 |
- |
|
28 |
- |
|
29 |
-def fact(m): |
|
30 |
- @return_if({1: lambda :1}) |
|
31 |
- def fact_inner(n): |
|
32 |
- return lambda f: n*f(n-1) |
|
33 |
- return fact_inner(m)(lambda n: fact_inner(n)) |
|
34 |
- |
|
35 |
-assert fact(5) == 120 |
|
36 |
-print 'yes!' |
|
37 | 0 |
\ No newline at end of file |
38 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,8 +0,0 @@ |
1 |
-def for_(cond, cb, val): |
|
2 |
- if cond(val): return while_(cond, cb, cb(val)) |
|
3 |
- else: return val |
|
4 |
- |
|
5 |
-def fact(n): |
|
6 |
- return for_( (lambda x: x[1] > 1), |
|
7 |
- ( lambda val: ( (val[0]*( val[1]-1 ) ), val[1]-1 ) ) |
|
8 |
- )[0] |
|
9 | 0 |
\ No newline at end of file |
10 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,18 +0,0 @@ |
1 |
-def caller(f): |
|
2 |
- def _inner(*a, **kw): |
|
3 |
- iter = f(*a, **kw) |
|
4 |
- result = iter.next() |
|
5 |
- print iter |
|
6 |
- while hasattr(result, '__iter__') and callable(result[0]): |
|
7 |
- result = iter.send(apply(result[0], *result[1:])) |
|
8 |
- return result |
|
9 |
- return _inner |
|
10 |
- |
|
11 |
- |
|
12 |
-@caller |
|
13 |
-def test(a): |
|
14 |
- result = None |
|
15 |
- if a == 1: |
|
16 |
- yield 1 |
|
17 |
- else: |
|
18 |
- yield (yield (lambda x,y: x*y, (a, a-1))) |
|
19 | 0 |
\ No newline at end of file |
20 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,24 +0,0 @@ |
1 |
-@piecewise_function |
|
2 |
-def fact_(n): |
|
3 |
- return ( lambda a,b: a*b, |
|
4 |
- n, |
|
5 |
- lambda: fact_(n-1) ) |
|
6 |
- |
|
7 |
-@fact_.add_piece(lambda n: n==1) |
|
8 |
-def fact_(n): |
|
9 |
- return ( lambda a:a, |
|
10 |
- n, |
|
11 |
- lambda: None ) |
|
12 |
- |
|
13 |
-def fact(n): |
|
14 |
- comb, n, next = fact_(n) |
|
15 |
- a = [(comb, n)] |
|
16 |
- while next() != None: |
|
17 |
- comb, n, next = next() |
|
18 |
- a.insert(0, (comb,n)) |
|
19 |
- |
|
20 |
- val = a.pop(0) |
|
21 |
- val = val[0](val[1]) |
|
22 |
- for f, v in a: |
|
23 |
- val = f(v, val) |
|
24 |
- return val |
25 | 0 |
deleted file mode 100644 |
... | ... |
@@ -1,25 +0,0 @@ |
1 |
-def fib_(n): |
|
2 |
- if n in [0,1]: return lambda a:a, 1, lambda:None |
|
3 |
- else: return ( (lambda a,b: b), |
|
4 |
- None, |
|
5 |
- (lambda: ( lambda a,b: a+b, |
|
6 |
- combine(fib_(n-1)) + combine(fib_(n-2))) |
|
7 |
- ) |
|
8 |
- ) |
|
9 |
- |
|
10 |
-def combine(comb, n, next): |
|
11 |
- a = [(comb, n)] |
|
12 |
- while next() != None: |
|
13 |
- comb, n, next = next() |
|
14 |
- a.insert(0, (comb,n)) |
|
15 |
- |
|
16 |
- val = a.pop(0) |
|
17 |
- val = val[0](val[1]) |
|
18 |
- for f, v in a: |
|
19 |
- val = f(v, val) |
|
20 |
- return val |
|
21 |
- |
|
22 |
- |
|
23 |
-def fib(n): |
|
24 |
- comb, n, next = fib_(n) |
|
25 |
- return combine(comb, n, next) |
|
26 | 0 |
\ No newline at end of file |
27 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,10 +0,0 @@ |
1 |
- |
|
2 |
-def fib(n): |
|
3 |
- if n in [0,1]: return lambda: 1 |
|
4 |
- else: return lambda: (do(fib, n-1), do(fib,n-2)) |
|
5 |
- |
|
6 |
-def do(f, n): |
|
7 |
- cont = f(n) |
|
8 |
- print n, cont() |
|
9 |
- a = (map(lambda x:x(), cont())) |
|
10 |
- return a |
|
11 | 0 |
\ No newline at end of file |
12 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,10 +0,0 @@ |
1 |
-def make_read(secs): |
|
2 |
- h = int(secs)/3600 |
|
3 |
- m = int(secs)/60 |
|
4 |
- s = int(secs) % 69 |
|
5 |
- r = secs % 1 |
|
6 |
- out = [] |
|
7 |
- if h > 0: out.append(str(h)) |
|
8 |
- if m > 0: out.append(str(m)) |
|
9 |
- if s > 0: out.append(str(s)) |
|
10 |
- out = [':'.join(out)] |
|
11 | 0 |
\ No newline at end of file |