Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to load ns by default when starting repl

I am using lein2. I would like to load some ns by default when the repl starts. Is it possible to either specify in project.clj the ns that should be loaded, when lein2 repl is executed for that project?

like image 758
murtaza52 Avatar asked Sep 08 '12 09:09

murtaza52


2 Answers

You will find a lot of answers in the sample project

;; Options to change the way the REPL behaves
:repl-options {;; Specify the string to print when prompting for input.
               ;; defaults to something like (fn [ns] (str *ns* "=> "))
               :prompt (fn [ns] (str "your command for <" ns ">, master? " ))
               ;; Specify the ns to start the REPL in (overrides :main in
               ;; this case only)
               :init-ns foo.bar
               ;; This expression will run when first opening a REPL, in the
               ;; namespace from :init-ns or :main if specified
               ;; This expression will run when first opening a REPL, in the
               ;; namespace from :init-ns or :main if specified
               :init (println "here we are in" *ns*)

Using a project.clj I had handy:

(defproject test "1.0.0"
  :repl-options { :init-ns test.core 
                  :init (do
                          (use 'clojure.set) 
                          (println (union #{1 2 3} #{3 4 5}))
                          (use 'incanter.core)
                          (println (factorial 5))) }
  :dependencies [[org.clojure/clojure "1.4.0"]
                 [incanter/incanter-core "1.3.0-SNAPSHOT"]])

When I fire up lein repl

$ lein repl
nREPL server started on port 1121
REPL-y 0.1.0-beta10
Clojure 1.4.0
    Exit: Control+D or (exit) or (quit)
Commands: (user/help)
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
          (user/sourcery function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
Examples from clojuredocs.org: [clojuredocs or cdoc]
      (user/clojuredocs name-here)
      (user/clojuredocs "ns-here" "name-here")
#{1 2 3 4 5}
120.0
test.core=>
like image 66
noahlz Avatar answered Oct 02 '22 12:10

noahlz


I sometimes use the :injections option in project.clj to load namespaces. The following example will load the foo.bar namespace when the lein2 command is executed:

(defproject org.example/sample "0.1.0-SNAPSHOT"
  :description "A sample project"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :injections [(use 'foo.bar)])
like image 40
tnoda Avatar answered Oct 02 '22 14:10

tnoda