2020-04-22_function-currying.html


001| #:name::Function Currying
002| #:link::https://thehardcorecoder.com/2020/04/22/function-currying/
003| #:date::2020-04-22
004| #:code::
005| def Add (a):
006|     '''Add function.'''
007| 
008|     def f (b):
009|         '''Secondary Add function.'''
010|         # Return Add result...
011|         return (a+b)
012| 
013|     # Return secondary function...
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|             # Return Between result...
035|             return True
036| 
037|         # Return tertiary function...
038|         return f2
039|     # Return secondary function...
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| #:code::
050| def process_statement (expression):
051|     '''Process an expression statement.'''
052|     stack = []
053| 
054|     def process_node (tok):
055|         '''Recursive node processor.'''
056| 
057|         # If token is a function, push it on the stack...
058|         if isinstance(tok, function):
059|             stack.append(tok)
060|             return tok
061| 
062|         # If token is a parameter, apply it to a function...
063|         if isinstance(tok, argument):
064| 
065|             # (Unless there aren't any; then we're done)...
066|             if len(stack) == 0:
067|                 return tok
068| 
069|             # Execute the function!
070|             f = stack.pop()
071|             new_tok = f(tok())
072| 
073|             # And recurse on the result...
074|             return process_node(new_tok)
075| 
076|         # ERROR: Unknown node type...
077|         raise RuntimeError('Unknown nodetype!')
078| 
079|     # Execute the nodes of the statement...
080|     ret_val = argument('###ERR', None)
081| 
082|     # Process nodes in expression...
083|     for token in expression:
084|         ret_val = process_node(token)
085| 
086|     # Return the (final) return value...
087|     return ret_val
088| 
089| #:code::
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| #:code::
127| def fSin (a):
128|     '''Sine function.'''
129|     # Return sine value...
130|     return argument('$sin', sin(radians(a)))
131| 
132| def fCos (a):
133|     '''Cosine function.'''
134|     # Return cosine value...
135|     return argument('$cos', cos(radians(a)))
136| 
137| def fAdd (a):
138|     '''Add function.'''
139|     def f (b):
140|         '''Secondary Add function.'''
141|         # Return Add value...
142|         return argument('$add', a+b)
143| 
144|     # Return secondary Add function...
145|     return function('_Add', f)
146| 
147| def fSub (a):
148|     '''Sub(tract) function.'''
149|     def f (b):
150|         '''Secondary Sub function.'''
151|         # Return Sub value...
152|         return argument('$sub', a-b)
153| 
154|     # Return secondary Sub function...
155|     return function('_Sub', f)
156| 
157| def fMul (a):
158|     '''Mul(tiply) function.'''
159|     def f (b):
160|         '''Secondary Mul function.'''
161|         # Return Mul value...
162|         return argument('$mul', a*b)
163| 
164|     # Return secondary Mul function...
165|     return function('_Mul', f)
166| 
167| #:end:code::
168|