Got to my hands some old code, and now trying to adopt eastwood as linter. when doing that eastwood says that it has some code that is deprecated for example in ns x.y.z I have a require for [a.b :as b] and I want this required to be ignore from eastwood! can i do that?
I have tried the followed, in variation of the regex with no success:
(disable-warning
{:linter :deprecations
:symbol-matches #{#"^a\.b\.*"}})
any idea?
Related
I have number of useful helper functions that I use at the REPL during Clojure development. This includes some built-in functions like doc and pprint, but also some custom ones of my own from my user.clj. The default clojure experience seems to be:
dev> (doc +)
;; works
dev> (in-ns 'project.core)
project.core> (doc +)
;; what is this "doc" thing you're talking about!?!?
which is pretty irritating (I'm aware that I can refer to clojure.repl/doc here). Is there an easy way to ensure that something is available during development regardless of which namespace I'm currently operating in?
One option is to add a :repl-options to your project map in project.clj:
(defproject myproj "1.0"
:dependencies [[org.clojure/clojure "1.9.0-alpha15"]]
:repl-options { :init-ns myproj.core
:init (require '[clojure.repl :refer :all]) })
...
$ lein repl
myproj.core=> (doc +) ; works
As Alex Gherega mentions, it's probably a bad idea to automatically and indiscriminately import stuff into every namespace. However, you could create a macro (or maybe a function would work?) to help import development-related namespaced when/if they are needed. Add it to your user.clj. Then if you are working in another namespace and decide it would be useful, you can just run (user/import-useful-dev-stuff) (or whatever you decide to call it).
For what it's worth, I pretty much do all my REPL sessions within the user namespace and require all the namespaces I need from there. I use Emacs, and if I need to change something in another namespace, I just change the source, then do C-c C-k (after initiating a cider session) to reload the file. If you don't use Emacs, it's only a little more work to reload from the REPL via require with the :reload option.
Unless you use boot or lein or some other form that would "magically" add this namespace to your REPL development session then I'm afraid you're just have to require it by hand every time.
The thing is namespaces have their well intended role of avoiding name conflicts. If you would have another way of telling a current namespace to use/require another namespace without doing it then you would have a whole lot of other problems.
If you're not using lein/boot you could just do as Alex Miller responded. For a more controlled way of what you need from the clojure.repl or some other ns API just have a require & use declaration in you namespace of interest e.g.:
(ns 'project.core
(:require [clojure.repl :refer [doc]]))
If you get yourself in this situation, do:
(clojure.core/refer-clojure)
to require and load everything from clojure.core.
Or an alternate approach, if project.core exists, is to instead:
(require 'project.core)
(in-ns 'project.core)
Loading the namespace via require will refer clojure.core.
Or if project.core does not exist, do:
(ns project.core)
The ns macro will do both refer-clojure and in-ns.
I want to use the overtone and seesaw namespaces in one project. However when i load them there seems to be a shared function named (select) which exists in both seesaw.core and overtone.core ((seesaw.core/select) (overtone.core/select)), why i can't load both of the namespaces in the project. How could i come up with this? Will using refer with the :exclude keyword an option?
While many tutorials and examples use the use function or the :use clause in the ns form, in real code it is better to never do this. The preferable form is as follows:
(ns foo.bar
(:require [a.something :as some]
[b.another :as a]))
(some/f)
(a/f)
The functions are fully distinct and unambiguous, and also this makes the code easier to read and refactor - you have a clear indication of where the definitions you are using come from.
Everything is in the title, but here is an example use case :
a function had a first parameter like the following :
(my-fn "a.b.c" ...)
Which now in a newer version became :
(my-fn ... ["a", "b", "c"])
Is it possible to :require a specific version of a library ?
For instance :
:require my.util.lib :as newlib ;; new version
:require my.util.lib#v0.0.1 :as lib ;; old library
It would then allow to migrate this library usage file per file.
As mentioned in the comments, I think Osgi is the only true way to accomplish this, and probably not worth the effort in complexity to set it up.
What are the versions of the library you are using? If it is following semantic versioning practices, and it still on a version with breaking changes, you have to be prepared for this sort of thing and will probably just have to update your code.
A question from an utter newcomer to clojure: What if I want to be able to start a clojure REPL from anywhere, for example because I just want to compute an exponent? How can I set up my system to do this? (I've deleted earlier links to not-quite-answers because they were cluttering up the question.) The Pomegranate documentation linked by #Jared314's answer below helped me see that I can do this:
~$ lein repl
...
user=> (use '[cemerick.pomegranate :only (add-dependencies)])
nil
user=> (add-dependencies :coordinates '[[org.clojure/math.numeric-tower "0.0.2"]])
{[org.clojure/clojure "1.3.0"] nil, [org.clojure/math.numeric-tower "0.0.2"] #{[org.clojure/clojure "1.3.0"]}}
user=> (use 'clojure.math.numeric-tower)
nil
user=> (expt 2 3)
8
Yay!
Now how can I make this happen every time I start the REPL, no matter what subdirectory I'm in?
I think I'm just ignorant of basic clojure setup. Sorry about that.
Second major edit:
I've figured out that if I use raw clojure without lein, I can execute commands on startup of the repl. For example, if the file .clojurerc contains the text (print "Yow!\n"), I can do this:
~$ java -cp /usr/local/lib/clojure-1.5.1/clojure-1.5.1.jar clojure.main -i .clojurerc -r
Clojure 1.5.1
Yow!
user=>
Can I do something like this with lein? Or maybe better yet, load clojure.math.numeric-tower automatically in clojure without using lein (since for simple command line experimentation, lein's startup is slower than starting clojure directly).
(It may seem as if I'm not trying to solve this on my own, but I that's not so. I have been doing web searches and experimenting, but I keep hitting brick walls. I'm starting to feel as if clojure is only intended for full-blown programming projects. I had assumed that it could be good for add-hoc experiments and calculations (as lisps traditionally are but Java is not). I'm not trying to incite arguments. I'm just frustrated. There ought to be a simple, well-known formula for doing what I'm trying to do.)
When you want external dependencies you will need either a new project, lein new testproject1, the lein-oneoff plugin, Pomegranate, a Leiningen profile :dependencies entry, or some specific IDE feature. (I know at least LightTable allows external dependencies in their Instarepl, so I assume you can do it in Emacs and CCW.)
It might be best to start with creating a new test project so you can see the project.clj layout. But, if you just want a one-off library in a repl, take a look at the instructions for Pomegranate's add-classpath command. Pomegranate is accessible by default in the lein repl, so their example should work without anything extra.
Edit:
From your updated question, it sounds like you want a persistent repl dependency. You can add [org.clojure/math.numeric-tower "0.0.2"] to your ~/.lein/profiles.clj profile file, under the :repl profile.
{:user {}
:repl {:dependencies [[org.clojure/math.numeric-tower "0.0.2"]]
:repl-options {:init (use 'clojure.math.numeric-tower)}}}
Then when you run lein repl:
(expt 2 3) ;=> 8
Looking at this question a few months later, I realized that I settled on a different solution, but never posted it. I now leave references to math.numeric-tower out of both .clojurerc and .lein/project.clj, since I don't need it for most Leiningen projects. And when I want to use Clojure for quick calculations, I start Clojure via a shell script without Leiningen to avoid slow startup. This is what's in the shell script:
#!/bin/sh
jars=/usr/local/lib/clojure-1.5.1/clojure-1.5.1.jar:/usr/local/lib/clojure-1.5.1/math.numeric-tower-0.0.2.jar
while [ "$1" != "${1%.jar}" ]; do # while param is a jar file
jars="$jars:$1"
shift
done
jars="$jars:."
exec rlwrap java -cp "$jars" clojure.main -e "(use 'clojure.math.numeric-tower)" "$#" -r
The middle part allows you to add jars on the command line, but in practice I don't find that useful, given that I use Leiningen for ongoing projects. Someone else might find that part of the script useful, though. rlwrap is a utility that gives you command line history, though it's not as good as what Leiningen provides, for my purposes.
I'm new to Clojure and building a web app using the Noir framework (very similar to Compojure, in fact I think it's Compojure with a different request handler layer). I'm getting a warning when I import the JDBC library:
WARNING: resultset-seq already refers to: #'clojure.core/resultset-seq in namespace: webapp.models.database, being replaced by: #'clojure.java.jdbc/resultset-seq
Do I have to live with this warning or is there a way around it? I'm importing the JDBC library using:
(use 'clojure.java.jdbc)
You can avoid the problem by specifying the exact bindings to be imported:
(use '[clojure.java.jdbc :only [insert-values transaction]])
(transaction
(insert-values ...))
Another option is to :exclude the offending binding:
(use '[clojure.java.jdbc :exclude [resultset-seq]])
(transaction
(insert-values ...))
You can also just use require instead:
(require '[clojure.java.jdbc :as db])
(db/transaction
(db/insert-values ...))
With regard to forward compatibility, require is arguably safest. Using :only is just slightly less clean but still a pretty good approach (and easy to fix when it breaks). Excluding the currently offending bindings is probably the least future-proof way of fixing the problem since other conflicting bindings can appear at any time and tracking down what is imported from where can be tricky.
There are lots of options. What this warning means is, that you are replacing an already defined symbol with a definition from different package. In this case, it looks like this is a variable that you've defined, right? If so the easiest solution might be to just rename it in your code.
Or if you don't need the resultset-seq from clojure.java.jdbc package you can exclude it:
(use '[clojure.java.jdbc :exclude (resultset-seq)])
or better yet,
(use '[clojure.java.jdbc :only (f1 f2 f3)])
where f1, f2, f3 are the things you're actually need.
(use '[clojure.java.jdbc :as jdbc])
and then use jdbc/resultset-seq
Or you can just:
(require 'clojure.java.jdbc)
and then use clojure.java.jdbc/reusltset-seq
In addition to the other excellent answers, if you want the jdbc resultset-seq instead of the core one, you can exclude the latter from being brought into the current ns:
(ns foo
(:refer-clojure :exclude [resultset-seq])
(:use clojure.java.jdbc))