Python Multiple Function CompositionCalling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory in Python?How to get the current time in PythonUsing global variables in a functionHow can I make a time delay in Python?How to make a chain of function decorators?Does Python have a string 'contains' substring method?

Which US defense organization would respond to an invasion like this?

Why are oscilloscope input impedances so low?

My first C++ game (snake console game)

Motion-trail-like lines

Why did WWI include Japan?

How to deal with employer who keeps me at work after working hours

Counting the Number of Real Roots of A Polynomial

Dangerous workplace travelling

Drawing an hexagonal cone in TikZ 2D

Should I simplify my writing in a foreign country?

How to pass query parameters in URL in Salesforce Summer 19 Release?

Sheared off exhasut pipe: How to fix without a welder?

What do you call a painting on a wall?

Is Iron Man stronger than the Hulk?

Is any special diet an effective treatment of autism?

Would a "Permanence" spell in 5e be overpowered?

How to remap repeating commands i.e. <number><command>?

Where are the "shires" in the UK?

It isn’t that you must stop now

Should homeowners insurance cover the cost of the home?

Dirichlet series with a single zero

Gerrymandering Puzzle - Rig the Election

weird pluperfect subjunctive in Eutropius

Madam I m Adam..please don’t get mad..you will no longer be prime



Python Multiple Function Composition


Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?How can I safely create a nested directory in Python?How to get the current time in PythonUsing global variables in a functionHow can I make a time delay in Python?How to make a chain of function decorators?Does Python have a string 'contains' substring method?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















So i have a homework question, but I'm not sure why I got it wrong / how it works.



once = lambda f: lambda x: f(x)
twice = lambda f: lambda x: f(f(x))
thrice = lambda f: lambda x: f(f(f(x)))
print(thrice(twice)(once)(lambda x: x + 2)(9))


My ans: 25 -> 8*2 +9



Actual ans: 11 -> 2 + 9



What I was thinking:



thrice -> f(f(f(x))),
let new_x = twice(x)



thrice -> f(f(new_x)),
let new_x2 = twice(new_x)



thrice -> f(new_x2),
let new_thrice = twice(new_x2)



so afterwards I add in the (once) and did
new_thrice(once)(lambda x: x+2)(9)



But answer seems to be that (once) nullifies the earlier thrice(twice) and am lost about. Would be great if someone has an explanation.. Thanks!










share|improve this question






















  • In general, it is bad form to assign lambdas to variables - prefer defining named functions with def instead.

    – gmds
    Mar 23 at 4:01











  • The code provided outputs 11. Where are you getting 25 from?

    – alec_a
    Mar 23 at 4:23











  • I was supposed to write it on paper lol

    – LostChild
    Mar 23 at 7:48

















0















So i have a homework question, but I'm not sure why I got it wrong / how it works.



once = lambda f: lambda x: f(x)
twice = lambda f: lambda x: f(f(x))
thrice = lambda f: lambda x: f(f(f(x)))
print(thrice(twice)(once)(lambda x: x + 2)(9))


My ans: 25 -> 8*2 +9



Actual ans: 11 -> 2 + 9



What I was thinking:



thrice -> f(f(f(x))),
let new_x = twice(x)



thrice -> f(f(new_x)),
let new_x2 = twice(new_x)



thrice -> f(new_x2),
let new_thrice = twice(new_x2)



so afterwards I add in the (once) and did
new_thrice(once)(lambda x: x+2)(9)



But answer seems to be that (once) nullifies the earlier thrice(twice) and am lost about. Would be great if someone has an explanation.. Thanks!










share|improve this question






















  • In general, it is bad form to assign lambdas to variables - prefer defining named functions with def instead.

    – gmds
    Mar 23 at 4:01











  • The code provided outputs 11. Where are you getting 25 from?

    – alec_a
    Mar 23 at 4:23











  • I was supposed to write it on paper lol

    – LostChild
    Mar 23 at 7:48













0












0








0








So i have a homework question, but I'm not sure why I got it wrong / how it works.



once = lambda f: lambda x: f(x)
twice = lambda f: lambda x: f(f(x))
thrice = lambda f: lambda x: f(f(f(x)))
print(thrice(twice)(once)(lambda x: x + 2)(9))


