在 slime REPL 中使用 clojure.contrib 函数 [英] Using clojure.contrib functions in slime REPL

查看:21
本文介绍了在 slime REPL 中使用 clojure.contrib 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 REPL 的 slime 中使用 clojure.contrib.trace 命名空间中的函数.我怎样才能让史莱姆自动加载它们?一个相关的问题,如何将特定的命名空间添加到正在运行的 repl 中?

在 clojure.contrib API 上,它描述了这样的用法:

(ns my-namespace(:需要clojure.contrib.trace))

但是将它添加到我的代码中会导致文件无法加载,并且对于跟踪命名空间中的任何函数都出现无法解析符号"错误.

我使用 leiningen 'lein swank' 来启动 ServerSocket 并且 project.clj 文件看起来像这样

 (defproject test-project "0.1.0":description "用 Clojure 编写的 Connect 4 Agent":dependencies [[org.clojure/clojure "1.2.0-master-SNAPSHOT"][org.clojure/clojure-contrib "1.2.0-SNAPSHOT"]]:dev-dependencies [[leiningen/lein-swank "1.2.0-SNAPSHOT"][swank-clojure1.2.0"]])

一切似乎都是最新的,即lein deps"不会产生任何变化.怎么了?

解决方案

  1. 您收到无法解析符号"异常,因为 :require 不会从给定的命名空间中提取任何变量,它只会使命名空间本身可用.

    因此,如果您在 ns 表单中使用 (:require foo.bar),则必须编写 foo.bar/quux 才能访问命名空间 foo.bar 中的 Var quux.您还可以使用 (:require [foo.bar :as fb]) 将其缩短为 fb/quux.最后一种可能是编写 (:use foo.bar) ;这使得 foo.bar 中的所有变量都可以在您的命名空间中使用.请注意,:use 外部库通常被认为是不好的风格;不过,在单个项目中可能没问题.

  2. Re:自动在 REPL 中提供可用的东西:

    ns 形式的 :require:use:refer 子句在 中有对应的子句clojure.core 中的 >requireuserefer 函数.还有:refer-clojure:import对应的宏.

    这意味着为了使 clojure.contrib.trace 在 REPL 中可用,您可以执行诸如 (require 'clojure.contrib.trace) 之类的操作>(require '[clojure.contrib.trace :as trace]).请注意,因为 require 是一个函数,所以您需要引用库规范.(userefer 也采用引用的 lib 规范;importrefer-clojure 不需要引用.)

    每次启动 Clojure REPL 时(包括使用 SLIME 执行时)使某些命名空间可用的最简单方法是将适当的 require 调用放在 ~/.clojure/用户.clj.请参阅 John 的要求所有可能的命名空间 博文劳伦斯·阿斯普登 (Lawrence Aspden) 描述了您可能在 user.clj 中放入什么以获取 all 的贡献(我个人不会这样做,尽管我确实有一个 <代码>(在那里使用'clojure.contrib.repl-utils)).

I want to use the functions in the clojure.contrib.trace namespace in slime at the REPL. How can I get slime to load them automatically? A related question, how can I add a specific namespace into a running repl?

On the clojure.contrib API it describes usage like this:

(ns my-namespace
  (:require clojure.contrib.trace))

But adding this to my code results in the file being unable to load with an "Unable to resolve symbol" error for any function from the trace namespace.

I use leiningen 'lein swank' to start the ServerSocket and the project.clj file looks like this

 (defproject test-project "0.1.0"
   :description "Connect 4 Agent written in Clojure"
   :dependencies [[org.clojure/clojure "1.2.0-master-SNAPSHOT"]
                  [org.clojure/clojure-contrib "1.2.0-SNAPSHOT"]]
   :dev-dependencies [[leiningen/lein-swank "1.2.0-SNAPSHOT"]
                      [swank-clojure "1.2.0"]])

Everything seems up to date, i.e. 'lein deps' doesn't produce any changes. So what's up?

解决方案

  1. You're getting "Unable to resolve symbol" exceptions because :require doesn't pull in any Vars from the given namespace, it only makes the namespace itself available.

    Thus if you (:require foo.bar) in your ns form, you have to write foo.bar/quux to access the Var quux from the namespace foo.bar. You can also use (:require [foo.bar :as fb]) to be able to shorten that to fb/quux. A final possiblity is to write (:use foo.bar) instead; that makes all the Vars from foo.bar available in your namespace. Note that it is generally considered bad style to :use external libraries; it's probably ok within a single project, though.

  2. Re: automatically making stuff available at the REPL:

    The :require, :use and :refer clauses of ns forms have counterparts in the require, use and refer functions in clojure.core. There are also macros corresponding to :refer-clojure and :import.

    That means that in order to make clojure.contrib.trace available at the REPL you can do something like (require 'clojure.contrib.trace) or (require '[clojure.contrib.trace :as trace]). Note that because require is a function, you need to quote the library spec. (use and refer also take quoted lib specs; import and refer-clojure require no quoting.)

    The simplest way to have certain namespaces available every time you launch a Clojure REPL (including when you do it with SLIME) is to put the appropriate require calls in ~/.clojure/user.clj. See the Requiring all possible namespaces blog post by John Lawrence Aspden for a description of what you might put in user.clj to pull in all of contrib (something I don't do, personally, though I do have a (use 'clojure.contrib.repl-utils) in there).

这篇关于在 slime REPL 中使用 clojure.contrib 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