0

In this sample program I'm naming the days 0 through 6 where day 0 is Sunday and day 6 is Saturday. I'm wondering if I can use a lambda expression here for starting day and length of stay and still return the number of the day of the week I would return on. I'm having trouble understanding how to use lambda expressions in a simple example like this.

starting_day = 3
length_of_stay = 10
end_day = (starting_day + length_of_stay) % 7
print("You will come back on day: ", end_day)
  • 3
    A lambda expression is used to define anonymous **functions**. Where in this piece of code do you define a function you want to "anonymize" so to speak? – Willem Van Onsem Jan 29 '17 at 22:47
  • This seems backwards. You have a solution (lambda) and now you're trying to find a problem where you can use it? – melpomene Jan 29 '17 at 22:47
  • @melpomene For learning purposes, that definitely seems apt. One must first to learn the uses of a tool before actually using it. – Code-Apprentice Jan 29 '17 at 22:48
  • I don't understand, you already have a solution... – m_callens Jan 29 '17 at 22:49
  • The challenge with this question is that there is no good reason to define a lambda here. lamdbas are functions and they need to be called to be useful. They are typically used when you want to pass a function to some other function that wants to use a callback. You could use an expression for your print, `print("You will come back on day: ", (starting_day + length_of_stay) % 7)`, but it would be silly to use a lambda `print("You will come back on day: ", (lambda start, length: (start + length) % 7)(starting_day, length_of_stay))`. – tdelaney Jan 29 '17 at 23:24

2 Answers2

4

Before thinking about how you'd write a lambda function for this, consider how you'd write a normal function. It would probably look something like this:

def day_of_return(starting_day, length_of_stay):
    return (starting_day + length_of_stay) % 7

Then you can easily convert this to a lambda function:

day_of_return = lambda starting_day, length_of_stay: (starting_day + length_of_stay) % 7

or, with shorter variable names:

day_of_return = lambda s,l: (s+l) % 7

And then you could call it like this:

print(day_of_return(3,10))

Edit: In the comments, @daragua points out that assigning lambdas to a variable kind of defeats the purpose of a lambda, so you can also remove the assignment altogether:

print((lambda s,l: (s+l) % 7) (3,10)) # This will print 6
Christopher Shroba
  • 7,006
  • 8
  • 40
  • 68
  • Well, assigning lambdas to variables [is considered an anti-pattern](http://stackoverflow.com/a/25010243/) for debugging reasons essentially, and that it defeats the concept of `anonymous` functions :) – daragua Jan 29 '17 at 22:56
  • @ChristopherHumbert My pleasure! If an answer here solves your problem, please mark it as accepted if you don't mind :) – Christopher Shroba Jan 29 '17 at 23:00
0

Lambdas are functions, not expressions. They can be used anywhere a function is used. They are typically anonymous and very short.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Though lambdas are defined using [lambda expressions](https://docs.python.org/3.6/reference/expressions.html#lambda). – daragua Jan 29 '17 at 23:00