My ans: 25 -> 8*2 +9



Actual ans: 11 -> 2 + 9



What I was thinking:



thrice -> f(f(f(x))),
let new_x = twice(x)



thrice -> f(f(new_x)),
let new_x2 = twice(new_x)



thrice -> f(new_x2),
let new_thrice = twice(new_x2)



so afterwards I add in the (once) and did
new_thrice(once)(lambda x: x+2)(9)



But answer seems to be that (once) nullifies the earlier thrice(twice) and am lost about. Would be great if someone has an explanation.. Thanks!










share|improve this question














So i have a homework question, but I'm not sure why I got it wrong / how it works.



once = lambda f: lambda x: f(x)
twice = lambda f: lambda x: f(f(x))
thrice = lambda f: lambda x: f(f(f(x)))
print(thrice(twice)(once)(lambda x: x + 2)(9))


My ans: 25 -> 8*2 +9



Actual ans: 11 -> 2 + 9



What I was thinking:



thrice -> f(f(f(x))),
let new_x = twice(x)



thrice -> f(f(new_x)),
let new_x2 = twice(new_x)



thrice -> f(new_x2),
let new_thrice = twice(new_x2)



so afterwards I add in the (once) and did
new_thrice(once)(lambda x: x+2)(9)



But answer seems to be that (once) nullifies the earlier thrice(twice) and am lost about. Would be great if someone has an explanation.. Thanks!







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 23 at 3:29









LostChildLostChild

133




133












  • In general, it is bad form to assign lambdas to variables - prefer defining named functions with def instead.

    – gmds
    Mar 23 at 4:01











  • The code provided outputs 11. Where are you getting 25 from?

    – alec_a
    Mar 23 at 4:23











  • I was supposed to write it on paper lol

    – LostChild
    Mar 23 at 7:48

















  • In general, it is bad form to assign lambdas to variables - prefer defining named functions with def instead.

    – gmds
    Mar 23 at 4:01











  • The code provided outputs 11. Where are you getting 25 from?

    – alec_a
    Mar 23 at 4:23











  • I was supposed to write it on paper lol

    – LostChild
    Mar 23 at 7:48
















In general, it is bad form to assign lambdas to variables - prefer defining named functions with def instead.

– gmds
Mar 23 at 4:01





In general, it is bad form to assign lambdas to variables - prefer defining named functions with def instead.

– gmds
Mar 23 at 4:01













The code provided outputs 11. Where are you getting 25 from?

– alec_a
Mar 23 at 4:23





The code provided outputs 11. Where are you getting 25 from?

– alec_a
Mar 23 at 4:23













I was supposed to write it on paper lol

– LostChild
Mar 23 at 7:48





I was supposed to write it on paper lol

– LostChild
Mar 23 at 7:48












2 Answers
2






active

oldest

votes


















-1














once(lambda x: x+2) evaluates to a function that applies lambda x: x+2 to its argument. In other words, it's equivalent to lambda x: x+2.



once(once(lambda x: x+2)) evaluates to a function that applies once(lambda x: x+2) to its argument. In other words, it's also equivalent to lambda x: x+2.



once(once(once(lambda x: x+2))) evaluates to a function that applies once(once(lambda x: x+2)) to its argument. In other words, this is also equivalent to lambda x: x+2. This doesn't change no matter how many times you apply once.



thrice(twice)(once) evaluates to a function that applies once to its argument some number of times. (8 times, not that it matters for the analysis.) once doesn't change a function's behavior. No matter how many times you apply once, the final function only applies the underlying function once.



thrice(twice)(once)(lambda x: x + 2) thus evaluates to a function that does the same thing as lambda x: x + 2.



Now, if it had been thrice(twice)(once(lambda x: x + 2)) (note the moved parentheses), then that would have applied thrice(twice) to once(lambda x: x + 2), and the result would be a function that applies lambda x: x + 2 8 times.






share|improve this answer























  • oh I see! It seems I had a misconception of starting from thrice(twice) first

    – LostChild
    Mar 23 at 7:55


















0














I hope this will help you to figure out what is going on!



