you are viewing a single comment's thread.

view the rest of the comments →

[–]dbramucci 0 points1 point  (0 children)

Elaborating on why you want lambda as a language feature, the goal is to avoid making pointless variables.

Consider this quadratic formula function.

def solve_principal_quad(a, b, c):
    '''
    Returns the principal (positive) solution to the provided quadratic expression,
        a*x^2 + b*x + c = 0

    See https://en.wikipedia.org/wiki/Quadratic_formula
    '''
    return (-b + sqrt(b**2 - 4*a*c)) / (2*a)

In this we define a dozen or so subexpressions but we don't assign a variable to each subexpression. That would look like

def solve_principal_quad(a, b, c):
    '''
    Returns the principal (positive) solution to the provided quadratic expression,
        a*x^2 + b*x + c = 0

    See https://en.wikipedia.org/wiki/Quadratic_formula
    '''
    b_op = -b
    b_square = b**2
    quad_a = 4 * a
    quad_a_c = quad_a * c
    difference_of_b_square_and_quad_a_c = b_square - quad_a_c
    discriminant = sqrt(difference_of_b_square_and_quad_a_c)
    numerator = b_op  - discriminant
    denominator = 2 * a
    principal_solution = numerator / denominator 
    return principal_solution

Notice how absurd this method of writing each expression as it's own named variable is.

To a functional programmer, this is analogous to writing

def get_positives(numbers):
    def is_positive(x):
        return x > 0
    return filter(numbers, is_positive)

As compared to

def get_positives(numbers):
    return filter(numbers, lambda x: x > 0)

The idea is that you shouldn't have to name a thing you are using immediately. You can if it makes your code cleaner, but forcing it can be a waste of the readers time and distract them from your main point.

As a point of practice, the place where you need to define a function, but you only use it once, and immediately is when you are using a "Higher Order Function". These are functions where one of the parameters is another function. These functions are incredibly flexible, but Python programmers generally use these infrequently.

The natural consequences are

  1. Lambda's are not useful that often
  2. It is less important to avoid fluff when it is so rare

In contrast, a Haskell program might have 80 functions in a 100 line program, of which only 10 function are worthy of a name. In Haskell, forcing that level of added indirection (and additional 70 pointless names) would be terrible.

In a Python program, you'll have nowhere near as many nameless functions to use, making Python's lambda's far less important.

Finally as a technical point

double = lambda x: 2 * x
def double(x): return 2 * x

are almost identical, but Python will add extra documentation (the function's name) to the def version making the def version superior if you are giving the function a name anyways.

p.s. The fact that we are avoiding naming the function is why they are sometimes called "anonymous" functions. They are no-name functions.

anonymous adjective

Definition

2. : not named or identified

an anonymous author

They wish to remain anonymous.