小编raj*_*kar的帖子

仅在使用 repl 时编译错误

仅当在 repl 中逐行输入代码时,我才会收到错误。当整个程序一次粘贴或从命令行粘贴时,它会起作用。

class A {
    method a () {
    return 1;
    }
}

class B {
    method b () {
    return 2;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是错误声明:

===SORRY!=== Error while compiling:
Package 'B' already has a method 'b' (did you mean to declare a multi method?)
Run Code Online (Sandbox Code Playgroud)

这个屏幕截图可能会更清楚。在左边我只是粘贴了代码,在右边我一行一行地输入了它。该代码仍在工作,但导致错误的原因是什么?

粘贴 vs 逐行

出于某种原因,我无法仅使用一个类来重现这一点。

rakudo read-eval-print-loop raku

7
推荐指数
2
解决办法
141
查看次数

由于 EVALFILE 引入类符号

类名在 之后的范围内可用EVALFILE,而文档可能另有说明。

EVALFILE 状态的文档:

Slurps 指定的文件并对其进行评估。在Blob 解码、范围、$lang 参数和 $check 参数方面的行为与EVAL相同。当 $check 不为​​ True 时,计算文件中最后一条语句产生的值。

EVAL 状态的文档:

由于词法作用域中的符号集在编译后是不可变的,因此EVAL 永远不能将符号引入周围的作用域。

我的输入文件是:

class SomeClass {
    method a () { "In SomeClass method a"; };
}

sub some-routine () {
    "Some routine";
}
Run Code Online (Sandbox Code Playgroud)
> my $f = EVALFILE("/tmp/eval-bug.raku");
&some-routine

> $f()
Some routine

> some-routine()
===SORRY!=== Error while compiling:
Undeclared routine:
    some-routine used at line 1
Run Code Online (Sandbox Code Playgroud)

以上三个执行符合文档,但以下不符合:

> SomeClass.a()
In SomeClass method a
Run Code Online (Sandbox Code Playgroud)

那么符号仅表示例程名称而不是类名称吗?或者这是一个错误?

eval rakudo raku

7
推荐指数
2
解决办法
75
查看次数

从特定方法调用更通用的方法

我试图从特定的方法中调用通用方法,但不知道如何调用。

function fn(x)
  # generic
  ...
end

function fn(x :: String)
  # I want to call the generic version here
  val = fn(x)
  # do something with val and then return it
  ...
end
Run Code Online (Sandbox Code Playgroud)

这可能吗?

解决方法是使用可从通用方法和特定方法调用的辅助函数。例如

function helper(x)
  # main work is here
  ...
end

function fn(x)
  # generic
  helper(x)
end

function fn(x :: String)
  val = helper(x)
  # now use the val to do something
  ...
end
Run Code Online (Sandbox Code Playgroud)

如果不使用此类助手,有没有办法控制调度以选择要使用的特定方法?Julia 中是否有类似:before:after关键字以及call-next-method来自 lisp CLOS 的内容?

julia

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

这会是 SWI-Prolog 中优化的尾调用吗

step_n(0, I, I).
step_n(N, In, Out) :-
    N > 0, plus(N1, 1, N), phase_step(In, T),
    step_n(N1, T, Out).
Run Code Online (Sandbox Code Playgroud)

phase_step 是一个转换数据的函数。

step_n会在几乎相同的内存中运行phase_step吗?如果没有,我应该如何重写它来做到这一点?这将取决于具有单一解决方案的 phase_step 吗?

编辑:使用 进行一些调试后prolog_current_frame,我发现如果phase_step是一个简单的函数,例如Out is In + 1,那么优化会发生,但不会发生在我的用例中

为什么 TCO 依赖于phase_step谓词?

prolog tail-call-optimization

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

如何使用 xargs 获取运行进程的运行时间

我想获取某些进程的运行时间。这就是我正在做的事情

ps -ef | grep "python3 myTask.py" | awk '{print $2}' | xargs -n1 ps -p {} -o etime
Run Code Online (Sandbox Code Playgroud)

我想通过以下方式获取pid

ps -ef | grep "python3 myTask.py" | awk '{print $2}'
Run Code Online (Sandbox Code Playgroud)

然后将这些传递给

ps -p {} -o etime
Run Code Online (Sandbox Code Playgroud)

通过使用 xargs,但它不起作用。我明白了

error: process ID list syntax error

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).
error: process ID list syntax error

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help …
Run Code Online (Sandbox Code Playgroud)

linux command-line xargs

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