2020-04-22_function-currying.html
001|
002|
003|
004|
005| def Add (a):
006| '''Add function.'''
007|
008| def f (b):
009| '''Secondary Add function.'''
010|
011| return (a+b)
012|
013|
014| return f
015|
016| r = Add(42)(21)
017| f = Add(42)
018|
019| r1 = f(21)
020| r2 = f(13)
021| r3 = f(42)
022|
023|
024| def Between (x):
025| '''Between function (two args).'''
026|
027| def f1 (a):
028| '''Secondary Between function.'''
029|
030| def f2 (b):
031| '''Tertiary Between function.'''
032| if x <= a: return False
033| if b <= x: return False
034|
035| return True
036|
037|
038| return f2
039|
040| return f1
041|
042| r = Between(35)(21)(42)
043| f = Between(35)
044|
045| r1 = f(21)(42)
046| r2 = f(42)(64)
047| r3 = f(50)(10)
048|
049|
050| def process_statement (expression):
051| '''Process an expression statement.'''
052| stack = []
053|
054| def process_node (tok):
055| '''Recursive node processor.'''
056|
057|
058| if isinstance(tok, function):
059| stack.append(tok)
060| return tok
061|
062|
063| if isinstance(tok, argument):
064|
065|
066| if len(stack) == 0:
067| return tok
068|
069|
070| f = stack.pop()
071| new_tok = f(tok())
072|
073|
074| return process_node(new_tok)
075|
076|
077| raise RuntimeError('Unknown nodetype!')
078|
079|
080| ret_val = argument('###ERR', None)
081|
082|
083| for token in expression:
084| ret_val = process_node(token)
085|
086|
087| return ret_val
088|
089|
090| class node (object):
091| '''Basic Node class.'''
092| def __init__ (self, name):
093| self.n = name
094|
095| def __str__ (self):
096| return self.n
097|
098|
099| class argument (node):
100| '''Argument Node class.'''
101| def __init__ (self, name, value):
102| super().__init__(name)
103| self.v = value
104|
105| def __call__ (self):
106| '''Calling the token returns its value.'''
107| return self.v
108|
109| def __repr__ (self):
110| return '{argument:{n:"%s", v:%s}}' % (self.n,self.v)
111|
112|
113| class function (node):
114| '''Function Node class.'''
115| def __init__ (self, name, function):
116| super().__init__(name)
117| self.f = function
118|
119| def __call__ (self, arg):
120| '''Calling executes the function with arg.'''
121| return self.f(arg)
122|
123| def __repr__ (self):
124| return '{function:{n:"%s", f:%s}}' % (self.n,self.f)
125|
126|
127| def fSin (a):
128| '''Sine function.'''
129|
130| return argument('$sin', sin(radians(a)))
131|
132| def fCos (a):
133| '''Cosine function.'''
134|
135| return argument('$cos', cos(radians(a)))
136|
137| def fAdd (a):
138| '''Add function.'''
139| def f (b):
140| '''Secondary Add function.'''
141|
142| return argument('$add', a+b)
143|
144|
145| return function('_Add', f)
146|
147| def fSub (a):
148| '''Sub(tract) function.'''
149| def f (b):
150| '''Secondary Sub function.'''
151|
152| return argument('$sub', a-b)
153|
154|
155| return function('_Sub', f)
156|
157| def fMul (a):
158| '''Mul(tiply) function.'''
159| def f (b):
160| '''Secondary Mul function.'''
161|
162| return argument('$mul', a*b)
163|
164|
165| return function('_Mul', f)
166|
167|
168|