I've made nested function that takes 3 separate arguments - each function takes one: (arg1)(operation)(arg2)
def simple_functional_calc(arg1: int):
def inner_functional_calc(operation: str):
import operator
ops = {
'+': operator.add,
'-': operator.sub
}
def inner_inner_functional_calc(arg2: int):
return ops[operation](arg1, arg2)
return inner_inner_functional_calc
return inner_functional_calc
There is also other idea (without imports):
def calculator(arg1: int):
def inner_calc(operation: str):
def add(arg2: int):
return arg1 + arg2
def sub(arg2: int):
return arg1 - arg2
if operation == '+':
return add
elif operation == '-':
return sub
return inner_calc
in this example print(calculator(1)('+')(7))
results in 8
. The problem is to make function which can take any number of arguments and return result when the last argument is '='. Example: print(calculator(2)('+')('5')('-')(3)('-')(1)('='))
results in 3
. Any hints? (if it is possible i would prefer not to use any imports and global variables!)