4 kyu

Currying vs. Partial Application

2,408 of 2,835surtich
Description
Loading description...
Functional Programming
  • Please sign in or sign up to leave a comment.
  • ase444ka Avatar

    This comment has been hidden.

  • CClairvoyant Avatar

    This comment has been hidden.

  • atakchidi Avatar

    For dynamic languages with varargs support and high-order functions this is a very simple one liner. It was like 7kyu for me

  • John Lebens Avatar

    My code passes all test cases except the very last 'tree of calls'. I wish there was a simpler test that did the same thing that was understandable enough to debug. The way that is written I have no idea how it expects to get a count from zero to 7. I keep returning 8 function calls instead. This criteria should be explained as part of the question prompt.

  • niceneasy Avatar

    There really should be some random tests with functions of higher arity. I plugged in a lazy solution that actually relies on the fact that there is 0 to 4 arguments in test functions and it worked. Now I am not satisfied with myself but too lazy to change the solution.

  • FArekkusu Avatar

    Newer versions of JavaScript should be enabled.

  • FArekkusu Avatar

    Python 3 should be enabled.

  • kikosoft Avatar

    Could this Kata possible be easier to solve in Javascript than in PHP? I only did the PHP version, but I found it to be more difficult than other level 4 Kayas.

    All 12 current PHP solutions, including mine, use stuff from the PHP manual where it says in a big red box: "Warning: This function is currently not documented; only its argument list is available.". In other words: "Who knows what this does? Use at your own peril.". This is clearly not something you would ever want use on a daily basis.

    Katas always seem have the same difficulty level, regardless of language, but perhaps that is not always right?

  • lechevalier Avatar

    This comment has been hidden.

  • lechevalier Avatar

    Python: only one solution passes this test (a.k.a. composing (partial applying) a function that has already enough parameters to be evaluated) This is unconsistent with the fact you have to ignore additional parameters when currying a function.

    from random import randint
    a = randint(1, 10)
    def double():
      return a * 2
    result = a * 2
    test.assert_equals(curryPartial(curryPartial(double), 1), result)
    

    You should add this test or remove handling of additional arguments from your spec.

  • Daniel Ray Avatar

    Tough kata, but a very nice one. After completion, it was fascinating to see how other users solved it. Apparently there are many ways to do it.

    May I suggest editing the tags on this kata? I would definitely add the tag: "metaprogramming". Also "advanced language features" seems a bit more appropriate than "fundamentals".

  • OscarAlvarez Avatar

    Looks like there's a bad spec, according to the examples you provided. Spec provides ultimately 4 arguments, but the function add being passed in only handles 3. My implementation takes those arguments without a problem and passes them all to add, but the expected result is 10 when it should be 6 - again, according to the examples.

  • Voile Avatar

    JS: Needs example tests

  • donaldsebleung Avatar

    [PHP] Although there is no official documentation on the type of casing that user-defined function names should adopt, the fact that most, if not all, built-in PHP functions are in snake_case suggests that that should be the convention adopted. Please change the name of the function in PHP from curryPartial() to curry_partial().

  • donaldsebleung Avatar

    My bad for approving this Kata without checking the Python and PHP versions of this Kata beforehand (I only completed this Kata in JavaScript at the time of approval), but ...


    Codewars Forums - Kata Best Practices - Follow Conventions

    [Python] Function names should always be in snake_case and not camelCase (source: PEP 8) so please change the name of the user function from curryPartial to curry_partial.

  • Axure Avatar

    This comment has been hidden.

  • tselishev-semen Avatar

    In my opinion, your tests don't cover all cases, for example, I found that a lot of solutions which don't take care of such case: var fn = curryPartial((a,b,c)=>a+b+c) var fn1 = fn(1) fn1(4,5) // returns 10 fn1(6,7) // returns 10, but must returns 14

    Could you fix this problem? Or I can add such tests for your kata if you want

  • AntraxMiope Avatar

    I never worked with "functions within functions" (is it officially called "Closure"?) Is there any kata envolving this specific theme and easier to understand/solve?

  • DeanBrown Avatar

    Interesting kata. Getting my head around what it was asking for was a bit difficult, but once done it wasn't too hard. ES6 enables some pretty elegant solutions.

  • talamb Avatar

    If you can you should make this for Lisp or Racket. Lambda calc is the core of Racket.

  • fadeev_ab Avatar

    I think it's good to add a hint about specific Python features which help to solve a kata. At least it's a Python's kata, and it should help to learn a language, not just functional programming concepts.

    P.S.: Also I think the difficulty of kata is not of "fundamentals": this is fundamental for any functional language, but not as excersise for Python.

  • nickie Avatar

    This comment has been hidden.

  • christofsteel Avatar

    I don't think

    curryPartial(add)()(1)()()(2)(3, 4, 5, 6)

    should be interpreted at all. You should throw a TypeError, because you gave it to many arguments. It would also be nice to have the examples as testcases.

    Maybe you should add to the description, that kwargs (and arguments with defaults in general) are ignored.

    Other than that, I really enjoyed solving this kata.

  • foxxyz Avatar

    It would've been nice if the tests given in the instructions were also given in the tests window. Not nice to have to write the tests out yourself if they could already have been given.

  • walshd15 Avatar

    Excercise could be clearer. I was unsure of where to start. more examples would probably be good.

  • alfe Avatar

    This comment has been hidden.

  • alfe Avatar

    I find it peculiar that superfluous arguments are to be ignored. That is no good coding style, I'd say. Errors will go unnoticed, and taken strictly, things like cur(add)(1)(2)(3)(4) should be evaluated as add(1,2,3)(4) which would mean to call 6(4) and ints aren't callable. So this breaks the pattern.

  • xcthulhu Avatar

    It's probably a good idea to drop the discussion of types in the description.

    For one thing, in Haskell we have:

    \ f -> curry ( curry f ) ≠ \ f -> curry f (since curry . curry has a different type than curry)

    However, in this kata we have

    lambda f: curryPartial(curryPartial(f)) ≣ lambda f: curryPartial(f)

    In fact curryPartial is not typeable in the simply typed or polymorphically typed λ-calculus. Given that both Javascript and Python are dynamically typed, talking about types doesn't really make a lot of sense for them.

  • peatey Avatar

    It's not obvious the function should handle any number of arguments, i.e. the number of arguments the passed funtion accepts.

  • crisptrutski Avatar

    Needs some polish to make it out of beta for Python IMHO

    There are some typos in example code (eg. curriedAdd(add)), and no unit tests. I realised the typo because I used the inline examples. Codewars is printing functions as empty strings, so found this particular mistake annoying and took me a while to debug.

    The repeated application of the function converter (curryParial(curryPartial(fn))), I found that tricky because of losing an easy way to read the original arity. Thought of setting some kind of property on function, but new to Python and wasn't sure how. See how now from other answers, but also quite a few people got lazy like me and just used exception handling instead. Not sure the value of handling this contingency anyway, since currying + variadic doesn't make much sense, and double application of currying would be a no-op and something to avoid.

  • user1358746 Avatar

    This should be ranked as a pretty difficult kata, the description alone suggests architecture level, in my opinion borders on self gratification for the kata author,especially since there are only two people involved in the discussion of this kata. The explanation is rather abstract and would only make sense to those who operate at that level. Perhaps a more verbose and structured description could encourage us lesser mortals to attmpt the kata?

  • wthit56 Avatar

    I was wondering... doesn't this just amount to a simple partial? Allowing any number of args in, until they meet the signature of the function, then running the function?

  • wthit56 Avatar

    Very nicely done.

  • xcthulhu Avatar

    Port to python?