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.
Very poorly described, very few test examples, not a great kata at all.
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.
this kata is not avalaible in newer PHP language versions (
7.4
,8.0
) because the user's solution and the tests are no longer concatenated into a single file in those versions, making the tests unable to find the variables$hello_world
and$person_description
.Adding
require '_solution.php';
at the top level scope in the test suite fixes the issue.I understand your point, but it does not seem to be official PHP terminology. Browsing the official documentation for anonymous functions, one can read sentences such as:
The whole document uses the terms "anonymous function" and "closure" interchangeably
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.
can you write an anonymous function for which
instanceof Closure
yieldsfalse
?Remembering the semicolons after the anonymous functions was the hardest part lol.
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.
Nice one, I really like your solution. Kudos!
This comment is hidden because it contains spoiler information about the solution
Why would this be more efficient?