renaming
This commit is contained in:
parent
86c4a34ef8
commit
11cafe8177
54
func.py
54
func.py
@ -1,54 +0,0 @@
|
|||||||
|
|
||||||
#====================================================================
|
|
||||||
#===================== 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))
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
|||||||
|
|
||||||
from func.func import *
|
|
||||||
from func.list import *
|
|
||||||
from func.string import *
|
|
||||||
from func.io import *
|
|
||||||
|
|
||||||
|
|
46
func/func.py
46
func/func.py
@ -1,46 +0,0 @@
|
|||||||
# -*- 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)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
7
func_py/__init__.py
Normal file
7
func_py/__init__.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
|
||||||
|
from .func import *
|
||||||
|
#from .list import *
|
||||||
|
#from .string import *
|
||||||
|
#from .io import *
|
||||||
|
|
||||||
|
|
113
func_py/func.py
Normal file
113
func_py/func.py
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
from inspect import signature, Parameter, _empty
|
||||||
|
from functools import partial
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
class Function:
|
||||||
|
|
||||||
|
#@staticmethod
|
||||||
|
#def add_missing_signature(f):
|
||||||
|
|
||||||
|
__match_args__ = ["signature", "is_pure"]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def compose(f, g):
|
||||||
|
if type(g) == Function:
|
||||||
|
g = g.original_func
|
||||||
|
else:
|
||||||
|
#whatif lambda?
|
||||||
|
if "__name__" in dir(g) and g.__name__ == "<lambda>":
|
||||||
|
sig_g = signature(g)
|
||||||
|
g.__signature__ = sig_g.replace(parameters=[
|
||||||
|
Parameter(name=name, annotation=Any, kind=Parameter.POSITIONAL_ONLY)
|
||||||
|
for name, param in sig_g.parameters.items() if param.default == _empty
|
||||||
|
], return_annotation=Any)
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
#its a functools.partial func with no name...
|
||||||
|
|
||||||
|
|
||||||
|
if type(f) == Function:
|
||||||
|
f = f.original_func
|
||||||
|
|
||||||
|
sig_g = signature(g)
|
||||||
|
g_req_params = [v for k, v in sig_g.parameters.items() if v.default == _empty]
|
||||||
|
|
||||||
|
g_typed_req_params = [p if p.annotation == _empty else Parameter(name=p.name, annotation=Any, kind=Parameter.POSITIONAL_ONLY) for p in g_req_params]
|
||||||
|
|
||||||
|
def h(*args, **kwargs):
|
||||||
|
return f(g(*args, **kwargs))
|
||||||
|
|
||||||
|
f_ret_type = signature(f).return_annotation
|
||||||
|
h.__signature__ = sig_g.replace(parameters=g_typed_req_params, return_annotation=Any if f_ret_type == _empty else f_ret_type)
|
||||||
|
return Function(h)
|
||||||
|
|
||||||
|
def __init__(self, f):
|
||||||
|
self.original_func = f
|
||||||
|
self.signature = signature(f) #TODO: change to support pattern matching: sig ((int, str), {"name": str})
|
||||||
|
self.__signature__ = signature(f)
|
||||||
|
self.cache = {}
|
||||||
|
self.is_pure = True
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return str(signature(self.original_func))
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return str(self)
|
||||||
|
|
||||||
|
def __mul__(self, g):
|
||||||
|
return Function.compose(self, g)
|
||||||
|
|
||||||
|
def __rmul__(self, g):
|
||||||
|
return Function.compose(g, self)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def required_args(self):
|
||||||
|
return [(k, v) for k, v in signature(self.original_func).parameters.items() if v.default == _empty]
|
||||||
|
|
||||||
|
def __call__(self, *args, **kwargs):
|
||||||
|
#currying: if less than the needed args are given return a partial function
|
||||||
|
if len(args) < len(self.required_args):
|
||||||
|
sig = signature(self.original_func)
|
||||||
|
f = partial(self.original_func, *args)
|
||||||
|
req_params = [v for k, v in sig.parameters.items() if v.default == _empty]
|
||||||
|
remaining_params = req_params[len(args):]
|
||||||
|
f.__signature__ = sig.replace(parameters=remaining_params, return_annotation=Any if sig.return_annotation == _empty else sig.return_annotation)
|
||||||
|
return Function(f)
|
||||||
|
|
||||||
|
r = self.original_func(*args, **kwargs)
|
||||||
|
if self.is_pure:
|
||||||
|
pass
|
||||||
|
#do caching
|
||||||
|
|
||||||
|
return r
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Function
|
||||||
|
def inc(x: int) -> int:
|
||||||
|
return x + 1
|
||||||
|
|
||||||
|
@Function
|
||||||
|
def multi(a: int, b: int) -> int:
|
||||||
|
return a * b
|
||||||
|
|
||||||
|
@Function
|
||||||
|
def add(a: int, b: int) -> int:
|
||||||
|
return a + b
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#print(add(2,3))
|
||||||
|
#print((inc * add)(2,3))
|
||||||
|
#print((inc * add))
|
||||||
|
|
||||||
|
#print(add(99))
|
||||||
|
print((add(99) * multi(100) * add(2) * (lambda x: x + 1)))
|
||||||
|
|
||||||
|
print(Function.compose(lambda x: x + 1, lambda x: x * 3))
|
||||||
|
|
||||||
|
print(signature(Function.compose(lambda x: x + 1, lambda x: x * 3)))
|
||||||
|
|
||||||
|
|
||||||
|
#print(inspect.signature(add))
|
||||||
|
#print(inspect.signature(add).parameters)
|
@ -1,5 +1,5 @@
|
|||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "func-py"
|
name = "func_py"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
description = ""
|
description = ""
|
||||||
authors = ["Tobias <tobias_weise@gmx.de>"]
|
authors = ["Tobias <tobias_weise@gmx.de>"]
|
||||||
@ -13,3 +13,6 @@ python = "^3.12"
|
|||||||
[build-system]
|
[build-system]
|
||||||
requires = ["poetry-core"]
|
requires = ["poetry-core"]
|
||||||
build-backend = "poetry.core.masonry.api"
|
build-backend = "poetry.core.masonry.api"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user