Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting f(x) into f([x]) using decorator in python

class foo(object):
        def __init__(self,f):
                self.f = f
        def __call__(self,args_list):
                def wrapped_f(args_list):
                        return [self.f(*args) for args in args_list]
                return wrapped_f(args_list)

if __name__=='__main__':

        class abc(object):
                @foo
                def f(a,b,c):
                        return a+b+c

        a = range(5)
        b = range(5)
        c = range(5)
        data = list(zip(a,b,c))
        print(abc.f(data))

I wrote this a few years back. When you decorate any function f(X) with @foo it becomes f(list of Xs).

What is this process called? What is it? What is its functional programming name?

Its not currying. I know simple map9(f,list of Xs) could have done it.

What are decorators/operation of decorating called mathematically?

like image 816
Pratik Deoghare Avatar asked Apr 13 '26 16:04

Pratik Deoghare


2 Answers

There are two transformations performed on your original function:

  1. it is converted from a function of three arguments to a function that takes a 3-tuple
  2. conversion from a function of a 3-tuple to a function that takes a list of 3-tuples

First transformation
In Haskell, there is a function called uncurry, documented here. (This is a two-argument version; 3-, 4-, ... versions could be easily created, too).

Second transformation
Also in Haskell, there are sets of functions with lift in their names. Here's a page on the Haskell wiki about lifting. I think that page explains it better than I could:

Lifting is a concept which allows you to transform a function into a   
corresponding function within another (usually more general) setting.

So in your case, you're lifting a function from operating on tuples to operating on a list of tuples.


Notes:

  • the OP asked for the mathematical name for decorators. I don't know what that would be, but I've heard that Haskell is supposed to be like executable mathematics, so I think Haskell's terminology is a good starting point. YMMV.
  • the OP asked for the FP name of these processes. Again, I don't know, but I assume that Haskell's terminology is acceptable.
like image 160
Matt Fenwick Avatar answered Apr 16 '26 07:04

Matt Fenwick


Decorators just have special syntax, but there are no rules what decorators can return and no mathematical description. They can be any callable after all.

Your function is just a partially applied starmap:

from functools import partial
from itertools import starmap

def foo(f):
    return partial(starmap, f)
like image 43
Jochen Ritzel Avatar answered Apr 16 '26 05:04

Jochen Ritzel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!