小编Dig*_*rat的帖子

如何遍历 Raku 哈希中的数组?

这似乎是一个简单的问题,但 Perl6/Raku 的行为并不像我预期的那样。我正在尝试在散列中创建对数组的引用,但没有得到预期的行为。在 Perl5 中,答案将涉及通过引用访问数组,但我没有看到 Perl6/Raku 的等效语法。

my $jsonstr = q:to/END/;
{
    "arr" : [
        "alpha","beta","delta","gamma"
    ]
}
END

my %json = from-json $jsonstr;
my @arr = %json{'arr'};
say "Arr length is " ~ @arr.elems; # Expect 4, get 1
say "Orig length is " ~ %json{'arr'}.elems; # Get expected value of 4
say "Arr[0] is " ~@arr[0].^name ~ " of length " ~ @arr[0].elems; # First index is array

say %json{'arr'}[0]; # Indexing into array in original location works …
Run Code Online (Sandbox Code Playgroud)

arrays hash json raku

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

如何在Perl6 NativeCall结构中定义固定长度的字符串?

我有一个第三方C库,它定义了一个类似于的结构:

struct myStruct {
  int a;
  int b;
  char str1[32];
  char str2[32];
};
Run Code Online (Sandbox Code Playgroud)

还有一个函数,它接受一个指向这个结构的指针并填充它.我需要我的Perl6本机调用来提供该结构,然后读取结果.

到目前为止,我已经在Perl6中定义了结构:

class myStruct is repr('CStruct') {
  has int32 $.a;
  has int32 $.b;
  has Str $.str1; # Option A: This won't work as Perl won't know what length to allocate
  has CArray[uint8] $.str2; # Option B: This makes more sense, but again how to define length?  
                     # Also, would this allocate the array in place, or 
                     #    reference an array that is separately allocated (and therefore not …
Run Code Online (Sandbox Code Playgroud)

perl6 nativecall

7
推荐指数
1
解决办法
254
查看次数

为什么 'git rev-list --left-right' 比较 A..B 与 B..A 不一致?

我正在尝试使用/sf/answers/1955801921/的答案来比较两个提交以获取其前方/后方差异

“git rev-list --left-right --count A..​​B”,其中 A 和 B 分别是 SHA1 提交引用。

在一种情况下,A..B 的比较给出 0 Ahead 和 17 Behind。如果我对 B..A 重新运行相同的命令,那么我会得到 0 Ahead 和 0 Behind,而我希望它是 17 Ahead 和 0 Behind 的简单反转。在另一种情况下,我在一个方向上得到 0/6,在另一个方向上得到 0/2。这违背了我试图更好地可视化差异的目的,在这种情况下是子模块引用与签出的提交。

为什么 A..B 得到的结果与 B..A 得到的结果完全不同?我需要/期望获得一致的值,无论顺序如何,只需交换前面与后面的计数...

有没有更好的方法来持续获取这些信息?目前,我看到的唯一解决方案是在脚本中运行命令两次(每个方向一次)并显示较大的值。

git

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

在不实例化的情况下检查类是否是类的子类

假设我在 JavaScript 模块中有以下内容:

class A { ... }
class B extends A { ... }
class C extends B { ... }
Run Code Online (Sandbox Code Playgroud)

如何在不创建 C 实例的情况下确认 C 是 A 的后代?

register(B);
register(C);

register(obj) {
  // I need this test, but without the new call
  if (!new obj() instanceof A) {
     throw Error "Not a child";
  }
  ...
}
Run Code Online (Sandbox Code Playgroud)

背景:我正在尝试创建一个模块化工厂方法,该方法可以自动实例化正确子类型的对象,而无需对列表进行硬编码。我正在使用从定义子类的模块文件中调用的“静态寄存器(obj)”方法来执行此操作。这允许根应用程序导入任何所需的库,而模块本身不知道可用的库。作为额外的完整性检查,我想在将它添加到列表之前确保该类实际上是一个后代。

javascript ecmascript-6 es6-class

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

标签 统计

arrays ×1

ecmascript-6 ×1

es6-class ×1

git ×1

hash ×1

javascript ×1

json ×1

nativecall ×1

perl6 ×1

raku ×1