如何在 Leiningen REPL 中捕获 Ctrl-C?
set-break-handler!似乎没有效果:
user=> (set-break-handler! (fn [sig] (println "HERE!" sig)))
#object[clojure.repl.proxy$java.lang.Object$SignalHandler$d8c00ec7 0x22e61b57
"clojure.repl.proxy$java.lang.Object$SignalHandler$d8c00ec7@22e61b57"]
user=>
Run Code Online (Sandbox Code Playgroud)
第二个提示后的那个有趣的字符(如果你能看到它)是 ASCII 3。这就是我按下 Ctrl-C 时出现的内容。Leiningen 似乎已禁用 Ctrl-C 和 SIGINT 信号之间的连接。(这是在 Linux 系统上。)Leiningen 将 Ctrl-C 视为另一个字符。如果我按 Enter 键,Clojure 编译器会抱怨它无法解析该符号。(同样,您可能看不到它;Ctrl-C 在大多数字体中都不是可打印的字形。)
我想要这个的原因是这样我可以运行一些我正在试验的代码作为无限循环,看看它做了一段时间,停止它,改变它一点,再试一次,等等。我实际上不想通过set-break-handler直接调用 REPL 来设置 INT 处理程序。我想将它放入我的代码中,以便它清理其线程并终止其子进程。
顺便说一句,我想看看 Leiningen 是否弄乱了我的 stty 设置,所以我输入了以下内容:
user=> (clojure.java.shell/sh "stty" "-a")
{:exit 1, :out "", :err "stty: standard input: Invalid argument\n"}
Run Code Online (Sandbox Code Playgroud)
什么??stty -a如果我在 shell 提示符下输入它,效果很好。这是问题的线索吗?
如何在Clojure表达式中找到所有自由变量?
通过自由变量,我指的是在本地环境中未定义的所有符号(包括表达式中定义的所有本地环境),未在全局环境中定义(当前命名空间中的所有符号,包括从其他命名空间导入的所有符号) ,而不是Clojure原语.
例如,此表达式中有一个自由变量:
(fn [ub]
(* (rand-int ub) scaling-factor))
Run Code Online (Sandbox Code Playgroud)
那就是scaling-factor.fn,*和rand-int,都在全球环境中定义.ub在它发生的范围内定义,因此它也是一个绑定变量(即不是自由的).
我想我自己也可以写这个 - 它看起来并不太难 - 但我希望有一些标准的方法来做我应该使用的,或者是一种标准的方式来访问Clojure编译器来做到这一点(因为Clojure)编译器肯定也必须这样做).天真实现的一个潜在缺陷是表达式中的所有宏必须完全展开,因为宏可以引入新的自由变量.
在抽象类中,我如何指定方法的返回值与它所属的具体类具有相同的类型?
例如:
abstract class Genotype {
def makeRandom(): Genotype // must return subclass type
def mutate(): Genotype // must return subclass type
}
Run Code Online (Sandbox Code Playgroud)
我想说,每当你调用mutate()一个具体的Genotype类时,你肯定会回到同一个Genotype类的另一个实例.
我宁愿不以类型参数的方式使用类型参数Genotype[SpecificGenotype259],因为该类型参数可能会在整个代码中泛滥(同样,它是多余的和令人困惑的).我更喜欢通过扩展各种特征来定义具体的基因型类.
(def ^:dynamic *d* 1)
(binding [*d* 2]
(println *d*)
(repeatedly 1 #(println *d*)))
Run Code Online (Sandbox Code Playgroud)
输出:
2
1
Run Code Online (Sandbox Code Playgroud)
为什么?为什么里面的函数repeatedly从外面看到动态var的值binding?
顺便说一句,我检查(.getId (java.lang.Thread/currentThread))了匿名函数的内部和外部:它是一样的.
我正在绘制LogSumExp函数的曲面图。该函数与max类似,它接受一组数字并返回(大约)这些数字中的最大值;与 max不同, LogSumExp 函数是平滑的。为了理解 LogSumExp 函数,我希望看到它是用仅由两个数字组成的输入来绘制的,用作曲面图的 x 和 y 坐标。
下面的代码:
function lse = lse(x)
lse = log(sum(exp(x)));
end
fsurf(@(x,y) lse([x y]))
Run Code Online (Sandbox Code Playgroud)
绘制图表(成功!)但会产生以下警告消息:
Warning: Function behaves unexpectedly on array inputs. To improve
performance, properly vectorize your function to return an output with
the same size and shape as the input arguments.
> In matlab.graphics.function.FunctionSurface>getFunction
In matlab.graphics.function/FunctionSurface/updateFunction
In matlab.graphics.function/FunctionSurface/set.Function
In matlab.graphics.function.FunctionSurface
In fsurf>singleFsurf (line 267)
In fsurf>@(f)singleFsurf(cax,{f},extraOpts,args) (line 233)
In fsurf>vectorizeFsurf (line 233)
In fsurf (line 206) …Run Code Online (Sandbox Code Playgroud) 这是在Scala 11.5 .empty中的scala.collection.mutable.Map对象中声明方法的方法:
def empty[A, B]: Map[A, B]
Run Code Online (Sandbox Code Playgroud)
这个方法不应该有空括号,像这样吗?
def empty[A, B](): Map[A, B]
Run Code Online (Sandbox Code Playgroud)
关于Scala的命名约定的页面建议,在没有明确说明的情况下,省略0-arity方法的括号是纯函数代码的约定,并且包括空括号意味着该方法具有副作用.(我想我遇到了一个更明确的错误信息.)
可变.empty方法有副作用,因为您可以区分单独调用的结果.empty.它不应该得到空括号,即使它的配偶immutable.Map没有?
关于我自己的代码,在从0-arity方法创建和返回可变对象时,我应该遵循一个特殊的命名约定吗?
该cytoscape.js可乐文档的gapInequalities一个元素layout字典上说:
gapInequalities: undefined, // list of inequality constraints for the gap between the nodes,
// e.g. [{"axis":"y", "left":node1, "right":node2, "gap":25}]
Run Code Online (Sandbox Code Playgroud)
您如何设置,在值指定节点的对象left和right?
maxkfranz 在这里很有帮助地指出这些对象需要是集合对象。这些包含对指定节点(“元素”)的引用,并通过查询 cytoscape.js“核心对象”创建。我不清楚的是,鉴于layout对象需要引用节点元素,并且在layout告诉如何渲染它们之前不应将元素添加到图中,您如何正确设置这些集合对象?
例如,要指定 nodeb应该放在 node 上方a,下面的代码中是什么来代替???a???和???b????
cy = cytoscape({
elements: [
{ data: { id: 'a' } },
{ data: { id: 'b' } },
. . .
],
layout: {
name: …Run Code Online (Sandbox Code Playgroud) 下面代码中的类型细化似乎表明路径依赖类型vt.ValueT包括this.type:
trait ValueType {
type ValueT <: Value
type ConstrainedT <: ConstrainedValue
def makeConstrainedValue(v: ValueT): ConstrainedT = ???
}
trait Value {
type ValueTypeT <: ValueType { type ValueT >: this.type } // <--- HEY, COMPILER, READ THIS
val vt: ValueTypeT
def asConstrainedValue = vt.makeConstrainedValue(this) // <--- Compiler complains here
}
trait ConstrainedValue { /* details omitted */ }
Run Code Online (Sandbox Code Playgroud)
但Scala编译器(版本2.11.2)说:
error: type mismatch;
found : Value.this.type (with underlying type Test.Value)
required: Value.this.vt.ValueT
override def asConstrainedValue = …Run Code Online (Sandbox Code Playgroud) 正如http://docs.scala-lang.org/overviews/core/value-classes.html所承诺的那样,这有效:
class Wrapper(val self: Int) extends AnyVal {
def toHexString: String = java.lang.Integer.toHexString(self)
}
println(12.toHexString)
Run Code Online (Sandbox Code Playgroud)
但这不编译:
class Wrapper(val self: Int) extends AnyVal {
def whyNot: String = java.lang.Integer.toHexString(self)
}
println(12.whyNot)
Run Code Online (Sandbox Code Playgroud)
为什么不?我唯一改变的是方法的名称!
这是错误消息:
error: value whyNot is not a member of Int
println(12.whyNot)
^
Run Code Online (Sandbox Code Playgroud)
是的,我已经仔细检查了里面的schtupit Unicode字符whyNot.
scala ×4
clojure ×3
collections ×1
cytoscape.js ×1
leiningen ×1
macros ×1
matlab ×1
scope ×1
side-effects ×1
signals ×1
type-bounds ×1
value-class ×1
webcola ×1