表达式中的函数,结果放入临时变量中,仅当表达式为 true 时才可访问

发布于 2024-12-05 15:15:38 字数 2582 浏览 0 评论 0原文

当表达式是返回某种值的函数时,有没有办法在临时变量中获取其处理后的值,并且只能在匹配的表达式中访问该值?

让我们随机举一个例子。

表达式中使用的函数:

function crazy($input){
    return $input * 5 - mt_rand(0, 5);
}

以及我如何看待它应该起作用:

if(crazy(2) > 5){
    $result = $crazyTemporaryVariable . ' crazies'; 
    // because calling crazy(2) here again will:
    // 1) process it again, 
    // 2) because of the `mt_rand();` return different value.
}else{
    $result = 'nothing can happen here, because $crazyTemporaryVariable is not set';
}

换句话说,如果表达式为真,它应该创建一个临时变量,否则,不会留下“任何内容”。

我知道这可以通过在表达式之前调用函数并将其返回值分配给先前设置的变量来完成。但是,您必须创建一个新变量 - 我看到一个小但仍然是性能下降,因为如果表达式不满足要求 - 该变量是无用的,但保持定义。

// we have our crazy function above, a new expression

$crazy = crazy(2);
if($crazy > 5){
    $result = $crazy . ' crazies';
}else{
    $result = 'anything...';
}

// or...

$result = $crazy > 5 ? $crazy . ' crazies' : 'anything';

请注意,这是 PHP 环境,我没有看到这种行为/可能性,也没有在我接触过的任何其他语言中看到这种行为/可能性(虽然没有太多)。

另一件事,它也很好地适用于循环:

while(crazy(2) <= 5){
    $result = $crazyTemporaryVariable . ' crazies';
}

// where normally you'd have to change the variable value;
$crazy = crazy(2);
while($crazy <= 5){
    $result = $crazy . ' crazies';
    $crazy = crazy(2);
}

注意:$crazyTemporaryVariable 只是一个虚构变量。如果我忽略了它,我怀疑它是一个变量,认为它可能是一个处理时间常量 EXPRESSION_RESULT,或者类似的东西线。但由于我的英语水平有限,我不知道要寻找什么才能找到它。

所以是的,这里的关键是定义的,因此可以访问,仅当表达式为真

所以问题是,是否有支持此功能的语言,如果我在 PHP 中忽略了它,那么我该如何访问它+参考?

写完上面的所有内容后,我记住了这种行为:

if(($crazy = crazy(2)) > 5){
    $result = $crazy . ' crazies';
}else{
    $result = $crazy . ' crazies, but... not crazy enough.';
}

这几乎就是我正在寻找的,但它与之前定义我们的变量几乎相同,如果表达式匹配,您可以访问函数的结果,因此,它这次不会砍了

提前致谢!

虚构的例子

三元和javascript..

// could be: (keep in mind, this would not work with our _crazy_ function, because of random number.)
var result = string && string.search(/\s/) ? string.substr(0, __tempvar__) : string; // not available
// ...but instead is
var result = string.search(/\s/g) > 0 ? string.substring(0, string.search(/\s/g)) : string; // +1 repeated function call that affects performance.

When an expression is a function that returns some kind of value, is there a way to get it's processed value in a temporary variable, that can be accessed only within matching expression?

Let's make a random example.

function used in expressions:

function crazy($input){
    return $input * 5 - mt_rand(0, 5);
}

And how I see this should work:

if(crazy(2) > 5){
    $result = $crazyTemporaryVariable . ' crazies'; 
    // because calling crazy(2) here again will:
    // 1) process it again, 
    // 2) because of the `mt_rand();` return different value.
}else{
    $result = 'nothing can happen here, because $crazyTemporaryVariable is not set';
}

In other words, it should create a temporary variable if the expression is true, else, "nothing" is left behind.

I'm aware that this can be done with the function call before the expression and assigning it's returned value to a previously set variable. But, you have to create a new variable- I see a small, but still a performance drop, because if the expression doesn't meet the requirements- the variable is useless, but stays defined.

// we have our crazy function above, a new expression

