Converting lambda functions to regular functions - python

I need to convert the following code involving several lambda functions into separate "regular" functions. How would I do this?
I'm working on Pyspark right now. Data source is an RDD
result = rdd.filter(lambda x: x[0]=='9439').map(lambda x: (x[0], json.loads(x[1])['exposures'])).flatMapValues(lambda x: x).map(lambda x: {'serial_no' : x[0], **x[1]})
My attempt:
def acct_tuple(x):
return (x[0], json.loads(x[1])['exposures'])
def flat_map(x):
return x
def get_tuple(x):
return {'serial_no': x[0], **x[1]}
rdd = rdd.map(acct_tuple(x)).flatMapValues(flat_map(x)).map(get_tuple(x))
is there a better way to do this?

You should pass the function itself as a parameter, and do not call it, hence:
def acct_tuple(x):
return (x[0], json.loads(x[1])['exposures'])
def flat_map(x):
return x
def get_tuple(x):
return {'serial_no': x[0], **x[1]}
rdd = rdd.map(acct_tuple).flatMapValues(flat_map).map(get_tuple)

Related

Function composition with just lambdas in python

Is there a way to do the following in just one line with lambdas in python?
squared = lambda x : x**2
cubed = lambda x : x**3
squareCubed = lambda x : squared(cubed(x))
without using any helper functions or reduce.
print(squareCubed(2)) # > 64
Sure, you can do something like:
squareCube = (lambda f, g: lambda x: f(g(x)))(lambda x: x**2, lambda x: x**3)
But why do this?
In any case, you shouldn't be assigning the result of lambda to a variable if you are following the official PEP8 style guide. What is wrong with:
def square(x):
return x**2
def cube(x):
return x**3
def compose(f, g):
return lambda x: f(g(x))
square_cube = compose(square, cube)
Is much more readable.
If you want to hard code it:
squareCube = lambda x: (x**3)**2
But generically:
compose = lambda f, g: lambda x: f(g(x))
squareCube = compose(lambda x: x**2, lambda x: x**3)
Or, in one line:
squareCube = (lambda f, g: lambda x: f(g(x)))(lambda x: x**2, lambda x: x**3)
squareCubed = lambda x : (x**3)**2
print(squareCubed(2)) # gives 64

How to apply multiple methods on the same data element in FastAPI

I want to perform the following processing of a String variable received as a parameter of my API endpoint (FastAPI) as shown below, and I have 30 functions that need to be applied to the String variable.
I'm kind of lost and don't really know how can I do this, any hints, or concepts I should learn about that can help solve the problem.
Code:
def func1(data):
return True
def func2(data):
return True
def func3(data):
return True
...
def func30(data):
return True
#app.get("/process/{data}")
def process(data):
# Apply all the methods on the data variable
return response ```
----------
thank you
You can use map with a list of functions and turn it into a list, as map is lazily evaluated. You may ignore the list if not needed.
def apply(data, functions):
return list(map(lambda f: f(data), functions))
from functools import reduce
from typing import Callable, Iterable, Any
def apply(funcs, data):
return reduce(lambda tmp, func: func(tmp), funcs, data)
funcs = [
lambda x: x + 'a',
lambda x: x + 'b',
lambda x: x + 'c',
lambda x: x + 'd',
]
print(apply(funcs, '')) # Result is 'abcd'

Why is this different? Currying/Higher Order Functions

I'm wondering why these 2 pieces of code are different?
This code gives me the answer of 5
curry2 = lambda f: lambda x: lambda y: f(x, y)
m = curry2(add)
m(2)(3)
5
This one gives me the location of my function
def lambda_curry2(func):
return lambda f: lambda x: lambda y: f(x, y)
curry2 = lambda_curry2(add)
add_three = curry2(3)
add_three(5)
The second one isn't using func. You don't need as many lambdas, because func is the function you want to call.
So it should be;
def lambda_curry2(func):
return lambda x: lambda y: func(x, y)
Put another way, your definition of curry2 is equivalent to:
def curry2(f):
return lambda x: lambda y: f(x, y)
In general,
name = lambda <vars>: <expression>
is short for
def name(<vars>):
return <expression>
lambda is usually used when you don't need to name the function (e.g. when you want to pass a simple function as an argument or return it as a value).

Python Lambda function variable ordering

If I state a lambda function like this:
someLambdaFunction = lambda x,y: x+x**2+y
can I reverse the ordering of that lambda to say
someOtherLambdaFunction = lambda y,x: x+x**2+y
without rewriting the whole lambda function? Just switch the arguments?
Yes, you can do something like this:
someLambdaFunction = lambda x,y: x+x**2+y
someOtherLambdaFunction = lambda y, x: someLambdaFunction(x, y)
Basically, reuse the original someLambdaFunction
You can write a general function that swaps the first two arguments of another function:
def swap2(func):
""" Swap the first two arguments of a function """
def wrapped(x, y, *args, **kwargs):
return func(y, x, *args, **kwargs)
return wrapped
f = lambda x,y: x+x**2+y
f_swapped= swap2(f)
assert f(3, 4) == f_swapped(4, 3)
This works because functions are just regular Python objects, that can be passed around just like integers, strings, etc.
By the way, there is almost never a reason to write something = lambda .... Use def something(...) instead.

Map Function for Numpy Matrix

I have two numpy matrices. One contains lambda functions. The other contains values.
Is there a function that is similar to Python's map function that will allow me to get the expected result?
Is there a better way?
functionMatrix = np.array([[lambda x:x**2, lambda x:x**3],[lambda x: x**2,
lambda x: np.sqrt(x)]])
valueMatrix = np.array([[1,2],[3,4]])
expectedResult = np.array([[1,8],[9,2]])
This is just syntactic sugar but does the job.
#np.vectorize
def apply_vec(f, x):
return f(x)
result = apply_vec(functionMatrix, valueMatrix)

Categories

Resources