使用百分号作为前缀运算符名称的一部分 [英] Using percent sign as part of the name for a prefix operator

查看:217
本文介绍了使用百分号作为前缀运算符名称的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为对于一个函数名称,%of 会比 percent-of 更简单易读。

I thought that %of would be more readable and concise than percent-of for a function name. Here is working code using the longer name.

#!/bin/env perl6

# Quick stats from gene_exp.diff file

sub percent-of
{
    return sprintf('%.1f', (100 * $^a/ $^b).round(0.1));
}

my $total = first-word-from("wc -l       gene_exp.diff ") -1;  # subtract one for the header
my $ok    = first-word-from "grep -c OK  gene_exp.diff";
my $yes   = first-word-from "grep -c yes gene_exp.diff";

put '| total |    OK | OK % |   yes | yes % | yes / OK |';
put "| $total | $ok | { percent-of $ok, $total } | $yes | { percent-of $yes,$total } | { percent-of $yes, $ok } |";



sub first-word-from ( $command )
{
    return ( qqx{ $command } ).words[0];
}

由于我把子程序的名字放在它的参数前面,我会认为这将是一个前缀运算符。所以这里是我认为可以使短名称工作(即使用子前缀:<%of> 来声明该函数):

Since I'm putting the subroutine name before its arguments, I would think that this would be a prefix operator. So here is what I thought would work to make the shorter name work (i.e. using sub prefix:<%of> for declaring the function):

#!/bin/env perl6

# Quick stats from gene_exp.diff file

sub prefix:<%of>
{
    return sprintf('%.1f', (100 * $^a/ $^b).round(0.1));
}

my $total = first-word-from("wc -l       gene_exp.diff ") -1;  # subtract one for the header
my $ok    = first-word-from "grep -c OK  gene_exp.diff";
my $yes   = first-word-from "grep -c yes gene_exp.diff";

put '| total |    OK | OK % |   yes | yes % | yes / OK |';
put "| $total | $ok | { %of($ok, $total) } | $yes | { %of($yes,$total) } | { %of($yes,$ok) } |";

sub first-word-from ( $command )
{
    return ( qqx{ $command } ).words[0];
}

但是我得到以下错误:

But I get the following error:

| total |    OK | OK % |   yes | yes % | yes / OK |
Too few positionals passed; expected 2 arguments but got 1
  in sub prefix:<%of> at /home/bottomsc/bin/gene_exp_stats line 6
  in block <unit> at /home/bottomsc/bin/gene_exp_stats line 15

我确定像我一样尝试是可能的。我已经看到了比这更疯狂的功能,比如中缀我不关心运营商 \(°_O)/¯ 。我究竟做错了什么?在尝试使用和不使用圆括号调用时,我会得到完全相同的错误,所以这不是问题。

I'm sure something like what I'm attempting is possible. I've seen much crazier functions than this, like the infix I don't care operator ¯\(°_o)/¯. What am I doing wrong? I get the exact same error when trying calling %of with and without parentheses, so that isn't the issue.

在我输入这个问题时,我意识到我应该尝试遵循示例刚刚引用了,并将其作为中缀运算符运行并运行。不过,我仍然好奇为什么我的前缀运算符代码不起作用。这可能是我忽视的一些非常基本的东西。

As I was typing up this question, I just realized that I should try following the example just cited and doing this as an infix operator and it worked. However, I'm still curious as to why my prefix operator code didn't work. It's probably something very basic that I'm overlooking.

更新:

这是作为中缀运算符完成的工作代码。但是我仍然对我做的错误前缀版本感到好奇:

Here is the working code when done as an infix operator. But I'm still curious about what I was doing wrong with the prefix version:

#!/bin/env perl6

# Quick stats from gene_exp.diff file

sub infix:<%of>
{
    return sprintf('%.1f', (100 * $^a/ $^b).round(0.1));
}

my $total = first-word-from("wc -l       gene_exp.diff ") -1;  # subtract one for the header
my $ok    = first-word-from "grep -c OK  gene_exp.diff";
my $yes   = first-word-from "grep -c yes gene_exp.diff";

put '| total |    OK | OK % |   yes | yes % | yes / OK |';
put "| $total | $ok | { $ok %of $total } | $yes | { $yes %of $total } | { $yes %of $ok } |";

sub first-word-from ( $command )
{
    return ( qqx{ $command } ).words[0];
}


推荐答案


我认为%对于一个函数名称来说会更易读,更简洁。

非常不赞同这看起来完全像一个哈希印记。如果我和你一起工作,如果我遇到过这个问题,我会坚持下去。 百分比作为中缀有什么问题? > 5% - 100 返回0.05或者什么?

Strongly disagree given this looks exactly like a hash sigil. If I was working with you I would go nuts if I ever came across this. What's wrong with percent-of as an infix? 5 percent-of 100 returning 0.05 or something?

我读了%as of hash called。在阅读Perl一百万年之后,我认为%是一个新定义的运算符,意思是以%为前缀的百分比。所以它在语言上是不可读的。它简洁并不是一个很好的理由,它有更多的认知负担,所以再次可读性较差。我不完全确定这种批评的平衡是什么?除了它的可怕和酷,你可以做到这一点......

I read %of as hash called of. Never in a million years when reading Perl will I think % as a newly defined operator meaning 'percent' prefixed like that. So it's not readable given the language. It being concise isn't really a great justification either, it has much greater cognitive load so is again less readable. I'm not entirely sure what the balance to that criticism is? Other than it's frightening and cool you can do it at all...

这篇关于使用百分号作为前缀运算符名称的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