Earn extra honor and gain new allies!
Honor is earned for each new codewarrior who joins.
Learn more
  • Yeah just for the purpose of the kata since that is the direct equivalent JS's Array Filter function. It's good to understand the lambda syntax for usage with map and reduce, as well as other more involved filter functions (where it wouldn't be as appropriate to use the "x for x" expression). Objectively though, neither solution is better than the other if the only goal is to return an array with only even integers.

    It's kind of similar in a way to the dynamic programming problems with the fibonacci or factorial sequence where you utilize recursion. For the purpose of writing a function that generates those sequences, recursion isn't necessary but it makes for good practice to start developing an understanding of it.

    For many complex programming topics, it's usually easier to understand how they work in a simple problem that may not necessarily require their usage. But learning how they work on the simple problems will make the more complex problems that require them more easy to understand.

  • Ah, so you're trying to say thah it would have been better to use filter with a lambda function other than a conditional like this ?

  • While this solution effectively works the same and is definitely a correct answer to the problem, I would say that it somewhat defeats the purpose of the kata. Due to the nature of python, it is not reasonably possible to limit syntax usage in manner that you're able to in JS, especially for this problem. Perhaps I might have been able to have the function return the filter expression instead of the filtered array itself, but that might have been overkill for a white kata.

    This is not at all a wrong way of "filtering" any array using a conditional, but doesn't do anything to teach the usage of lambda expressions in python. In most practical environments you would probably use the "element for element in iterable" expressions, but it is useful to understand how lambda expressions work.