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)
5
  • 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? Commented Jan 29, 2017 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? Commented Jan 29, 2017 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. Commented Jan 29, 2017 at 22:48
  • I don't understand, you already have a solution... Commented Jan 29, 2017 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)). Commented Jan 29, 2017 at 23:24

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Well, assigning lambdas to variables is considered an anti-pattern for debugging reasons essentially, and that it defeats the concept of anonymous functions :)
@ChristopherHumbert My pleasure! If an answer here solves your problem, please mark it as accepted if you don't mind :)
0

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

1 Comment

Though lambdas are defined using lambda expressions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.