匿名函数的递归

发布于 2024-11-29 18:18:58 字数 879 浏览 1 评论 0原文

可能的重复:
javascript:递归匿名函数?
匿名递归 PHP 函数

我想知道...是否可以使用匿名函数进行递归?

这是一个例子:我需要获取六个字符长的字符串,该字符串可能只包含数字和空格。唯一的规则是它不能以空格开头或结尾。我们检查是否存在这种情况,如果发生这种情况 - 只需在同一个匿名函数上调用递归即可。到底如何!?

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;

}

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一生独一 2024-12-06 18:18:58

是的,但我不会推荐它,因为它有点棘手;)

第一种可能性:

<?php
$some_var1="1";
$some_var2="2";
function($param1, $param2) use ($some_var1, $some_var2)
{
    call_user_func(__FUNCTION__, $other_param1, $other_param2);
}
?>

另一种:

<?php 
$recursive = function () use (&$recursive){ 
    // The function is now available as $recursive 
} 
?> 

示例取自 http://php.net/

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/

没有心的人 2024-12-06 18:18:58

答案很复杂,但并非不可能。我花了几分钟才弄清楚。我们首先必须定义一个名为 $combinator() 的实用函数。

问题的解决方案:

$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;
} }
);

$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);
      }
    );
  });
}

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);
      }
    );
  });
}
一身仙ぐ女味 2024-12-06 18:18:58

做同样事情的更明智的方法。也只需要一个循环。

$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);

不过,很抱歉回避了实际的问题。

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.

滥情稳全场 2024-12-06 18:18:58

使用 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;

但使用 goto 并不是最好的主意。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文