6 kyu
Function Composition
2,520 of 13,859user578387
Loading description...
Functional Programming
Fundamentals
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
Scala translation
too easy as a 6 kyu
This comment has been hidden.
this is something i think i understand then i don't then i understand and then again i don't, can anyone help? where to learn it better? or what about its use????
In the Output the second Sample Tests message is: It says "Should work with a single argument" although in that case multiple arguments are tested. I think that message should be "Should work with multiple arguments" instead.
JS: Node v12 should be used along with its appropiate assertion tools (Mocha + Chai). Refer to this and this
Updated to Node v12
JS: No random tests
For Js need more example of multiple arguments
Great kata, but an example of what 'multiple arguments' would make this kata eve better, as the provided link (to mathIsFun.com) does not cover what is meant by multiple arguments in function composition.
Happy learning and happy coding!
Ty, helps a lot. Easy.
I didn't quite get the point of having many arguments, because we end up using only the first one from the "args" array. Or have I got something wrong?
Many languages don't have random tests.
! "Test results: ... FunctionS may be called with many arguments. ... " I think it should be "FunctioN".
This comment has been hidden.
The inner function need not be of arity 1.
I see you figured out how to do it. Closing.
Yes, I finally did it! Thank you. Voted very satisfied. :-)
This comment has been hidden.
The description isn't detailed. Test cases only gave 1 argument but 5 were given when you tried to attempt this. but it isn't an problem. you just lose sight of variable parameter. like "*args" and "**kw" in python
Not a suggestion
This comment has been hidden.
Yes, read about the splat operator / spread syntax
This comment has been hidden.
Not an issue
Functions may be called with many arguments? Does that look like this: compose(add, subtract)(0)(1), or this: compose(add, subtract)(0, 1)?
It's the second one
For Python 3.4: There seems to be an issue with your test cases:
SyntaxError: invalid syntax
Seems fixed
Hi...I came up with a solution that passes when I hit "RUN TESTS", but I get the below message when I try to SUBMIT my solution. Can anyone help me with what this means. Thanks!
Test Passed Order should be as specified! 1 Passed 1 Failed 0 Errors
passed 2 tests, but the 3rd one says "() takes exactly 1 argument (5 given)"
@Dafaso You probably mixed up the order of
f()
andg()
. @Thoros Think about how to deal with uncertain numbers of arguments.i have the same thing what dose it mean please!?
You should think what happend if the result function need to work with more than one argument. try compose(whatever_function1,what_ever_function2)(1,2,3,4). I hope this can help you.
This comment has been hidden.
The kata specs mentioned only 2 functions will be given. Your approach would be able to solve any number of given functions which is a general case that is wise to implement
Javascript: the description says "Ruby functions will be passed" when I think you meant "Javascript functions".
In function(...a),what's the(...a) mean?please tell me?thank you
Should this be included in Fundamentals? Everyone has his own definition of "fundamentals" but, from my perspective as someone who has done only the Codecademy course in Javascript and a handful of katas, the 'fundamental' tag seems a bit misplaced here.
Yes, absolutely.
It may not be "Simple", but function composition certainly is "Fundamental".
Look at it this way:
"1243".split("").sort().join("")
is also function composition, just with a different syntax.( That could have been
compose( join(""), sort(), split("") )( "1243" )
. Mind that the arguments tocompose
are functions. )Hi everyone, I really am not quite understanding the exercise, could someone explain me a little better what we are supposed to do?
Have you followed the link in the description?
This kata really annoyed me.
After tackling it for a while and getting nowhere, I basically decided to give up.
About fifteen minutes later, while I was doing something else, the solution just sort of came to me.
Now I consider this to have a been a pretty worthwhile exercise.
If you're reading this and you're feeling stuck, don't give up!
That is all.
Javascript version of Kata could ideally include random tests; keep that in mind when authoring future Kata :)
HELLO! I can not find any information about (...a) ,Even thought i can understand it's mean, i really want to know why you use it?And how do you know it? dose it has a official explanation?thank you.
...someArray
is called the "spread operator" which can be used to transform arrays into a list of elements or arguments.More information can be found here.
JS: keep getting error Functions may be called with many arguments. But my function does work with multiple arguments. Am I right, that, for example, compose(add1,id)(1,3,2) should return 7?
No.
add1(id(1,3,2)) === add1(1) === 2
id
does not accept multiple arguments.This comment has been hidden.
Not a kata issue.
Closing.
This is a great kata. Forces the student to have a nuanced understanding of functional abstraction (and, you know, stuff). It's too bad that the description mentions passing "Ruby functions." I did this in JS and was slightly turned off by that but was happy to discover that the translation worked just fine.
I keep getting this error "TypeError: () takes exactly 1 argument (5 given)" and I don't know what's wrong, it seems to work for the test cases and when I submit it worked and still gives this error and says 2 passed what am I doing wrong?
Quote from description:
...but I guess you figured it out already.
Python: the description says "Ruby functions will be passed" when I think you meant "Python functions".
The description is global, with the exception of code blocks.
TOOK ME WAY LONGER THAN I'D LIKE TO ADMIT. I STARTED QUESTIONING MY KNOWLEDGE OF THIS LANGUAGE
GOD IM BAD
Hey, that's why the kata's here! We can all get better. ;)
OK OK
Im with you bro
An exercise in obfuscation.
Great Kata. Simple but very useful
So I solved the kata by doing some splatting. But I dont understand why it works the way it does. Can someone be nice and supply the test code for submission (with multiple arguments) so that I can try to understand it better.
Click the 'show kata test cases' bar just above this comment. Also, never share the solution to a kata, or ask anyone to share the test cases. You're basically asking for cheating.
Thank you AJ. :)
What does "Remember that the resulting function may be passed multiple arguments!" mean?
Does it mean that the resulting function will be called several times? Does it mean that the resulting function will receive a list of arguments and thus must return a list of arguments? Does my code in that case have to check what type the arguments are being provided in?
This is actually really simple, you're just going off on a tangent, which I guess is understandable for a new guy.
What I mean by this is that a function
f . g
relies on the number of arguments given tog
. Ifg
takes 2 arguments,f . g
will take 2 arguments. Here's a demo:This means that your resulting function will have to pass it's arguments to
g
, no matter what number."Remember that the resulting composed function may be passed multiple arguments" is very vague as that description could also include some variety of partial applications, etc.
r(1)(10)(12)
The example unit test uses 'expect' instead of 'assert_equal'
The example uses 'lambda' instead of 'def' for defining f and g.
Too simple, and doesn't really show in code, the point of why function composition is useful.
disable comp in clojure would be better.
I went ahead and did it for AJFarmar.
Can you please attempt this again?
after 4 years still works
This comment has been hidden.
This comment has been hidden.
CoffeeScript: test case example has a typo (add1 vs min1)
I don't see it, could you specify?
Here is the test case:
I guess you want to change line 1 to: add1 = (a) -> a + 1
Oh, the examples! Fixed.
Two small a problem. Too simple a solution. Why not have something more to think about in it?
I intended this kata to be 7 kyu, a fine rating for a simple problem. Additiionally, function composition may be simple, but it is fundemental to functional programming. There being no other function composition kata, I made this one.
Clojure: I think there's a missing close bracket in the initial test cases on line 9.
Fixed, thanks!
This comment has been hidden.
This comment has been hidden.
The kata doesn't specify that the're can be multiple arguments passed in.
This was already a posted issue, do not re-post it, please.
There should be some example test cases.
This is not an issue, next time please post a comment such as this as a suggestion.
Added example cases.
You should add some information, that there can be many arguments. It is not clear from the description.
Added, thanks!
(In Python) It might be needed to include **kwargs style arguments as a test case.
That's true, but I'd prefer keeping this language-equivocal. You could implement
**kwargs
if you were to utilize this yourself, but let's keep it on the theoretical side.This comment has been hidden.