0

This code suppose to find smallest left point. I am not sure how to understand how the lambda function works. Please help!

Yunfei Li
  • 19
  • 2
  • For a basic class, there's no sense of "order" defined on the class. However, there is a sense of order in tuples: (1, 0) is less than (1, 3) which is less than (2, 0). This is called lexicographic ordering (order by the 1st element, then by the 2nd, and so on, like a dictionary). So what the key function does in this case is tell `min` what values from your object to use---the x and y coordinates, and stuffs them into a tuple so that `min()` will take the smallest element by lexicographic order. The lambda is just a version of `def f(p): return (p.x, p.y)` but defined in-line. – alkasm Jan 27 '19 at 07:24
  • 1
    So it finds the point with the smallest `.x`, and if there are multiple points with that `.x`, it "breaks the tie" by returning one with the smallest `.y`. – PM 2Ring Jan 27 '19 at 08:07

1 Answers1

0

Lambda functions are anonymous function, they work exactly in same way a function works.

for example:

def add_two(a, b):
    return(a+b)

print(add_two(2,3))

output

5

Now we can write the same thing in lambda way

lambda(a,b): a+b
Atendra Gautam
  • 465
  • 3
  • 11