Recursion with anonymous function [duplicate]
-
20-12-2020 - |
Question
Possible Duplicates:
javascript: recursive anonymous function?
Anonymous recursive PHP functions
I was wondering... Is it possible to do recursion with anonymous function?
Here is one example: I need to get six-chars long string which may contain only numbers and spaces. The only rules are that it cannot start or end with spaces. We check for that and if that occurs - just call recursion on the same, anonymous, function. Just how!?
function() {
$chars = range(0, 9);
$chars[] = ' ';
length = 6;
$count = count($chars);
$string = '';
for ($i = 0; $i < $length; ++$i) {
$string .= $chars[mt_rand(0, $count - 1)];
}
$string = trim($string);
if (strlen($string) !== $length) { // There were spaces in front or end of the string. Shit!
// Do recursion.
}
return $string;
}
Solution
Yes it is, but I wouldn't recommend it as it's a bit tricky ;)
First possibility:
<?php
$some_var1="1";
$some_var2="2";
function($param1, $param2) use ($some_var1, $some_var2)
{
call_user_func(__FUNCTION__, $other_param1, $other_param2);
}
?>
Another one:
<?php
$recursive = function () use (&$recursive){
// The function is now available as $recursive
}
?>
Examples taken from http://php.net/
OTHER TIPS
The answer is complicated but not impossible. It took me several minutes to figure out. We first must define a utility function called $combinator().
The solution to your problem:
$combinator(
function($self) { function() use (&$self) {
$chars = range(0, 9);
$chars[] = ' ';
length = 6;
$count = count($chars);
$string = '';
for ($i = 0; $i < $length; ++$i) {
$string .= $chars[mt_rand(0, $count - 1)];
}
$string = trim($string);
if (strlen($string) !== $length) {
return $self();
}
return $string;
} }
);
The definition of $combinator():
$combinator = function($principle)
{
(function($transept) use (&$principle)
{
$principle(
function($arguments) use (&$transept)
{
call_user_func_array($transept($transept), $arguments));
}
);
})
(function($transept) use (&$principle)
{
$principle(
function($arguments)
{
call_user_func_array($transept($transept), $arguments);
}
);
});
}
A much saner method to do the same thing. Requires only one loop as well.
$chars = array_merge(range(0, 9), array(' '));
$string = mt_rand(0, 9);
for ($i = 1; $i <= 4; $i++) {
$string .= $chars[array_rand($chars)];
}
$string .= mt_rand(0, 9);
Sorry for sidestepping the actual question though.
use goto
function() {
start:
$chars = range(0, 9);
$chars[] = ' ';
length = 6;
$count = count($chars);
$string = '';
for ($i = 0; $i < $length; ++$i) {
$string .= $chars[mt_rand(0, $count - 1)];
}
$string = trim($string);
if (strlen($string) !== $length) { // There were spaces in front or end of the string. Shit!
goto start;
}
return $string;
But it's not the best idea to use goto.