-2

Let's say I have a HOF

def some_func(lst):
    def func(args):
        if args[0]=='compute':
            return sum(lst)
        elif args[0]=='add':
            XXXXX  #Return a new HOF with the 2 sub HOF input parameters added together.  
    return func
x1=some_func([1,2,3])
x2=some_func([2,3,4])

One of the input *args for HOF is ('add', another_hof), which require the HOF to add another HOF parameter and return a HOF with added parameter.

example:

x3=x1('add',x2)
x4=some_func([3,5,7])

Then,

x3 should equal x4.
test_case: 
x1=some_func([1,2,3])
x2=some_func([2,3,4])
x1('compute')=6
x2('compute')=9
x3=x1('add',x2)
x3('compute')=15

When I am doingx HOF the ('add,x2) for x1 function, is possible me to know the x2' input parameter [2,3,4] inside the function func(*args)?

Marcus.Aurelianus
  • 1,520
  • 10
  • 22
  • ... Wouldn't you be able to access it within `func` by using the name `lst`? Doesn't seem any different than accessing any other variable. – Kevin May 31 '18 at 11:08
  • Did you try `print(lst)` inside `func`? I guess not. This is not different than any other nested function or decorator – DeepSpace May 31 '18 at 11:10
  • Forgive my poor english. Trying to ask is it possible for me to know HOF x2 parameter when I am doing 'add' for x1 – Marcus.Aurelianus May 31 '18 at 11:14
  • You can access `lst` inside `func`, as long as you don't attempt to assign to the name `lst` in there, since that will tell Python to treat that name as a local of `func`. If `lst` is an actual `list` then it's safe to _mutate_ it, though. See https://stackoverflow.com/questions/30157655/python-load-fast-vs-load-deref-with-inplace-addition for details. – PM 2Ring May 31 '18 at 11:14
  • 1
    If we're not understanding your actual question then you need to add some more code to your question that more closely illustrates what you want to do. All this HOF talk is a bit confusing: it's a bit hard to keep track of which HOF is which. ;) – PM 2Ring May 31 '18 at 11:18
  • Please edit your question so someone else than you have at least a chance to understand what you're trying to do. Also, remember that you can define your own callable objects (python functions are objects...) which provide the same overall features as HOFs/closures but make state management much easier. Quite a few decorators (one of Python's most well-known use of HOFs) are actually implemented as callable classes instead of functions. – bruno desthuilliers May 31 '18 at 11:29
  • Edited...hope this time it looks better. – Marcus.Aurelianus May 31 '18 at 11:38

1 Answers1

2

As I see it, the core of the problem is: given a reference to a func instance, you need to get the lst value that it contains.

One way to do this is to add another mode to your conditional block that returns lst. Let's call it get_lst:

def some_func(lst):
    def func(*args):
        if args[0]=='compute':
            return sum(lst)
        elif args[0]=='add':
            new_lst = [a+b for a,b in zip(lst, args[1]("get_lst"))]
            return some_func(new_lst)
        elif args[0] == "get_lst":
            return lst
    return func

x1=some_func([1,2,3])
x2=some_func([2,3,4])
print(x1('compute'))
print(x2('compute'))
x3=x1('add',x2)
print(x3('compute'))

Result:

6
9
15

You could also assign lst to an attribute of the function object:

def some_func(lst):
    def func(*args):
        if args[0]=='compute':
            return sum(lst)
        elif args[0]=='add':
            new_lst = [a+b for a,b in zip(lst, args[1].params)]
            return some_func(new_lst)
    func.params = lst
    return func

x1=some_func([1,2,3])
x2=some_func([2,3,4])
print(x1('compute'))
print(x2('compute'))
x3=x1('add',x2)
print(x3('compute'))
Kevin
  • 74,910
  • 12
  • 133
  • 166