Ad
  • Custom User Avatar

    Very poorly described, very few test examples, not a great kata at all.

  • Custom User Avatar

    I see, that's a good point then, if everybody calls them closures it's important to stick to them so everybody uses a common language.

    That being said, I feel this is a limit on php part, being an implementation-derived rathen than spec-derived language (such as C, JS, Java and others) terminology is often used in a confusing and improper way.

    That was a helpful discussion in any case for those that will read comments.

  • Custom User Avatar

    No, you can't, you also can't write a PHP anonymous function for which is_object yields false too, but that's beside the point, you wouldn't use the term object interchangeably with the function one.

    Closure, capitalized, refers to a type/class of all anonymous functions, but that is just a name.

    When you use the term closure, not capitalized, you refer to the pair of (function, lexical scope), and PHP anonymous functions do not form closure by default, that's true in every language out there.

    The fact that PHP devs, made the class Closure a parent of all anonymous functions does not imply that all anonymous functions display closure behavior.

  • Custom User Avatar

    Remembering the semicolons after the anonymous functions was the hardest part lol.

  • Custom User Avatar

    I think the text in this paragraph is incorrect if not quite misleading. Closures are a specific subset of anonymous functions, not every anonymous functions forms a closure.

    "An anonymous function is basically an unnamed function (aka closure)"

    First of all, what's a closure?

    A closure is the combination of a function and its lexical scope. In layman terms a closure can access variables in its lexical scope even after the scope has exited.

    In JavaScript the capture of outer variables is automatic, a JS function can always access variables in its outer scope. In PHP it cannot, you need to explicitly use the use keyword.

    There's also other differences in syntax and mutability, but that's irrelevant.

    In PHP, a closure is thus a type of anonymous function that captures variables from the surrounding scope via use.

    Thus, closures are a subset of anonymous function, they are absolutely not synonyms.

  • Custom User Avatar

    Nice one, I really like your solution. Kudos!

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Why would this be more efficient?