$crazy = crazy(2);
if($crazy > 5){
    $result = $crazy . ' crazies';
}else{
    $result = 'anything...';
}

// or...

$result = $crazy > 5 ? $crazy . ' crazies' : 'anything';

A note, this is PHP environment where I haven't seen this behavior/possibility nor in any other language which I've come in contact with (there are not much tho').

One more thing, it nicely applies to loops too:

while(crazy(2) <= 5){
    $result = $crazyTemporaryVariable . ' crazies';
}

// where normally you'd have to change the variable value;
$crazy = crazy(2);
while($crazy <= 5){
    $result = $crazy . ' crazies';
    $crazy = crazy(2);
}

Note: $crazyTemporaryVariable is just an imaginary variable. And if I've overlooked it, I doubt that it's a variable, think it could be a processing time constant EXPRESSION_RESULT, or something along those lines. But because of my English limitations, I have no idea what to look for to find it.

So yes, the key here is- defined, therefore accessible, only if expression is true.

So the question is, are there languages that support this and if I've overlooked it in PHP how can I access it then + reference, please?

After writing all what's above, I rememebered about this behavior:

if(($crazy = crazy(2)) > 5){
    $result = $crazy . ' crazies';
}else{
    $result = $crazy . ' crazies, but... not crazy enough.';
}

This is almost what I'm looking for, but it's nearly the same as defining our variable before, and you can access the result of the function if the expression matches, therefore, it won't cut this time.

Thanks in advance!

imaginary example

Ternary and javascript..

// could be: (keep in mind, this would not work with our _crazy_ function, because of random number.)
var result = string && string.search(/\s/) ? string.substr(0, __tempvar__) : string; // not available
// ...but instead is
var result = string.search(/\s/g) > 0 ? string.substring(0, string.search(/\s/g)) : string; // +1 repeated function call that affects performance.

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

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

发布评论

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

评论(1

北城孤痞 2024-12-12 15:15:38

我看到性能略有下降,但仍然存在,因为如果表达式不满足要求,则变量无用,但仍保持定义状态。

声明变量是否会导致性能差异取决于语言及其实现。性能差异是否重要取决于应用程序;可能只有当变量引用一个非常大的对象时,该对象不会被销毁/GC,因为它仍然有一个引用。在任何半体面的语言实现中,变量本身实际上应该是免费的。

换句话说,如果表达式为真,它应该创建一个临时变量,否则,“什么也不会留下”。

这使得您的语言的作用域规则非常复杂。您可能想要在具有类似 C 作用域规则的语言中执行以下操作:

# declare $result
{
    $crazy = crazy(2);
    if ($crazy > 5){
        $result = $crazy . ' crazies';
    } else {
        $result = 'anything...';
    }
    # end of scope for $crazy, so it disappears
    # and the object it refers to takes up no more space
}

所以问题是,是否有语言支持这一点

IIRC,Perl 将最后一个表达式的值(或类似的东西)存储在 $_ 中。我个人觉得那很可怕。简而言之,你真的不应该想要这个;有更简单、更有原则的方法来处理这个问题。

I see a small, but still a performance drop, because if the expression doesn't meet the requirements- the variable is useless, but stays defined.

Whether declaring a variable makes any performance difference at all depends on the language and its implementation. Whether the performance difference matters depends on the application; probably only if the variable refers to a very large object that is not destroyed/GC'd because it still has a reference. Variables themselves should be practically free in any half-decent language implementation.

In other words, it should create a temporary variable if the expression is true, else, "nothing" is left behind.

This makes your language's scoping rules very complicated. What you might want to do in a language with C-like scoping rules is:

# declare $result
{
    $crazy = crazy(2);
    if ($crazy > 5){
        $result = $crazy . ' crazies';
    } else {
        $result = 'anything...';
    }
    # end of scope for $crazy, so it disappears
    # and the object it refers to takes up no more space
}

So the question is, are there languages that support this

IIRC, Perl stores the value of the last expression (or something like that) in $_. I personally find that hideous. In short, you really shouldn't want this; there are simpler, more principled ways of dealing with this problem.

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