once = lambda f: lambda x: f(x)
twice = lambda f: lambda x: f(f(x))
thrice = lambda f: lambda x: f(f(f(x)))

# Created this one to help readability.
custom_func = lambda x: x + 2

print("once:", once(custom_func)(9)) # returns value
print("twice:", twice(custom_func)(9)) # returns value
print("thrice:", thrice(custom_func)(9)) # returns value

print("applying all those:", thrice(custom_func)(twice(custom_func)(once(custom_func)(9))))
# This represents the following: (((9 + 2) + 2 + 2) + 2 + 2 + 2)
# each pair of parenthesis mean one function being applied, first once, then twice, then thrice.

# If I've understood correctly you need to achieve 25
# to achieve 25 we need to apply +4 in this result so, which means +2 +2, twice function...
print("Achieving 25:", twice(custom_func)(thrice(custom_func)(twice(custom_func)(once(custom_func)(9)))))

# That is it! Hope it helps.





share|improve this answer

























  • thanks for the help! But i wanted to understand why it's 11, not really to get 25 haha

    – LostChild
    Mar 23 at 7:56











  • Hmm sorry, I didn't get you wanted 11, because it was already giving 11 haha but alright!

    – João Luiz
    Mar 23 at 12:31











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55310356%2fpython-multiple-function-composition%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














once(lambda x: x+2) evaluates to a function that applies lambda x: x+2 to its argument. In other words, it's equivalent to lambda x: x+2.



once(once(lambda x: x+2)) evaluates to a function that applies once(lambda x: x+2) to its argument. In other words, it's also equivalent to lambda x: x+2.



once(once(once(lambda x: x+2))) evaluates to a function that applies once(once(lambda x: x+2)) to its argument. In other words, this is also equivalent to lambda x: x+2. This doesn't change no matter how many times you apply once.



thrice(twice)(once) evaluates to a function that applies once to its argument some number of times. (8 times, not that it matters for the analysis.) once doesn't change a function's behavior. No matter how many times you apply once, the final function only applies the underlying function once.



thrice(twice)(once)(lambda x: x + 2) thus evaluates to a function that does the same thing as lambda x: x + 2.



Now, if it had been thrice(twice)(once(lambda x: x + 2)) (note the moved parentheses), then that would have applied thrice(twice) to once(lambda x: x + 2), and the result would be a function that applies lambda x: x + 2 8 times.






share|improve this answer























  • oh I see! It seems I had a misconception of starting from thrice(twice) first

    – LostChild
    Mar 23 at 7:55















-1














once(lambda x: x+2) evaluates to a function that applies lambda x: x+2 to its argument. In other words, it's equivalent to lambda x: x+2.



once(once(lambda x: x+2)) evaluates to a function that applies once(lambda x: x+2) to its argument. In other words, it's also equivalent to lambda x: x+2.



once(once(once(lambda x: x+2))) evaluates to a function that applies once(once(lambda x: x+2)) to its argument. In other words, this is also equivalent to lambda x: x+2. This doesn't change no matter how many times you apply once.



thrice(twice)(once) evaluates to a function that applies once to its argument some number of times. (8 times, not that it matters for the analysis.) once doesn't change a function's behavior. No matter how many times you apply once, the final function only applies the underlying function once.



thrice(twice)(once)(lambda x: x + 2) thus evaluates to a function that does the same thing as lambda x: x + 2.



Now, if it had been thrice(twice)(once(lambda x: x + 2)) (note the moved parentheses), then that would have applied thrice(twice) to once(lambda x: x + 2), and the result would be a function that applies lambda x: x + 2 8 times.






share|improve this answer























  • oh I see! It seems I had a misconception of starting from thrice(twice) first

    – LostChild
    Mar 23 at 7:55













-1












-1








-1







once(lambda x: x+2) evaluates to a function that applies lambda x: x+2 to its argument. In other words, it's equivalent to lambda x: x+2.



once(once(lambda x: x+2)) evaluates to a function that applies once(lambda x: x+2) to its argument. In other words, it's also equivalent to lambda x: x+2.



