clojure - 如何在Clojure中使用expt函数?

标签 clojure dependencies leiningen read-eval-print-loop

我正在尝试使用 expt功能根据 this answer但是当我尝试做 (use 'clojure.math.numeric-tower)在 REPL 我收到错误

user> (use 'clojure.math.numeric-tower)
(use 'clojure.math.numeric-tower)FileNotFoundException Could not locate clojure/math/numeric_tower__init.class or clojure/math/numeric_tower.clj on classpath:   clojure.lang.RT.load (RT.java:443)

我想我需要按照解释 here 放入 Leiningen 依赖信息
[org.clojure/math.numeric-tower "0.0.2"]

在我的 project.clj ,我这样做了,但我仍然遇到同样的错误。我究竟做错了什么?

编辑

As in this answer我去了我的项目目录并做了 lein deps
a@b:~/command-line-args$ lein deps
Retrieving org/clojure/math.numeric-tower/0.0.2/math.numeric-tower-0.0.2.pom from central
Retrieving org/clojure/math.numeric-tower/0.0.2/math.numeric-tower-0.0.2.jar from central
a@b:~/command-line-args$ 

但我在 REPL 中仍然遇到同样的错误。

编辑 2

根据 Vidya 的回答,我正在尝试使用 Pomegranate 但没有成功。这是我尝试过的。我究竟做错了什么:
user> (use '[cemerick.pomegranate :only (add-dependencies)])
nil
user> (add-dependencies :coordinate '[[org.clojure/math.numeric-tower "0.0.2"]]
                        :repositories (merge cemerick.pomegranate.aether/maven-central
                                             {"clojars" "http://clojars.org/repo"}))
{}
user> (require '(numeric-tower core stats charts))
FileNotFoundException Could not locate numeric_tower/core__init.class or numeric_tower/core.clj on classpath:   clojure.lang.RT.load (RT.java:443)
user> (require 'clojure.contrib.math)
FileNotFoundException Could not locate clojure/contrib/math__init.class or clojure/contrib/math.clj on classpath:   clojure.lang.RT.load (RT.java:443)
user> 

最佳答案

这是一个正确配置的项目的示例,用于比较:

项目.clj:

(defproject math-example "0.1.0-SNAPSHOT"                            
  :description "FIXME: write description"                            
  :url "http://example.com/FIXME"                                    
  :license {:name "Eclipse Public License"                           
            :url "http://www.eclipse.org/legal/epl-v10.html"}        
  :dependencies [[org.clojure/clojure "1.5.1"]                       
                 [org.clojure/math.numeric-tower "0.0.2"]]) 

src/math_example/core.clj:
(ns math-example.core                                                
  (:require [clojure.math.numeric-tower :as math]))                  

(def x (math/expt 2 10)) 

回复:
math-example.core> (math/expt 2 10)                                  
1024                                                                 
math-example.core> x                                                 
1024                                                                 
math-example.core> 

通常,使用大多数 clojure 库应该不会比添加依赖项和添加 :require 更难。标记到命名空间(如果您愿意,也可以使用 :use 标记)。

关于clojure - 如何在Clojure中使用expt函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19870495/

相关文章:

Clojure:在特定命名空间中启动 repl

clojure - 数据查询和懒惰

linux - 最佳实践 : deploying depencencies on Linux

clojure - 如何在重新构建模板中运行后端服务器/代码?

clojure - 在 Clojure 中测试 "self-evaluating"原子的单个谓词

syntax - 在 Clojure 中应用函数列表中的第一个

java - Hadoop maven 依赖错误 - 找不到 hadoop 类

java - 清理 Maven 依赖管理

java - 在 Leiningen 项目中使用 Emacs 中的 Java

clojure - 安装 Leiningen 以与 Clojure 一起使用