Error in #'namespace/name_of_function - clojure

I am doing a project in Clojure. When I run the program I get an error:
#'targil4_2_new.core/writeAndNext
Where targil4_2_new is the name of my namespace and writeAndNext is the name of a function in this namespace.
How do I know what the problem is?

How are you calling your function?
Could you show me your trace error?
In your example you should call your function in this way:
1 - require function namespace:
In REPL:
(require '[targil4_2_new.core :as xpto])
Or in your application:
(ns my-app.old-core
(:require [targil4_2_new.core :as xpto]))
2 - call your function:
(xpto/writeAndNext ) //you can add your arguments

Related

Injecting namespaces in functions in clojure

Is it possible to inject namespaces in a function in Clojure?
I want my i/o to be outside from the program and only inject it. The problem i faced, that i tried to give a namespace and invoke it i get the error message:
No name namespace: my
(ns mymain
(:require [myio]))
...
(defn my-test [my]
(my/showworld))
;(play_game)
(my-test myio)
The usual way to pass logic is through functions and not by namespace aliases:
(defn my-test [show-world]
(show-world))
(require 'myio)
(my-test myio/show-world)

How can I use clj-http in riemann.config

I use riemann and now I write my riemann.config.
I want to use clj-http post all events from riemann stream to my web server. But I don't know how to import clj-http from riemann.jar.
I code (:use clj-http.client) or (:require [clj-http.client :as client]) in riemann.config but got error:
java.lang.ClassNotFoundException: clj-http.client
Could anyone help me ?
I did something similar few months ago and this was working for me. I was using http-kit :
(require '[org.httpkit.client :as http])
Since both http-kit and cli-http are available in riemann ( see https://github.com/aphyr/riemann/blob/master/project.clj ) You should be able to require cli-http the same way :
(require '[clj-http.client :as client])
Problem in your configuration is that you are using (:use ... an (:require .... which is supposed to be used within the namespace declaration. Since riemann.config doesn't contain namespace declaration you can't use these forms. When calling
(:use clj-http.client)
you get ClassNotFoundException because clojure is trying to invoke function :use on clj-http.client , which can't be found. Outside the namespace declaration :use is just a standard keyword with no special meaning.

How to require namespace inside function -main?

Suppose that in Leiningen project yo, I have these files:
foo.clj:
(ns yo.foo)
(def x 234.5)
bar.clj:
(ns yo.bar)
(def x -15)
And I have a main file (core.clj):
(ns yo.core)
(require '[yo.foo :as f])
(when (= f/x 234.5) (println "Succesfully required foo."))
(defn -main [& args]
(println (require '[yo.bar :as b]))
;(when (= b/x -15) (println "Succesfully required bar."))
)
When I enter "lein run" on the command line, I get this output:
Succesfully required foo.
nil
The first line tells me that I understand how to use the require function at the top level of a file. (Normally I would use :require in the ns statement.) The second line seems to indicate that I successfully required yo.bar.
However, when I uncomment the line containing when in -main, I get an exception:
java.lang.RuntimeException: No such namespace: b, compiling:(yo/core.clj:6:9).
Is there a way to perform a require from inside a function? My goal is to pass the name of a namespace on the command line, and have that namespace loaded as a result. Is there another way to do this? (I already know how to access command line arguments from within -main.)
(The problem is not that I wrapped the require call with println. I get the same exception if the first line of -main says only (require '[yo.bar :as b]).)
(The title of this question makes it seem as if it's the same as mine, but the question and the answer don't address the problem of requiring a namespace from inside a function.)
The require statement within the function -main is not invoked during compilation. Thus the compiler can't resolve the namespace b and complains.

ClassNotFoundException on use of another ns

As simple as this question is, I can't seem to find the right way for different namespaces in the same directory to validly refer to one another. I have two files:
project_root/src/babbler/core.clj:
(ns babbler.core
(:gen-class)
(use '[clojure.string :only (join split)]))
(defn foo [] "Foo")
and then project_root/src/babbler/bar.clj:
(ns babbler.bar)
(use [babbler.core :as babble])
This file also contains a main method, which is specified in my project.clj via :main babbler.bar
My entire structure is that generated by counterclockwise, with with default leiningen template.
The result of running lein repl is this:
Exception in thread "main" java.lang.ClassNotFoundException: babbler.core, compiling:(babbler/bar.clj:3:1)
at clojure.lang.Compiler.analyze(Compiler.java:6380)
at clojure.lang.Compiler.analyze(Compiler.java:6322)
at clojure.lang.Compiler$VectorExpr.parse(Compiler.java:3024)
at clojure.lang.Compiler.analyze(Compiler.java:6363)
at clojure.lang.Compiler.analyze(Compiler.java:6322)
(...)
Your use should be inside the definition of the namespace:
(ns babbler.bar
(use [babbler.core :as babble]))
In fact use is discouraged, you may want to write it as:
(ns babbler.bar
(:require [babbler.core :as babble :refer [foo]]))
That way you can call any function f from the babbler.core namespace as babble/f, and you can call foo directly. In addition, your file has information about where foo comes from so you or someone else won't need to go searching for it.

using clojure.string causes WARNINGs

When using clojure.string, I receive the following warnings
WARNING: replace already refers to: #'clojure.core/replace in namespace: tutorial.regexp, being replaced by: #'clojure.string/replace
WARNING: reverse already refers to: #'clojure.core/reverse in namespace: tutorial.regexp, being replaced by: #'clojure.string/reverse
my clojure script is:
(ns play-with-it
(:use [clojure.string]))
Is there any way to fix those warnings?
Yes, switch to
(ns play-with-it
(:require [clojure.string :as string]))
and then say e.g.
(string/replace ...)
to call clojure.string's replace function.
With :use, you bring in all Vars from clojure.string directly into your namespace, and since some of those have names clashing with Vars in clojure.core, you get the warning. Then you'd have to say clojure.core/replace to get at what's usually simply called replace.
The clash of names is by design; clojure.string is meant to be required with an alias like this. str and string are the most frequently chosen aliases.
In addition to MichaƂ's answer, you can exclude vars from clojure.core:
user=> (ns foo)
nil
foo=> (defn map [])
WARNING: map already refers to: #'clojure.core/map in namespace: foo, being replaced by: #'foo/map
#'foo/map
foo=> (ns bar
(:refer-clojure :exclude [map]))
nil
bar=> (defn map [])
#'bar/map
In addition to Alex's answer you can also refer only the vars you want from a given namespace.
(ns foo.core
(:use [clojure.string :only (replace-first)]))
This would not throw a warning since replace-first is not in clojure.core. However, you would still receive a warning if you did the following:
(ns foo.core
(:use [clojure.string :only (replace)]))
In general it seems people are tending toward (ns foo.bar (:require [foo.bar :as baz])).
Since Clojure 1.4 you can refer the individual functions you need from a namespace using :require with a :refer:
(ns play-with-it
(:require [clojure.string :refer [replace-first]]))
This is now recommended over :use.
Assuming you don't need the clojure.string/replace or clojure.string/reverse, that would also remove the warnings.
See this SO question and this JIRA issue for more details.