简体   繁体   English

在Perl中使用map或grep

[英]The use of map or grep in Perl

I've been writing perl code full-time for a couple months now(bioinformatics), and am always trying to improve my skills. 我已经写了几个月的全职perl代码(生物信息学),并且总是试图提高我的技能。 Just today, it dawned on me that I never use map or grep. 就在今天,我突然意识到我从不使用地图或grep。 Looking back through my code I realize these tools could save me a couple lines here or there, but only at the expense of the flexibility of a foreach loop. 回顾一下我的代码,我意识到这些工具可以在这里或那里省去几行,但只是牺牲了foreach循环的灵活性。 My question is as follows: 我的问题如下:

Are there any circumstances you have run across where using map or grep has brought significant advantage over a foreach/for loop, beyond saving a line or two of code? 你有没有遇到过使用map或grep比foreach / for循环带来显着优势的情况,除了保存一行或两行代码之外?

Thanks for your time! 谢谢你的时间!

The Schwartzian Transform would be an example: Schwartzian变换就是一个例子:

@sorted = map  { $_->[0] }
          sort { $a->[1] cmp $b->[1] }
          map  { [$_, foo($_)] }
          @unsorted;

You could do that with a pile of foreach loops but you'd have to pick them apart to figure out what was going on; 你可以用一堆foreach循环来做到这一点,但是你必须将它们分开以找出发生了什么; once you've seen the Schwartzian Transform you recognize the idiom immediately. 一旦你看到Schwartzian变换,你就会立即认出这个成语。

In general I think map and grep are good in that they allow you to clearly and compactly represent your intent without layers of syntax. 一般来说,我认为mapgrep很好,因为它们允许你在没有语法层的情况下清晰而紧凑地表示你的意图。 If you see a map then you know that some sort of simple data structure transformation is going on; 如果您看到map那么您就知道正在进行某种简单的数据结构转换; if you see a grep then you know that some filtering/selection is going on. 如果你看到一个grep然后你知道正在进行一些过滤/选择。 You could do it all with foreach but the intent of your code isn't as clear as it would be with map or grep ; 您可以使用foreach完成所有操作,但代码的意图并不像mapgrep那样清晰; you could even do it all with if and goto if you wanted to but then your intent would be buried under even more syntax and state tracking. 如果你愿意,你甚至可以用ifgoto完成所有这些,但是你的意图将被埋没在更多的语法和状态跟踪之下。

I'm constantly using map and grep . 我经常使用mapgrep And apply , first , any , and many others from List::MoreUtils . 并且first apply List::MoreUtils any其他List::MoreUtils I find that, in general, they explain what the code is doing as opposed to how the code is doing it. 我发现,在一般情况下,他们解释代码是做,而不是代码是如何做的。

In general, I find that when my code reads the same as the spec, it's more likely to be correct as well as more likely to handle corner/edge cases. 一般来说,我发现当我的代码与规范读取相同时,它更可能是正确的,并且更有可能处理角落/边缘情况。 Perl allows me to do this much better than any language I've used in the past, and I take advantage of it. Perl允许我比过去使用的任何语言都做得更好,并且我利用它。

For example, if my spec says that I will do foo() if $blah is in some list, my code reads exactly that way: 例如,如果我的规范说如果$blah在某个列表中,我将执行foo() ,我的代码就是这样读取的:

foo() if any { $_ eq $blah } some_list();

Same idea for the rest of these tools. 其余这些工具的想法相同。 The code and the spec look eerily similar, and that's one of the great things about Perl. 代码和规范看起来非常相似,这是Perl的优点之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM