How to use libraries in Clojure in LightTable - clojure

I have the following Clojure code in LightTable
(ns exercise
(:require [clojure.string :as str]))
(defn -main
[& args]
(def str1 "Hello world")
(str/includes? str1 "world")
)
When i try to run it i get the following exception:
clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: No such var: str/includes?
Am i importing the library wrong or is something else wrong ?

You're probably using an old Clojure version. includes? was added in 1.8.

Related

Is there a bug with foreign-libs in the cljsbuild repl?

When I:
clone the cljs-webgl project,
Compile it with:
lein cljsbuild once
Start the repl with
lein trampoline cljsbuild repl-listen
Paste the following into the REPL
`
(ns learningwebgl.lesson-06
(:require
[WebGLUtils]
[mat4]
[learningwebgl.common :refer [init-gl init-shaders get-perspective-matrix
get-position-matrix deg->rad animate load-image]]
[cljs-webgl.buffers :refer [create-buffer clear-color-buffer clear-depth-buffer draw!]]
[cljs-webgl.shaders :refer [get-attrib-location]]
[cljs-webgl.constants.buffer-object :as buffer-object]
[cljs-webgl.constants.capability :as capability]
[cljs-webgl.constants.draw-mode :as draw-mode]
[cljs-webgl.constants.data-type :as data-type]
[cljs-webgl.constants.texture-parameter-name :as texture-parameter-name]
[cljs-webgl.constants.texture-filter :as texture-filter]
[cljs-webgl.constants.webgl :as webgl]
[cljs-webgl.texture :refer [create-texture]]
[cljs-webgl.typed-arrays :as ta]))`
I get the following:
WARNING: No such namespace: WebGLUtils at line 1 <cljs repl>
(even though it is defined in the project.clj as:
:foreign-libs [
{:file "resources/js/gl-matrix-min.js" :provides ["mat4","mat3","vec3"]}
{:file "resources/js/webgl-utils.js" :provides ["WebGLUtils"]}]}
My question is: Is there a bug with foreign-libs in the cljsbuild repl?
:foreign-libs support in ClojureScript REPLs is quite new, and cljs-webgl is using a fairly old version of the compiler without the support.

Clojure CLR with multiple namespaces

I wrote a small namespace to do some database operations and I would like to use it from within another namespace. Normally having the files in the same directory and then doing
(ns program (:require [other-ns :as other]) (:gen-class))
would be all that's necessary. However this doesn't work in Clojure CLR, compiler complains about not knowing about other-ns. So what is the proper method of doing this? Having a seperate assembly for every namespace?
[EDIT] Another example
another.clj
(ns another)
(defn hello [name] (str "Hello " name))
program.clj
(ns program
(:require [another :as a])
(:gen-class))
I load up program.clj in the repl and get this message:
FileNotFoundException Could not locate another.clj.dll or another.clj on load path. clojure.lang.RT.load (d:\work\clojure-clr\Clojure\Clojure\Lib\RT.cs:3068)
I created two files in the same directory filea.clj and fileb.clj. Here's filea.clj:
(ns filea)
(defn hi []
(println "hi from filea"))
Here's fileb.clj:
(ns fileb
(require [filea :as a])
(:gen-class))
(defn -main []
(println "hi from fileb")
(a/hi))
I then changed into the directory where these files live and ran:
C:\temp\multi-ns>clojure.compile fileb
Compiling fileb to . -- 59 milliseconds.
And when I ran it I saw:
C:\temp\multi-ns>c:\Tools\clojure-clr-1.3.0-Debug-4.0\fileb.exe
hi from fileb
hi from filea
Are you using vsClojure or writing your code outside of VS?

Problem using redis-clojure with Leiningen

Hey, I'm new to Clojure and Leiningen and a bit stuck. I've managed to setup a project with Leiningen. I'm able to compile it into an uberjar and run the repl. I've also managed to load a dependency named aleph to run a simple concurrent webserver.
The next step for me is to use redis-clojure to access redis. But here I'm stuck. This is my project.clj:
(defproject alpha "0.0.1-SNAPSHOT"
:description "Just an alpha test script"
:main alpha.core
:dependencies [[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]
[aleph, "0.1.2-SNAPSHOT"]
[redis-clojure "1.2.4"]])
And here is my core.clj: Note that I only added the line (:requre redis) according to the example from redis-clojure.
(ns alpha.core
(:require redis)
(:gen-class))
(use `aleph.core 'aleph.http)
(defn alpha [channel request]
(let [] (enqueue-and-close channel
{:status 200
:header {"Content-Type" "text/html"}
:body "Hello Clojure World!"}))
(println (str request)))
(defn -main [& args]
(start-http-server alpha {:port 9292}))
When I try to run lein repl this happens:
java.io.FileNotFoundException: Could not locate redis__init.class or redis.clj on classpath: (core.clj:1)
Yes, I have run lein deps and the redis-clojure jar is available in my lib directory. I'm probably missing something trivial, but I've been at this issue for a few hours now and not getting any closer to a solution. Thanks!
Namespace redis does not exist. I suppose you need
(:require [redis.core :as redis])
A way to check for available namespaces:
(use 'clojure.contrib.find-namespaces)
(filter #(.startsWith (str %) "redis") (find-namespaces-on-classpath))
This works with more current versions of Clojure, in this example it finds the names of all namespaces that contains the sub string "jdbc":
(map str
(filter
#(> (.indexOf (str %) "jdbc") -1)
(all-ns)))
The result is a sequence, in example:
=>
("clojure.java.jdbc")

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

Compiling Clojure?

I'm feeling slightly silly here, but I can't get Clojure Hello World to compile.
Directory structure:
hello-world/
clojure-1.1.0.jar
build/
classes/
src/
test/
hello.clj
hello.clj:
(ns test.hello
(:gen-class))
(defn -main [& args]
(println "Hello" (nth args 0)))
Interaction:
$ cd hello-world
[hello-world]$ java -cp ./clojure-1.1.0.jar:./build/classes:./src clojure.main
Clojure 1.1.0
user=> (require 'test.hello)
nil
user=> (test.hello/-main "there")
Hello there
nil
user=> (compile 'test.hello)
java.io.IOException: No such file or directory (hello.clj:2)
user=> *compile-path*
"classes"
user=> (doseq [p (.split (System/getProperty "java.class.path") ":")] (println p))
./clojure-1.1.0.jar
./build/classes
./src
nil
So I can load and call the file from the REPL, but it doesn't compile.
According to clojure.org, compilation needs
namespace must match classpath-relative file path - check
*compile-path* must be on the classpath - check
:gen-class argument to the ns form - check
I found this post from a year back, as far as I can tell I'm doing exactly the same, but it doesn't work.
What am I missing?
System: OS X 10.6, Java 1.6.0, Clojure 1.1
Got it, there's a fourth requirement:
*compile-path* is resolved relative to the JVMs working directory, normally the directory where java is started. Or by REPL: (System/getProperty "user.dir"),
So this works:
user=> (set! *compile-path* "build/classes")
"build/classes"
user=> (compile 'test.hello)
test.hello
Why you don't use Leiningen? It's much easier to use it, than compile code manually. You can use my article about it as introduction...
To run clojure file
clojure filename.clj