小编Håk*_*and的帖子

找不到包java.nio.file

我的java编译器找不到java.nio.file包.考虑:

import java.nio.file.*;
public class Test {
    public static void main(String[] args) { 
        Path currentRelativePath = Paths.get("");
    }
}
Run Code Online (Sandbox Code Playgroud)

用...编译

bash$ javac Test.java
Run Code Online (Sandbox Code Playgroud)

Test.java:1: package java.nio.file does not exist
import java.nio.file.*;
                ^
Test.java:5: cannot find symbol
symbol  : class Path
location: class Test
    Path currentRelativePath = Paths.get("");
    ^
Test.java:5: cannot find symbol
symbol  : variable Paths
location: class Test
    Path currentRelativePath = Paths.get("");
                               ^
3 errors
Run Code Online (Sandbox Code Playgroud)

我正在使用Ubuntu 12.04,我认为我安装了JDK 7(参见:package java.nio.file不存在)

bash$ java -version
java version …
Run Code Online (Sandbox Code Playgroud)

java nio

9
推荐指数
2
解决办法
1万
查看次数

试图改进Encode :: decode警告消息:$ SIG {__ WARN__}处理程序中的Segfault

我正在努力改进发出的警告信息Encode::decode().我希望它不打印模块的名称和模块中的行号,而是打印正在读取的文件的名称以及该文件中找到格式错误的数据的行号.对于开发人员来说,原始消息可能很有用,但对于不熟悉Perl的最终用户来说,它可能毫无意义.最终用户可能更愿意知道哪个文件存在问题.

我首先尝试使用$SIG{__WARN__}处理程序解决这个问题(这可能不是一个好主意),但我得到了一个段错误.可能是一个愚蠢的错误,但我无法弄清楚:

#! /usr/bin/env perl

use feature qw(say);
use strict;
use warnings;

use Encode ();

binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';

my $fn = 'test.txt';
write_test_file( $fn );

# Try to improve the Encode::FB_WARN fallback warning message :
#
#   utf8 "\xE5" does not map to Unicode at <module_name> line xx
#
# Rather we would like the warning to print the filename and the line number:
#
#   utf8 "\xE5" does not map to Unicode …
Run Code Online (Sandbox Code Playgroud)

perl

9
推荐指数
1
解决办法
181
查看次数

Python 3:使用pip安装gi包

我试图使用Python 3 运行这个 Matplotlib示例.要运行它,我需要先安装gi(我正在使用pyenv):

$ python --version
Python 3.6.1
$ pip --version
pip 9.0.1 from /home/hakon/.pyenv/versions/3.6.1/lib/python3.6/site-packages (python 3.6)
$ pip install gi
Collecting gi
  Downloading gi-1.2.tar.gz
Collecting requests (from gi)
  Downloading requests-2.16.0-py2.py3-none-any.whl (85kB)
    100% |????????????????????????????????| 92kB 959kB/s 
Collecting idna<2.6,>=2.5 (from requests->gi)
  Downloading idna-2.5-py2.py3-none-any.whl (55kB)
    100% |????????????????????????????????| 61kB 1.2MB/s 
Collecting chardet<3.1.0,>=3.0.2 (from requests->gi)
  Downloading chardet-3.0.3-py2.py3-none-any.whl (133kB)
    100% |????????????????????????????????| 143kB 1.8MB/s 
Collecting urllib3<1.22,>=1.21.1 (from requests->gi)
  Downloading urllib3-1.21.1-py2.py3-none-any.whl (131kB)
    100% |????????????????????????????????| 133kB 1.8MB/s 
Collecting certifi>=2017.4.17 (from requests->gi)
  Downloading …
Run Code Online (Sandbox Code Playgroud)

python pip matplotlib

9
推荐指数
3
解决办法
1万
查看次数

如何使用元对象协议向对象添加属性?

我试图回答这个问题,并认为我可以使用元对象协议向类添加属性.这是一个最小的例子,我尝试在构造后test向类中添加一个属性Configuration:

use v6;

class Configuration {
}

my $config = Configuration.new;
my $attr = Attribute.new(
    :name('$.test'), # Trying to add a "test" attribute
    :type(Str),
    :has_accessor(1), 
    :package(Configuration)
);
$config.^add_attribute( $attr );
$config.^compose();
say "Current attributes: ", join ', ', $config.^attributes();
$attr.set_value( $config, "Hello" ); # <-- This fails with no such attribute '$.test'
say $config.test;
Run Code Online (Sandbox Code Playgroud)

当我运行这个时,我得到:

Current attributes: $.test
P6opaque: no such attribute '$.test' on type Configuration in a Configuration when trying to bind a …
Run Code Online (Sandbox Code Playgroud)

class perl6 raku

9
推荐指数
1
解决办法
118
查看次数

如何从perl6中的匿名递归子返回?

这就是我所期待的.fib(13)返回233.

sub fib(Int $a --> Int) {
    return 0 if $a == 0;
    return 1 if $a == 1;

    return fib($a -1) + fib($a -2);
}

my $square = -> $x { $x * 2 };   # this works with no return value
my @list = <1 2 3 4 5 6 7 8 9>.map( $square );
# returns [2 4 6 8 10 12 14 16 18]
Run Code Online (Sandbox Code Playgroud)

