clojure - Why does my clojure REPL output gibberish?
问题描述
I'm prototyping some really basic functionality, but the REPL is outputting totally random stuff.
Just trying to write a function to update a value in a map gives this:
fwd.core=> (fn [step] (update {:x 10 :y 10} :x 20) 20)
#object[fwd.core$eval1593$fn__1594 0x3d2da438 "fwd.core$eval1593$fn__1594@3d2da438"]
解决方案
函数是 Clojure 中的一等公民。因此,您定义了一个匿名函数(请注意,这里接近无操作,因为您不坚持它-而且您在错误的地方也有一些括号(见下文))和 RE P我给你打印了。
所以这基本上是.toString()
JVM 看到的函数对象的表示。
边注:
为了更好地命名您的堆栈跟踪,您还可以命名 anon fns,例如:
user=> (fn [step] (update {:x 10 :y 10} :x 20) 20)
#<Fn@559d19c user/eval8096[fn]>
user=> (fn my-fancy-name [step] (update {:x 10 :y 10} :x 20) 20)
#<Fn@451816fd user/eval8105[my_fancy_name]>
编辑(该功能存在多个问题)
至于函数本身,内部更新也是无操作的,因为您也没有分配更新的结果。该函数现在总是返回20
。
要调用该函数,您必须修复括号:
user=> ((fn [step] (update {:x 10 :y 10} :x step)) 20)
Execution error (ClassCastException) at user/eval8105$fn (REPL:1).
java.lang.Long cannot be cast to clojure.lang.IFn
(该函数首先用于周围的括号,因此将调用它。
这现在给出了一个错误,因为update
需要一个函数 -assoc
改为使用:
user=> ((fn [step] (assoc {:x 10 :y 10} :x step)) 20)
{:x 20, :y 10}
推荐阅读
- python - 用字符串键和数值绘制 python 字典
- javascript - 当父级有点击事件时,引导下拉菜单不起作用
- firebase - 如何下载 Firebase Cloud Functions 的(原始)日志?
- c++ - 按字母顺序对对象数组进行排序c ++
- scala - What this error mean in high-order function? [Scala]
- sql - 多个特征组合导致要索引的数字的 Sql 问题
- node.js - 请求许可时的错误响应
- coq - coq 中有限映射的相等性(使用 map2 定义)
- c - 尝试使用用户输入时的 AddressSanitizer:DEADLYSIGNAL
- ios - 英国自动续订订阅者没有收据数据?