【问题标题】:Clojure function reading from stream locksClojure 函数从流锁中读取
【发布时间】:2010-11-06 21:07:15
【问题描述】:

我有一个函数,它从被调用的输入流中读取一个标记(get-next-indicator 流指示符)并返回它。我正在尝试使用它来构建地图。

但是,当我运行它时,它会锁定。如果我删除 get-next-indicator 函数之一,它确实有效。 这两个函数是否尝试同时读取流是这样的。是什么原因造成的?


(defn decode-map [ stream ]
  (loop [result {}]
    (let [c (char (.read stream))]
      (if (= c \e)
        result
        (recur (assoc result (get-next-indicator stream (int c))
                             (get-next-indicator stream (int c)) ))))))

【问题讨论】:

  • “锁定”是什么意思?有错误信息吗?
  • 没有错误信息 repl loop just locks 注意被打印。如果我用say“e”替换对get-next-indicator的第二次调用,它会按预期生成地图,但是当它像我的sn-p一样被调用时。什么都没发生。没有错误没有例外。
  • get-next-indicator 有副作用吗?也发布代码。

标签: function recursion clojure stream


【解决方案1】:

只是一个猜测,但如果流中没有可获取的内容,get-next-indicator 是否会阻塞?您在重复之前连续调用它两次(然后在此之后执行 .read 以检测结束)。如果在任一 get-next-indicator 调用期间流用完字节,则函数将挂起,等待流上足够的字节来完成这些调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    相关资源
    最近更新 更多