Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
kata hint != kata suggestion (you can always provide suggestion on how to improve description
Not a suggestion.
This comment is hidden because it contains spoiler information about the solution
Lol I read "sum of the cube of the digits is divisble by 2" and I knew I'd skip that immediatly, probably not too hard but I dropped out of school for a reason ahah
Thanks for the clear description!
Yes this most likely crossed my "Note: ..." edit where i do specify that the inner function is called by the return statement of the outer function, not by mere fact of having the outer function called with multiple parentheses set.
This is misleading because it implies the call of inner functions and the passing of arguments is happening solely due to the
func(1)(2)(3)
statement.so this is all explained by the basic premise of that you can call a function and get something back. no separate mode of operation is needed for that to happen, it's just the same thing done repeatedly.
and having a function nested inside another doesn't mean you can call it, you can only call it if that function is returned, same as how you can't be aware of some integer unless that integer is returned
likewise, you can return any function you want, regardless of where it is defined:
Almost.
You're not passing arguments to an inner function. You're calling a function. (has nothing to do with where it's defined, you have a function, you call it)
calling a function looks like this:
you're doing this:
func is a function. you call it. you give it 1. it gives you a value back. that value is a function. you assign the variable f to it.
f is a funciton. you call it. you .........
g is ...
h isn't a function, and you're not trying to call it. you convert it to string and write that string to stdout
Ok so I've found a satisfactory answer here : https://stackoverflow.com/questions/42874825/python-functions-with-multiple-parameter-brackets
I'm a bit ashamed it's that simple, but basically you can pass arguments to inners functions by calling the outer function with two or more sets of parentheses.
In python (sorry, did not specify language earlier...) you could do:
Outputs:
6
So ...
func(1)(2)(3)
passes the args1, 2, 3
to the three functions ... yeah ... (edit: misleading statement, see note below)Note : don't forget to return the inner functions ! It feels a bit weird, but keep in mind that
func1
returnsfunc2
which actually callsfunc2
right away. It's weird (to me) because in this case the verb "return" is insdiguishable from the verb "call". So, right whenfunc2
gets called (or returned, same thing!), it looks for arguments (in the example provided, it's(b)
which will take the value2
, and the arguments are provided with the callfunc1(1)(2)(3)
.Note how this statement:
let result = chained([f1,f2,f3])(0)
can be broken down:Now your task would be to write
chained
in such a way, that when it returns something, this thing can be called in the next line assomething(0)
.Don't feel bad for not getting this. The concept of high-order functions is not a complex one, but at the same time not easy to grasp for people not familiar with it. The fact that a function can be passed as a parameter to another function, and that a function can return another function, is quite often mind-boggling for beginners.
Just do some research on "high order functions", "function chaining", "function composition", or come to Codewars Discord for further help.
You should probably be looking at the individual calls.
a + b + c
doesn't make
+
behave differently from here:a + b
in both cases,
+
gets two values as input and produces an output.and, you can rewrite the first one to this:
which you can also do with all the calls involved in this kata. there's only ever one call happening at a time, and there's no special behaviour.
Ok then I must interpret the input in some erronous way. But if I do:
I get the output:
TypeError: 'NoneType' object is not callable.
At first I interpeted it (...any interpreation in code is wrong ik, but begginners always will somehow...) as "you cannot call the integer 0 as it is a NoneType object and those cant get called."
But actually, NoneType refers to the returned value in my defined functions which indeed do not return anything, which is equivalent to returning None.
If I change my functions to return say, a string, then the error is updated to:
TypeError: 'str' object is not callable.
You don't have to answer this, I'm sure I can find answers on Google, but the question is now: so the (0) here is some kind of parameter that gets passed to all functions ? the first function ? the latter one ? not that at all ? And why is it called input (and not say, args) ?
Edit: sorry for 99 edits, learning markdowns and I'm a typo minigun. Also thanks for the reply it helped me unstick my line of thinking <3
If you mean syntax like this:
chained([f1,f2,f3])(0)
, then it is a valid syntax.Can someone explain why this kata description seems, to me, like it could just have been:
Your function should look like that: "some valid function call with example arguments"...
And you should output something like this: "some output related to the example given"...
Why oh god why is this "Function(args)(input)" abomination standing here and no one seems to mind ... ? There's something I must not get. A Function(args)(input) call is not valid syntax.
Much clearer than current top 1 imo.
Loading more items...