小编Ope*_*uce的帖子

封闭类与声明类

是否有任何情况Class.getDeclaringClass可能会产生不同的结果Class.getEnclosingClass

我认为它可能与外部类的子类实例化一个未声明为静态的内部类,但我无法通过这种方式获得差异:

public class Main {
  private static class StaticInnerClass {

  }

  private class MemberInnerClass {

  }

  private static class ChildClass extends Main {

  }

  public MemberInnerClass getMemberInnerClassInstance() {
    return new MemberInnerClass();
  }

  public static void main(String[] args) {
    System.out.println( StaticInnerClass.class.getDeclaringClass() );
    System.out.println( StaticInnerClass.class.getEnclosingClass() );
    System.out.println( MemberInnerClass.class.getDeclaringClass() );
    System.out.println( MemberInnerClass.class.getEnclosingClass() );
    System.out.println( new ChildClass().getMemberInnerClassInstance().getClass().getEnclosingClass() );
    System.out.println( new ChildClass().getMemberInnerClassInstance().getClass().getDeclaringClass() );
  }
}
Run Code Online (Sandbox Code Playgroud)

输出:

class Main
class Main
class Main
class Main
class Main
class Main
Run Code Online (Sandbox Code Playgroud)

java reflection

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

Clojure中的快速复数运算

我在Clojure中实现了一些基本的复数运算,并注意到它比大致相当的Java代码慢了大约10倍,即使是类型提示也是如此.

相比:

(defn plus [[^double x1 ^double y1] [^double x2 ^double y2]]
    [(+ x1 x2) (+ y1 y2)])

(defn times [[^double x1 ^double y1] [^double x2 ^double y2]]
    [(- (* x1 x2) (* y1 y2)) (+ (* x1 y2) (* y1 x2))])

(time (dorun (repeatedly 100000 #(plus [1 0] [0 1]))))
(time (dorun (repeatedly 100000 #(times [1 0] [0 1])))) 
Run Code Online (Sandbox Code Playgroud)

输出:

"Elapsed time: 69.429796 msecs"
"Elapsed time: 72.232479 msecs"
Run Code Online (Sandbox Code Playgroud)

有:

public static void main( String[] args ) {
  double[] z1 …
Run Code Online (Sandbox Code Playgroud)

optimization clojure numerical-computing type-hinting

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

可读地打印lisp功能

我正在尝试使用lisp,并在http://www.gigamonkeys.com/book/上阅读Practical Common Lisp一书.

是否有一个命令可以打印以前在REPL中定义的函数,以后可以读取?我试过了

(print #'function-name)
Run Code Online (Sandbox Code Playgroud)

但是这会导致封闭的输出#<>无法读回(如果*print-readably*设置为TI则只会出错).我使用princprin1取代相同的结果print.

我有点惊讶这不容易找到.我在lisp上阅读的一些内容鼓励在REPL中进行实验,但如果在输入后无法保存函数,则在进入它们之前,您必须将它们写在单独的文件中,这部分失败了关键点.

lisp common-lisp

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

最快/最简洁的bash one-liner,用于从文件中提取指定的行

我想从文件中提取具有特定行号的行(我有大约20-50行号,文件有30,000行).到目前为止,我发现最简洁的方法是:

gawk 'BEGIN {split("13193,15791,16891", A, ",")} NR in A' <file_name>
Run Code Online (Sandbox Code Playgroud)

但似乎我应该能够进一步减少所涉及的打字数量.我看了sed,但我想我需要一个-n-p每个行号,也想过cat -ngrep,但它比上述更详细.有谁知道更好的方法?

bash perl awk grep sed

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

在Clojure中使用defprotocol和defrecord的休息参数有什么问题?

下面在Clojure中使用defprotocol和defrecord的休息参数有什么问题?

(defprotocol prot
  (f [this] [this & rest]))

(defrecord rec []
  prot
  (f [this] "one arg")
  (f [this & rest] "more than one arg"))

(prn (f (rec.)))
; (prn (f (rec.) 5))
(prn (f (rec.) 5 6))
; (prn (f (rec.) 5 6 7))
Run Code Online (Sandbox Code Playgroud)

上面的代码打印出我期望的输出:

"one arg"
"more than one arg"
Run Code Online (Sandbox Code Playgroud)

但如果我取消注释任何一个注释行,我会得到以下异常:

Exception in thread "main" java.lang.IllegalArgumentException: No single method: f of interface: user.prot found for function: f of protocol: prot (bug.clj:10)
    at clojure.lang.Compiler.analyzeSeq(Compiler.java:5376)
    at clojure.lang.Compiler.analyze(Compiler.java:5190)
    at clojure.lang.Compiler.analyze(Compiler.java:5151)
    at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3057) …
Run Code Online (Sandbox Code Playgroud)

clojure variadic-functions

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