From 1b1ad5a25dc56b8d483f51402f8dcd796800935a Mon Sep 17 00:00:00 2001 From: Tobias Weise Date: Sat, 11 May 2024 23:42:45 +0200 Subject: [PATCH] add files --- func.py | 54 ++++++++++++++++++++++ func/__init__.py | 7 +++ func/__init__.pyc | Bin 0 -> 263 bytes func/debug.py | 20 +++++++++ func/func.py | 46 +++++++++++++++++++ func/func.pyc | Bin 0 -> 2864 bytes func/io.py | 58 ++++++++++++++++++++++++ func/lib.py | 49 ++++++++++++++++++++ func/list.py | 111 ++++++++++++++++++++++++++++++++++++++++++++++ func/monad.py | 28 ++++++++++++ func/path.py | 20 +++++++++ func/string.py | 30 +++++++++++++ 12 files changed, 423 insertions(+) create mode 100644 func.py create mode 100644 func/__init__.py create mode 100644 func/__init__.pyc create mode 100644 func/debug.py create mode 100644 func/func.py create mode 100644 func/func.pyc create mode 100644 func/io.py create mode 100644 func/lib.py create mode 100644 func/list.py create mode 100644 func/monad.py create mode 100644 func/path.py create mode 100644 func/string.py diff --git a/func.py b/func.py new file mode 100644 index 0000000..07dcde7 --- /dev/null +++ b/func.py @@ -0,0 +1,54 @@ + +#==================================================================== +#===================== Function Decorators ========================== +#==================================================================== + +def log(f): + def g(*args): + r = f(*args) + print("Calculating: %s(%s) = %s" % ( str(f).split(" ")[1], str(",".join(map(str,list(args)))), str(r)) ) + return r + return g + +def maybe(f): + def g(*args): + try: return f(*args) + except: return None + return g + +def memoize(f): + mem = {} + def g(*args): + if args in mem: + print("Remember: f%s = %s" % (args,mem[args])) + return mem[args] + r = f(*args) + if r != None: + print("Memoize: f%s = %s" % (args,r)) + mem[args] = r + return r + return g + +#docurry +def curryD(n): + def deco(f): + if n == 1: return f + return lambda x: curry(fix(f, x), n-1) + return deco + + +#========================================================== +#===================== Functions ========================== +#========================================================== + +#======== Composition ================ +def fix(f, x): + return lambda *args: f(*([x]+list(args))) + +def curry(f, n): + if n == 1: return f + return lambda x: curry(fix(f, x), n-1) + +def comp(g,f): + return lambda *args: g(f(*args)) + diff --git a/func/__init__.py b/func/__init__.py new file mode 100644 index 0000000..e1000d0 --- /dev/null +++ b/func/__init__.py @@ -0,0 +1,7 @@ + +from func.func import * +from func.list import * +from func.string import * +from func.io import * + + diff --git a/func/__init__.pyc b/func/__init__.pyc new file mode 100644 index 0000000000000000000000000000000000000000..9f5b589cbd98ae20387d264a6f53b25c34894681 GIT binary patch literal 263 zcmYL^%?iRW41n8B{F(S3MP$8;hzIp3gNdM*g57k1t&=r7>|uNapU#sfO<|j9^YsfM zg!=DU@OeEinziB4yP>oTLPD3boUvnTyf?2mpUh*i@M!?P6oQkX!~7ry&L@OhZFK1H zuX3=6)2?T+0&~YvS3@aC*&lXErOe7Al?522Ok}LHDupY8)tTJxPBJ>nJuQhE>2^t_ azyK;0J{la;_^K$AI#1W`n|4SDxH~_c^E^NR literal 0 HcmV?d00001 diff --git a/func/debug.py b/func/debug.py new file mode 100644 index 0000000..d9a99c0 --- /dev/null +++ b/func/debug.py @@ -0,0 +1,20 @@ + +""" +def log(func): + def new_func(*args): + r = func(*args) + print(">>> " + str(func).split(" ")[1] + str(list(args)) + " -> " + str(r)) + return r + return new_func +""" + + + +def log(f): + def g(*args): + r = f(*args) + print("Calculating: %s(%s) = %s" % ( str(f).split(" ")[1], str(",".join(map(str,list(args)))), str(r)) ) + return r + return g + + diff --git a/func/func.py b/func/func.py new file mode 100644 index 0000000..ae2ca3d --- /dev/null +++ b/func/func.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +from functools import * +from itertools import * + + +def compose(g,f): + return lambda *args: g(f(*args)) + + +def comp(*args): + return reduce(compose, args) + +def maybe(f): + def g(*args): + try: return f(*args) + except: return None + return g + +def memoize(f): + mem = {} + def g(*args): + if args in mem: + print("Remember: f%s = %s" % (args,mem[args])) + return mem[args] + r = f(*args) + if r != None: + print("Memoize: f%s = %s" % (args,r)) + mem[args] = r + return r + return g + +def curryD(n): + def deco(f): + if n == 1: return f + return lambda x: curry(fix(f, x), n-1) + return deco + +def fix(f, x): + return lambda *args: f(*([x]+list(args))) + +def curry(f, n): + if n == 1: return f + return lambda x: curry(fix(f, x), n-1) + + + diff --git a/func/func.pyc b/func/func.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c2547b5e50ee7f29fb7515cfb1845f3dea7d5ee9 GIT binary patch literal 2864 zcmcgu+iuf95S@+FG%39R6%-IiL8uTwB5yn(sCX!s7b4^!%H;v2w$oaTW8`(E2EkJi z6p0_;fsf&v_y9O(HaFS|Az32IOlH@+GrQ-^jN|=WnEUqQ`>Q~PpAvrWL+l1rh_8{J z$g`nkV3p*f$g7fso*JHnWi`qY&ZsdXVMUFKgtKbQN?28+Dq&69jXC&^f8oF7txG-5^Uv)mY$ZoxNZfaYY&VZh`bm1Dza~|mMdBfC~shrR@#rIF2?x@Aq8%y)GorCWe$k(WD#;4 z&2DZSxIoODEG3=6W<**ouP0{73yy$#5f5UKFXnI^9YvQo(~(xw8yLuw10y>X$*z$S zH-tWqVm$lkF`jYIJ{VP;a|!5oJjq@Q+6111Lbn`-+i|zAv+V|1Z`sq(ew0MXW|ZBj zcUNuwUVYWtQ<#1dC29OFn#?--2(xB|W~kP0=El52PPcHUU>AT`X*~m9+F8WRn`Kjl zo;OQo!8j@zPjg5YAD%nG%>`NtD;pOf1gs6qA{A3Hr)x)f{(JE-Ek>!KuIiWo8(?*( zB-u$;Q5&^4Y_uiUXqBIs`!pU6=7&1C0K;_?Yz56|D9+zGZ7o`}y47`Brfaa9WdmiW z8}BKTO5NTRbqvv07z^O$^N?2{7BZ|uI(Egb7T+r_mw3$+PZ3CTQF;> %s %s -> %s" % (str(f).split(" ")[1], str(list(args)), str(r))) + return r + return g + +#========= Composition ========= +def fix1(f, v): + def g(*args): + r = f(*([v]+list(args))) + return r + return g + +def curry(func, numArgs): + if numArgs == 1: return func + def f1(v): + return curry(fix1(func, v), numArgs-1) + return f1 + +def comp(g,f): + def r(*args, **kwargs): + return g(f(*args, **kwargs)) + return r + +#======== Operations =========== +def add(a, b): + return a + b + +def multi(a, b): + return a * b + + + + +def Monade(): + #Monade, (M a -> (a -> M b) -> M a) + def returm(x): + return [x] + def bind(m, f): + return returm(list(map(f,m))) + return returm, bind + + + + + + diff --git a/func/list.py b/func/list.py new file mode 100644 index 0000000..7c8d7a7 --- /dev/null +++ b/func/list.py @@ -0,0 +1,111 @@ + +def zipWith(f,ls,xs): + for i in range(len(ls)): + yield f(ls[i],xs[i]) + +def all(f, ls): + for x in ls: + if not f(x): return False + return True + +def splitWhen(f, ls): + b, rs, xs = False, [], [] + for x in ls: + if f(x): b = True + if b: xs += [x] + else: rs += [x] + return rs, xs + +def tail(ls): + return list(ls)[1:] + +def take(n,ls): + for x in ls: + if n < 1: break + yield x + n -= 1 + +def drop(n,ls): + for x in ls: + if n < 1: yield x + n -= 1 + +def splitAt(i, ls): + ls = list(ls) + return ls[:i], ls[i:] + +def partBy(n, ls): + rs = [] + while len(ls) >= n: + xs, ls = splitAt(n,ls) + rs.append(list(xs)) + return rs + +def concat(ls): + for xs in ls: + for x in xs: + yield x + + +def dropWhile(f, ls): + b = False + for x in ls: + if not b: + if f(x): continue + else: + b = True + if b: + yield x + +def unfold(f, x): + """ + def f(x): + if x > 0: return x, x-1 + + print(list(unfold(f, 5))) + """ + while True: + r = f(x) + if not r: break + v, x = r + yield v + + +def nub(ls): + rs = [] + for x in ls: + if x not in rs: + rs.append(x) + return rs + + +def cartProd(ls, xs): + for i in ls: + for x in xs: + if not i is x: + yield i, x + + +def numEqPrefix(ls, xs): + acc = 0 + for i in range(len(ls)): + try: + if ls[i] == xs[i]: + acc += 1 + except: + break + return acc + + +def pairs2list(ls): + rs = [] + for a, b in ls: + if not a in rs: + rs.append(a) + if not b in rs: + rs.append(b) + return rs + + + + diff --git a/func/monad.py b/func/monad.py new file mode 100644 index 0000000..155818e --- /dev/null +++ b/func/monad.py @@ -0,0 +1,28 @@ + +class Monad: + + def __init__(self, bind, value): + self.__bind = bind + self.__m = unit(value) + + def bind(self, f): + self.__m = self.__bind(self.__m, f) + return self + + def __lshift__(self, f): + self.__m = self.__bind(self.__m, f) + return self + + +""" +if __name__ == "__main__": + Monad(bind, 1) << (lambda x: x+10) << (lambda x: 2*x) + + add = lambda a, b: a+b + print(list(zip([1,2,3],[4,5,6]))) + + print(list(zipWith(add,[1,2,3],[4,5,6]))) +""" + + + diff --git a/func/path.py b/func/path.py new file mode 100644 index 0000000..42e4690 --- /dev/null +++ b/func/path.py @@ -0,0 +1,20 @@ + + + +def path2FileName(path : "Path"): + return path.split("/")[-1] + +def fname2Ext(fname): + return fname.split(".")[-1] + +def fname2name(fname): + return "".join(fname.split(".")[:-1]) + + + + + + + + + diff --git a/func/string.py b/func/string.py new file mode 100644 index 0000000..886e546 --- /dev/null +++ b/func/string.py @@ -0,0 +1,30 @@ +import re + +def isMatch(rgx, s): + return bool(re.compile(rgx).match(s)) + +def strconcat(ls): + return "".join(ls) + +def lines(s): + return s.split("\n") + +def unlines(ls): + return "\n".join(map(str,ls)) + +def lines(s): + return s.split("\n") + +def split(sign, ls): + rs = [] + curr = [] + for x in ls: + if x==sign: + rs.append(curr) + curr = [] + else: + curr.append(x) + rs.append(curr) + return rs + +