Last active
February 3, 2023 09:00
-
-
Save jamigibbs/adafd95cf738fa872bda to your computer and use it in GitHub Desktop.
Calculating With Functions. eg. seven(times(five()));
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makeNum(num, func) { | |
if (func === undefined) { | |
return num; | |
} else { | |
return func(num); | |
} | |
} | |
function zero(func) { | |
return makeNum(0,func); | |
} | |
function one(func) { | |
return makeNum(1,func); | |
} | |
function two(func) { | |
return makeNum(2,func); | |
} | |
function three(func) { | |
return makeNum(3,func); | |
} | |
function four(func) { | |
return makeNum(4,func); | |
} | |
function five(func) { | |
return makeNum(5,func); | |
} | |
function six(func) { | |
return makeNum(6,func); | |
} | |
function seven(func) { | |
return makeNum(7,func); | |
} | |
function eight(func) { | |
return makeNum(8,func); | |
} | |
function nine(func) { | |
return makeNum(9,func); | |
} | |
function plus(right) { | |
return function(left) { return left + right; }; | |
} | |
function minus(right) { | |
return function(left) { | |
return left - right; | |
}; | |
} | |
function times(right) { | |
return function(left) { return left * right; }; | |
} | |
function dividedBy(right) { | |
return function(left) { return left / right; }; | |
} | |
eight(minus(three())); // return 5 | |
six(dividedBy(two())); // return 3 | |
two(plus(five())); // return 7 | |
five(plus(two())); // return 7 | |
seven(times(five())); // return 35 | |
console.log(four(plus(nine()))); // must return 13 |
I don't get it. Can you explain some details about how this works? Thank you!
@timmcwilliams They are higher-order functions which means they operate using other functions either by taking them as arguments or by returning them.
The "left" and "right" attributes indicate whether the mathematical operation (plus()
, minus()
, divideBy()
) is using the number function (one()
, two()
, three()
...) that initially started the calculation (left), or, using the number function (one()
, two()
, three()
...) that was passed in as an argument (right).
The solution is based on this kata: https://www.codewars.com/kata/525f3eda17c7cd9f9e000b39
Hi Jami and thanks - a bit over my ability right now. So when the
assertTest is passed in in the format seven(times(five())) how does the
attribute left know it is seven and the attribute "right" pick up the
five?
You start by using function makeNum(num, func) { return func(num);}
not sure where num value or func value are from.
I suppose you wouldn't want to comment on each line for edification?
Don't blame you if you didn't - but I am studying the higher order
functions with some difficulty.
Appreciate the reply though and am impressed with the simplicity of the
solution. I have been looking at that kata for a couple of months - have
looked at some videos on the call function topic as well as the higher
orders with no breakthrough.
Tim
On Tue, Mar 1, 2022 at 7:54 AM Jami Gibbs ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
@timmcwilliams <https://github.com/timmcwilliams> They are higher-order
functions
<https://eloquentjavascript.net/05_higher_order.html#h_xxCc98lOBK> which
means they operate using other functions either by taking them as arguments
or by returning them.
The "left" and "right" attributes indicate whether the mathematical
operation (plus(), minus(), divideBy()) is using the number function (
one(), two(), three()...) that initially started the calculation (left),
or, using the number function (one(), two(), three()...) that was passed
in as an argument (right).
The solution is based on this kata:
https://www.codewars.com/kata/525f3eda17c7cd9f9e000b39
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/adafd95cf738fa872bda#gistcomment-4081500>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AG6IDDLES5NVULXYH77S5VDU5YOPXANCNFSM47TF7FPQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you were mentioned.Message ID:
***@***.***>
<http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
Virus-free.
www.avg.com
<http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
Hi Jami, I need some clarity on this code can i get?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!