once(once(once(lambda x: x+2))) evaluates to a function that applies once(once(lambda x: x+2)) to its argument. In other words, this is also equivalent to lambda x: x+2. This doesn't change no matter how many times you apply once.



thrice(twice)(once) evaluates to a function that applies once to its argument some number of times. (8 times, not that it matters for the analysis.) once doesn't change a function's behavior. No matter how many times you apply once, the final function only applies the underlying function once.



thrice(twice)(once)(lambda x: x + 2) thus evaluates to a function that does the same thing as lambda x: x + 2.



Now, if it had been thrice(twice)(once(lambda x: x + 2)) (note the moved parentheses), then that would have applied thrice(twice) to once(lambda x: x + 2), and the result would be a function that applies lambda x: x + 2 8 times.






share|improve this answer













once(lambda x: x+2) evaluates to a function that applies lambda x: x+2 to its argument. In other words, it's equivalent to lambda x: x+2.



once(once(lambda x: x+2)) evaluates to a function that applies once(lambda x: x+2) to its argument. In other words, it's also equivalent to lambda x: x+2.



once(once(once(lambda x: x+2))) evaluates to a function that applies once(once(lambda x: x+2)) to its argument. In other words, this is also equivalent to lambda x: x+2. This doesn't change no matter how many times you apply once.



thrice(twice)(once) evaluates to a function that applies once to its argument some number of times. (8 times, not that it matters for the analysis.) once doesn't change a function's behavior. No matter how many times you apply once, the final function only applies the underlying function once.



thrice(twice)(once)(lambda x: x + 2) thus evaluates to a function that does the same thing as lambda x: x + 2.



Now, if it had been thrice(twice)(once(lambda x: x + 2)) (note the moved parentheses), then that would have applied thrice(twice) to once(lambda x: x + 2), and the result would be a function that applies lambda x: x + 2 8 times.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 23 at 6:44









user2357112user2357112

160k13179274




160k13179274












  • oh I see! It seems I had a misconception of starting from thrice(twice) first

    – LostChild
    Mar 23 at 7:55

















  • oh I see! It seems I had a misconception of starting from thrice(twice) first

    – LostChild
    Mar 23 at 7:55
















oh I see! It seems I had a misconception of starting from thrice(twice) first

– LostChild
Mar 23 at 7:55





oh I see! It seems I had a misconception of starting from thrice(twice) first

– LostChild
Mar 23 at 7:55













0














I hope this will help you to figure out what is going on!



once = lambda f: lambda x: f(x)
twice = lambda f: lambda x: f(f(x))
thrice = lambda f: lambda x: f(f(f(x)))

# Created this one to help readability.
custom_func = lambda x: x + 2

print("once:", once(custom_func)(9)) # returns value
print("twice:", twice(custom_func)(9)) # returns value
print("thrice:", thrice(custom_func)(9)) # returns value

print("applying all those:", thrice(custom_func)(twice(custom_func)(once(custom_func)(9))))
# This represents the following: (((9 + 2) + 2 + 2) + 2 + 2 + 2)
# each pair of parenthesis mean one function being applied, first once, then twice, then thrice.

# If I've understood correctly you need to achieve 25
# to achieve 25 we need to apply +4 in this result so, which means +2 +2, twice function...
print("Achieving 25:", twice(custom_func)(thrice(custom_func)(twice(custom_func)(once(custom_func)(9)))))

# That is it! Hope it helps.





share|improve this answer

























  • thanks for the help! But i wanted to understand why it's 11, not really to get 25 haha

    – LostChild
    Mar 23 at 7:56











  • Hmm sorry, I didn't get you wanted 11, because it was already giving 11 haha but alright!

    – João Luiz
    Mar 23 at 12:31















0














I hope this will help you to figure out what is going on!



once = lambda f: lambda x: f(x)
twice = lambda f: lambda x: f(f(x))
thrice = lambda f: lambda x: f(f(f(x)))

# Created this one to help readability.
custom_func = lambda x: x + 2

print("once:", once(custom_func)(9)) # returns value
print("twice:", twice(custom_func)(9)) # returns value
print("thrice:", thrice(custom_func)(9)) # returns value

