这个循环还有哪些其他方法可以重写?
得到这个简单的循环:
use Config::Simple:from<Perl5>;
my $cfg = Config::Simple.new(syntax => 'ini');
%config{'short_name'} = 'blah';
for %config.kv -> $k, $v {
$cfg.param("%config{'short_name'}.$k", $v);
}
工作正常。但我想更熟悉实现相同目标的其他方法,即使只是为了轻松地阅读其他人的代码。另外,循环似乎是“旧”学校的做事方式,而不是很“像 Raku”,我需要更舒适地以更高级的方式使用函数。
不管怎样,为了伸展我的新 Raku 肌肉,我想出了这个单行作为替代方案:
map(-> $k, $v { $cfg.param("%config{'short_name'}.$k", $v) }, %config.kv);
它的可读性较差(至少对于我未经训练的眼睛来说),但它有效。
我的预感是有一些好方法可以使这段代码更加简洁和可读。有兴趣看看我是否是对的。
Got this simple loop:
use Config::Simple:from<Perl5>;
my $cfg = Config::Simple.new(syntax => 'ini');
%config{'short_name'} = 'blah';
for %config.kv -> $k, $v {
$cfg.param("%config{'short_name'}.$k", $v);
}
Works fine. But I want to get more familiar with other ways to achieve the same goal, if only to get comfortable reading other people's code. Plus, loops seem to be the "old" school way of doing things and not very "Raku-like" and I need to get more comfortable working with using functions in more advanced ways.
Anyway, to stretch my new Raku muscles, I came up with this one-liner as an alternative:
map(-> $k, $v { $cfg.param("%config{'short_name'}.$k", $v) }, %config.kv);
It's less readable (at least to my untrained eye), but it works.
My hunch is there is some good way of making this code both more concise and readable. Interested in seeing if I might be right.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重写(恕我直言,如果变量发生更改,
for
比map
更合适)by
set_block
rewritten (IMHO,
for
is more suitable thanmap
, if a variable is changed)by
set_block