我尝试使用匿名子实现fib()

my $fib = -> Int $x --> Int {
    return 0 …
Run Code Online (Sandbox Code Playgroud)

perl6 raku

9
推荐指数
2
解决办法
172
查看次数

Avoid creating temporary scalars when returning multiple arrays

Is it possible to avoid creating temporary scalars when returning multiple arrays from a function:

use v6;
sub func() {
    my @a = 1..3;
    my @b = 5..10;
    return @a, @b;
}
my ($x, $y) = func();
my @x := $x;  
my @y := $y;
say "x: ", @x;  # OUTPUT: x: [1 2 3]
say "y: ", @y;  # OUTPUT: y: [5 6 7 8 9 10]
Run Code Online (Sandbox Code Playgroud)

I would like to avoid creating the temporary variables $x and $y …

perl6 raku

9
推荐指数
1
解决办法
79
查看次数

在单个语句(Perl 5与Perl 6)中进行后增量和变量重用

我天真地想在Perl 6和Perl 5之间进行相当多的字面上的翻译,实际上却没有,因为后增量变量的处理方式不同。

Perl 6产生了理想的结果,即幻方:[[8,1,6],[3,5,7],[4,9,2]]

my @sq; my $n = 3; my $i = 1; my $x = $n/2; my $y = 0;
@sq[($i%$n ?? $y-- !! $y++) % $n][($i%$n ?? $x++ !! $x) % $n] = $i++ for 0..$n**2 - 1;
say join ' ', $_ for @sq;
Run Code Online (Sandbox Code Playgroud)

Perl 5中的“相同”代码不:[[8,1,3],[9,5,7],[4,6,2]]

$n = 3; $i = 1; $x = $n/2; $y = 0;
$sq[($i%$n ? $y-- : $y++) % $n][($i%$n ? $x++ : $x) % $n] = $i++ for 0..$n**2 …
Run Code Online (Sandbox Code Playgroud)

perl perl6 undefined-behavior sequence-points raku

9
推荐指数
2
解决办法
192
查看次数

在Perl6中声明多个范围的数组

我试图在Perl6中创建一个数组,该数组具有两个与Perl5中相同的范围:

my @cols = (3..9, 11..16);
use Data::Printer:from<Perl5>;
p @cols; exit;
Run Code Online (Sandbox Code Playgroud)

但是,这将创建一个二维数组

[
    [0] [
            [0] 3,
            [1] 4,
            [2] 5,
            [3] 6,
            [4] 7,
            [5] 8,
            [6] 9
        ],
    [1] [
            [0] 11,
            [1] 12,
            [2] 13,
            [3] 14,
            [4] 15,
            [5] 16
        ]
]
Run Code Online (Sandbox Code Playgroud)

应该是一维数组,就像(3..9, 11..16)在Perl5中那样:

[
    [0]  3,
    [1]  4,
    [2]  5,
    [3]  6,
    [4]  7,
    [5]  8,
    [6]  9,
    [7]  11,
    [8]  12,
    [9]  13,
    [10] 14,
    [11] 15,
    [12] 16,
    [13] 17,
    [14] …
Run Code Online (Sandbox Code Playgroud)

perl6 raku

9
推荐指数
1
解决办法
84
查看次数

如何为语法的动作类添加默认方法?

最近,我尝试了一些语法来修改文件的小部分.在那些测试用例中,我会保留大部分文件,只是在这里和那里修改小块,请参阅 评论问题的示例.

所以我需要一个(或几个)动作方法(即语法的动作类中的方法),我将使用其make方法将文件的修改部分附加到匹配对象.问题是语法本身会有更多令牌/规则/正则表达式与复杂的嵌套.因此,需要将(通过连续调用make())小变化(当前附加到令牌的匹配对象)传播 到TOP()动作类中的方法,使得文件中的所有其他(所有其他标记/规则/正则表达式)都是语法.parse()呼叫返回的结果保持不变.

因此,除了一个之外,action类中的所有方法都采用完全相同的形式:

method make-data ($match-data) {
    $match-data.make( [~] $match-data.chunks.map: {$_.value.?made // $_.value} );
}
Run Code Online (Sandbox Code Playgroud)

现在,对于所有动作方法,显式重复相同的代码在我看来非常冗长,并且还打破了DRY编程原则.

有没有办法告诉语法类,如果没有指定一个动作方法(对应于语法中的一个标记),它将默认为make-data上面的方法(或类似的方法)?

所以在这种情况下,我设想了DEFAULT()动作类中的一个方法:

method DEFAULT ($match-data) {
    $match-data.make( [~] $match-data.chunks.map: {$_.value.?made // $_.value} );
}
Run Code Online (Sandbox Code Playgroud)

如果语法类中的标记在动作类中没有相应的方法,则调用该方法.

grammar perl6 raku

8
推荐指数
1
解决办法
116
查看次数

Perl6 Terminal :: Print如何提示用户输入文本?

我正在将Perl6 Terminal :: Print模块用于基于控制台的应用程序。

它运行良好-但是,现在我需要提示用户输入文本字符串。

有什么好方法吗?

printing terminal tui perl6 raku

8
推荐指数
1
解决办法
123
查看次数