在 Raku 中没有换行符

Lar*_*een 4 raku

我想连续打印偶数,但我不能。

use Terminal::ANSIColor;
# Wanna print even numbers in red
for <1 2 3 4>
{ $_ %2 == 0 ?? say color('red'),$_,color('reset') !! say $_ }
Run Code Online (Sandbox Code Playgroud)

printf 似乎不适用于Terminal::ANSIColor指令,put也不起作用。

是否有任何开关可以say使其在没有换行符的情况下打印?如何Terminal::ANSIColor连续打印那些 格式化的部分?

Bra*_*ert 7

say 基本上定义为:

sub say ( +@_ ) {
    for @_ {
        $*OUT.print( $_.gist )
    }

    $*OUT.print( $*OUT.nl-out );
}
Run Code Online (Sandbox Code Playgroud)

如果您不想要换行符,您可以更改$*OUT.nl-out或使用printand的值gist

say $_;

print $_.gist;
Run Code Online (Sandbox Code Playgroud)

在许多情况下,调用的结果.gist.Str. 这意味着您甚至不需要调用.gist.

use Terminal::ANSIColor;
# Wanna print even numbers in red
for <1 2 3 4> {
    $_ %% 2 ?? print color('red'), $_, color('reset') !! print $_
}
Run Code Online (Sandbox Code Playgroud)

(请注意,我使用了被 operator 整除的整数%%。)


say适用于人类,这就是它使用.gist并添加换行符的原因。

如果您想要更细粒度的控制,请不要使用say. 使用printput代替。

  • @LarsMalmsteen 大概 Emacs 正在等待换行符。使用“put()”代替可能更有意义。 (2认同)