How use a function from algo.generic - clojure

I'm trying to use from the Repl the algo.generic pow multifunction, in order to learn
clojure.
How can I use algo.generic from the Repl?
-I'm using lein repl.
-added [org.clojure/algo.generic "0.1.1"] at project.clj
-lein deps
=> (use 'clojure.algo.generic)
=> (pow 5 6)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: pow in this context, compiling:(/tmp/form-init6907113967558511638.clj:1:1)`
Thanks..

The pow function is in the clojure.algo.generic.math-functions namespace, so you will need to "use" or "require" that namespace instead of the clojure.algo.generic

Related

Spyscope Java RuntimeException No reader function for tag spy/p

I'm trying out spyscope, and following the documented example, I'm getting:
user=> (take 20 (repeat #spy/d (+ 1 2 3)))
RuntimeException No reader function for tag spy/d clojure.lang.LispReader$CtorReader.readTagged (LispReader.java:1245)
RuntimeException Unmatched delimiter: ) clojure.lang.Util.runtimeException (Util.java:221)
RuntimeException Unmatched delimiter: ) clojure.lang.Util.runtimeException (Util.java:221)
My ~/.lein/profile.clj is:
{:user {:dependencies [[spyscope "0.1.6"]]
:injections [(require 'spyscope.core)
(use 'clojure.tools.trace)]
:plugins [[lein-try "0.4.3"]]
}
}
My version of Leiningen is:
$ lein --version
Leiningen 2.8.1 on Java 1.8.0_212 OpenJDK 64-Bit Server VM
I can't seem to find any answers on how to resolve this.
Answering my own question to make it easier for the next person to looking for the solution.
The answer can be traced to [this issue]: (https://github.com/dgrnbrg/spyscope/issues/8)
Lein 2 allows users to fire lein repl in a non project directory and that's the case
You have to use spyscope within a directory with a project.clj file.
You can use spyscope this way at your profiles.clj
; try/catch to workaround an issue where `lein repl` outside a project dir
; will not load reader literal definitions correctly:
(try (require 'spyscope.core)
(catch RuntimeException e))

Can not require third-party library in lein repl

clojure version: 1.8.0
leiningen version: 2.7.1
jdk version: 1.8.0_101
When I require a library, say reagent(has added in the project dependencies), in the lein repl:
user=> (require '[reagent.core :as r])
nil
The output is nil, which I think it means success. But when I use the r,say (r/atom 3), the repl throw an error says:
CompilerException java.lang.RuntimeException: Unable to resolve symbol: r in
this context, compiling: (/private/var/folders/_y/n3ym4ftj2ld9tl471g_kcv_00000gn/T
/form-init1002540725014588615.clj:1:4923)
That seems strange.
Anyone has some idea?
By the way, I can require java and clojure library.
Probably you are trying to use ClojureScript library: reagent within the Clojure REPL. Finding the cause to error is not that useful since Clojure and ClojureScript are suppose to run in very different environments, JVM and JavaScript respectively. But same attempt will succeed in ClojureScript context.
As a test, create a ClojureScript project based on figwheel template.
lein new figwheel rtest
cd rtest
Add the regent dependency in project.clj
:dependencies [[org.clojure/clojure "1.8.0"]
[reagent "0.6.0"] ; only add this line
Run the project
rlwrap lein figwheel
You will be connected to the browser repl automatically. Otherwise browse to http://localhost:3449/index.html to get connected.
Now you can retry what you did.
cljs.user=> (require '[reagent.core :as r])
nil
cljs.user=> (type r/render-component)
#object[Function "function Function() { [native code] }"]

Missing doc in non-user namespaces in Clojure 1.5

What should I do to get doc available again?
brew install --devel leiningen
lein new app yeehaw
cd yeehaw
; edit project.clj and replace "1.4.0" with 1.5.0-RC1"
lein repl
yeehaw.core=> (doc map)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: doc
in this context, compiling:(NO_SOURCE_PATH:1:1)
This change comes from CLJ-1085 and this associated source.
Note that doc and the usual REPL functionality is still available in the user namespace, but it would be nice to have it at my fingertips in my app's namespace, at least while in the REPL.
Get doc into current namespace:
(clojure.core/use '[clojure.repl :only (doc)])
And after that try doc again.
Thanks to help from clojure IRC, here are some options:
use clojure.repl/doc instead of doc
use (apply require clojure.main/repl-requires) to bring doc and other REPL functionality into a new namespace.

clojure namespaces .core file and repl

A beginners question.
running clojure using lein + emacs + nrepl.
I am slightly confused about the following:
I wish to use the exponent function. This function lives in the following place clojure.math.numeric-tower. I add [org.clojure/math.numeric-tower "0.0.1"] to the dependencies and run lein deps.
Now is it possible (I'm sure it is possible) to add this to my .core ns as follows:
(ns learning.core
(:require [clojure.math.numeric-tower :as math]))
(def i-know-the-answer
(math/expt 2 10))
now when I try to load (ctl-x e) this into the REPL, it throws errors.
clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: No such namespace: math, compiling:(NO_SOURCE_PATH:2)
do the dependencies need to be loaded into the REPL directly? Can I not just change the source file / recompile it and use that?
Load the file with ctrl-c ctrl-l then Switch your repl to the namespace in that file with either
(in-ns 'learning.core)
Or hit ctrl-c alt-n from the Clojure buffer to switch the repl to the buffer's namespace. You can tell if it worked by looking at the prompt in the repl.

Importing clojure.contrib.generic.math-functions

I download the clojure 1.2 and clojure-contrib-1.2.0.jar from the download site.
And I found the info about the math functions.
As is shown in the example, I tried to run the code.
(ns your-namespace
(:require clojure.contrib.generic.math-functions))
(println (abs 10))
But, I got the following error, when I run as follows.
CLOJURE_JAR=/Users/smcho/bin/jar/clojure.jar:/Users/smcho/bin/jar/clojure-contrib-1.2.0.jar
java -cp $CLOJURE_JAR:$CLASSPATH clojure.main SOURCE.CLJ
Exception in thread "main" java.lang.Exception: Unable to resolve symbol: abs in this context (hello.clj:4)
at clojure.lang.Compiler.analyze(Compiler.java:5205)
...
at clojure.main.main(main.java:37)
Caused by: java.lang.Exception: Unable to resolve symbol: abs in this context
at clojure.lang.Compiler.resolveIn(Compiler.java:5677)
at clojure.lang.Compiler.resolve(Compiler.java:5621)
at clojure.lang.Compiler.analyzeSymbol(Compiler.java:5584)
at clojure.lang.Compiler.analyze(Compiler.java:5172)
... 25 more
What might be wrong?
Try :use instead of :require
(ns your-namespace
(:use clojure.contrib.generic.math-functions))
(println (abs 10))
10
nil
Require makes the symbol (abs in this case) available, but you'd have to fully qualify it. Use imports the symbol into "your-namespace":
(ns your-namespace2
(:require clojure.contrib.generic.math-functions))
(println (clojure.contrib.generic.math-functions/abs 10))
10
nil