print("applying all those:", thrice(custom_func)(twice(custom_func)(once(custom_func)(9))))
# This represents the following: (((9 + 2) + 2 + 2) + 2 + 2 + 2)
# each pair of parenthesis mean one function being applied, first once, then twice, then thrice.

# If I've understood correctly you need to achieve 25
# to achieve 25 we need to apply +4 in this result so, which means +2 +2, twice function...
print("Achieving 25:", twice(custom_func)(thrice(custom_func)(twice(custom_func)(once(custom_func)(9)))))

# That is it! Hope it helps.





share|improve this answer

























  • thanks for the help! But i wanted to understand why it's 11, not really to get 25 haha

    – LostChild
    Mar 23 at 7:56











  • Hmm sorry, I didn't get you wanted 11, because it was already giving 11 haha but alright!

    – João Luiz
    Mar 23 at 12:31













0












0








0







I hope this will help you to figure out what is going on!



once = lambda f: lambda x: f(x)
twice = lambda f: lambda x: f(f(x))
thrice = lambda f: lambda x: f(f(f(x)))

# Created this one to help readability.
custom_func = lambda x: x + 2

print("once:", once(custom_func)(9)) # returns value
print("twice:", twice(custom_func)(9)) # returns value
print("thrice:", thrice(custom_func)(9)) # returns value

print("applying all those:", thrice(custom_func)(twice(custom_func)(once(custom_func)(9))))
# This represents the following: (((9 + 2) + 2 + 2) + 2 + 2 + 2)
# each pair of parenthesis mean one function being applied, first once, then twice, then thrice.

# If I've understood correctly you need to achieve 25
# to achieve 25 we need to apply +4 in this result so, which means +2 +2, twice function...
print("Achieving 25:", twice(custom_func)(thrice(custom_func)(twice(custom_func)(once(custom_func)(9)))))

# That is it! Hope it helps.





share|improve this answer















I hope this will help you to figure out what is going on!



once = lambda f: lambda x: f(x)
twice = lambda f: lambda x: f(f(x))
thrice = lambda f: lambda x: f(f(f(x)))

# Created this one to help readability.
custom_func = lambda x: x + 2

print("once:", once(custom_func)(9)) # returns value
print("twice:", twice(custom_func)(9)) # returns value
print("thrice:", thrice(custom_func)(9)) # returns value

print("applying all those:", thrice(custom_func)(twice(custom_func)(once(custom_func)(9))))
# This represents the following: (((9 + 2) + 2 + 2) + 2 + 2 + 2)
# each pair of parenthesis mean one function being applied, first once, then twice, then thrice.

# If I've understood correctly you need to achieve 25
# to achieve 25 we need to apply +4 in this result so, which means +2 +2, twice function...
print("Achieving 25:", twice(custom_func)(thrice(custom_func)(twice(custom_func)(once(custom_func)(9)))))

# That is it! Hope it helps.






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 23 at 6:36









Matt C

2,39541836




2,39541836










answered Mar 23 at 4:21









João LuizJoão Luiz

264




264












  • thanks for the help! But i wanted to understand why it's 11, not really to get 25 haha

    – LostChild
    Mar 23 at 7:56











  • Hmm sorry, I didn't get you wanted 11, because it was already giving 11 haha but alright!

    – João Luiz
    Mar 23 at 12:31

















  • thanks for the help! But i wanted to understand why it's 11, not really to get 25 haha

    – LostChild
    Mar 23 at 7:56











  • Hmm sorry, I didn't get you wanted 11, because it was already giving 11 haha but alright!

    – João Luiz
    Mar 23 at 12:31
















thanks for the help! But i wanted to understand why it's 11, not really to get 25 haha

– LostChild
Mar 23 at 7:56





thanks for the help! But i wanted to understand why it's 11, not really to get 25 haha

– LostChild
Mar 23 at 7:56













Hmm sorry, I didn't get you wanted 11, because it was already giving 11 haha but alright!

– João Luiz
Mar 23 at 12:31





Hmm sorry, I didn't get you wanted 11, because it was already giving 11 haha but alright!

– João Luiz
Mar 23 at 12:31

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55310356%2fpython-multiple-function-composition%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript