I have a fairly standard Quil file that I am editing with Emacs and nrepl.
(defn setup []
(qc/smooth)
(qc/frame-rate 24)
(qc/background 200))
(defn draw []
(draw-world))
(qc/defsketch run
:title "Circles!"
:setup setup
:draw draw
:size [800 600]
:renderer :opengl)
To start with, I use C-c C-l to load the file; this creates a sketch window. I then edit my draw-world function to, say, draw in a different color. My question is:
How do I update the current Quil window with this new function?
*C-x C-e doesn't seem to work.
Try C-M-x (this evals the current top-level form) in the function you want to change or C-c C-k (this evals the current buffer) in the source buffer. Btw, C-x C-e should be working too (it certainly works for me, but I rarely use it). Maybe you're not using nrepl.el's latest version?
I just set up a sample project to handle my workflow for live-coding in Quil. I copied some basics from several places, such as the Quil wiki and forums.
If you look at the basic core.clj file of the project, you'll see it requires separate "draw" and "setup" namespaces:
(ns basic-metronome.core
(:use [basic-metronome.setup :only [HEIGHT WIDTH]])
(:require [basic-metronome.draw :as dynamic-draw]
[basic-metronome.setup :as dynamic-setup]
[quil.core :as qc]))
(defn run-sketch []
(qc/defsketch the-sketch
:title "Hello Metronome"
:setup dynamic-setup/setup
:draw dynamic-draw/draw
:size [WIDTH HEIGHT]))
From:
https://github.com/mudphone/basic_quil_metronome/blob/master/src/basic_metronome/core.clj
In this way, I can re-evaluate C-c C-k the draw.clj file without having to re-evaluate the top-level core namespace (which can cause problems, such as the one you describe where you're seeing a new window).
Related
I am having a similar issue to this person: Problems while creating a deps.edn file
However, I'm on MacOS and trying to follow the book and use deps.edn instead of leiningen, so I wasn't able to solve my issue from reading the answers in that post.
I'm using my terminal window and just text files, or Emacs.
Within the terminal, I created a folder called tennisProject. Then I created 2 files, deps.edn and tennisProject.clj inside that folder. Then I put the csv file of tennis data in that folder.
Then I go back to the terminal and restart it. I make tennisProject the current directory. I type in "clj" to start a repl. Then I do (in-ns 'packt-clj.tennisProject) to get into the right namespace. Then, I type (first-match "match_scores_1991-2016_unindexed_csv.csv"), and I get an error:
Syntax error compiling at (REPL:1:1).
Unable to resolve symbol: first-match in this context
The contents are as follows (I copied and pasted from the book).
deps.edn:
{:deps
{org.clojure/data.csv {:mvn/version "1.0.0"}
semantic-csv/semantic-csv {:mvn/version "0.2.1-alpha1"}}}
tennisProject.clj:
(ns packt-clj.tennisProject
(:require
[clojure.data.csv :as csv]
[clojure.java.io :as io]
[semantic-csv.core :as sc]))
(defn first-match [csv]
(with-open [r (io/reader csv)]
(->> (csv/read-csv r)
sc/mappify
first)))
I have a few things different than the book: I changed the name from tennis to tennisProject because I kept making new folders after getting errors. I also changed the data.csv version from "0.1.4" to "1.0.0" because that's what was in the answer I linked, but that didn't resolve my issue. Then I also have semantic-csv/semantic-csv but in the book it's just semantic-csv. I changed that because the repl advised me to make the change.
If I just require the dependencies one by one in the repl, and define the function in the repl, everything works fine, but I really want to understand how all these files work together and I appreciate your help!
By default, the Clojure CLI / deps.edn assumes your source code is going to be in a tree under a folder called src.
The namespace in a Clojure file must "match" its filepath relative to src so for packt-clj.tennisProject, the file should be src/packt_clj/tennisProject.clj -- note the - in a namespace corresponds to an _ in the filepath.
If you reorganize your project like that, and restart your REPL, you should be able to require your code and work with it.
As a stylistic note, we don't use camelCase much in Clojure: it would be more idiomatic to have tennis-project as the namespace (which means tennis_project.clj as the filename).
(edited to add this example session)
(! 556)-> pwd
/Users/sean/clojure/tennisProject
(! 557)-> ls
deps.edn example.csv src
(! 558)-> tree
.
|____deps.edn
|____example.csv
|____src
| |____packt_clj
| | |____tennisProject.clj
(! 559)-> clj
Clojure 1.10.3
user=> (require 'packt-clj.tennisProject)
nil
user=> (in-ns 'packt-clj.tennisProject)
#object[clojure.lang.Namespace 0x128c502c "packt-clj.tennisProject"]
packt-clj.tennisProject=> (first-match "example.csv")
{:some "42", :headers "A value", :in "1", :this "2", :file "3.333"}
packt-clj.tennisProject=> ^D
(! 560)-> cat src/packt_clj/tennisProject.clj
(ns packt-clj.tennisProject
(:require
[clojure.data.csv :as csv]
[clojure.java.io :as io]
[semantic-csv.core :as sc]))
(defn first-match [csv]
(with-open [r (io/reader csv)]
(->> (csv/read-csv r)
sc/mappify
first)))
(! 561)-> cat example.csv
some,headers,in,this,file
42,"A value",1,2,3.333
Let's say I create a new Leiningen project (lein new app example) and add some code in example/src/example/core.clj that makes use of :gen-class:
(ns example.core
(:gen-class :extends javafx.application.Application))
(defn -start [this stage]
(.show stage))
(defn -main [& args]
(javafx.application.Application/launch example.core args))
If I then create a JAR (lein uberjar) and run it, everything works fine. However, if I instead try to run my app directly (lein run), I get a ClassNotFoundException. In addition, if I open a REPL (lein repl), I first get the same error as before, but after running this code:
(compile 'example.core)
the error no longer appears in lein run or lein repl.
Could someone please explain to me what exactly is going on here, and how I can run my app directly without needing to manually compile my code from a REPL?
Edit: After fooling around a bit more, I found that the solution to this problem is to add
:aot [example.core]
to project.clj. (Thanks #Mars!) I'm still confused, though, because I had previously tried simply removing ^:skip-aot, which (according to the docs) should work:
This will be AOT compiled by default; to disable this, attach
^:skip-aot metadata to the namespace symbol.
But it doesn't. Why?
Another edit (if I should split any of this into a separate question, let me know and I'll do so): I've been playing with hyphens (lein new app my-example), and weird stuff has been happening. This doesn't work:
(ns my-example.core
(:gen-class :extends javafx.application.Application))
;; ...
(defn -main [& args]
(javafx.application.Application/launch my-example.core args))
But this does:
(ns my-example.core
(:gen-class
:name my-example.Core
:extends javafx.application.Application))
;; ...
(defn -main [& args]
(javafx.application.Application/launch my-example.Core args))
So my class name can either start with a lowercase letter (example.core) or contain a hyphen (my-example.Core), but not both? I really don't get it.
And finally, lein uberjar fails on that final example (with the explicit :name), because
Warning: The Main-Class specified does not exist within the jar. It may not be executable as expected. A gen-class directive may be missing in the namespace which contains the main method.
As far as I can tell, the only way to fix that is to split the Application subclass into a separate namespace. Is there another way?
Agreed with #Mars, the problem is that lein run does not AOT the example.core namespace. The default Leiningen template made the example.core non AOT:
(defproject example "0.1.0-SNAPSHOT"
...
:main ^:skip-aot example.core
...)
My best guess is that you could define your app using defrecord and use that as a class instead of the namespace.
JavaFX 8, Java 1.8.0_31, Windows 7 x64
I have a minimal JavaFX program in Clojure. The (ns...) clause is able to import the required Java packages fine except the classes in javafx.scene.control, such as Button and TextField, etc.
I have to put the import for these after initializing the toolkit. Why can't I import these classes before the toolkit is initialized? I'm not actually creating any objects yet... so I'm guessing JFX is somehow doing something in the background while these classes are imported, requiring the initialization first. Below is my complete lein project (minimized from the actual application where I saw this problem, and without all the nice macros that clean up the JFX syntax):
File project.clj:
(defproject jfx-so "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.6.0"]]
:main jfx-so.core)
File src/jfx_so/core.clj:
(ns jfx-so.core
(:import [javafx.scene Scene]
[javafx.scene.layout BorderPane]
[javafx.stage Stage]))
(defonce force-toolkit-init (javafx.embed.swing.JFXPanel.))
;; For some reason the following must be imported after initting the toolkit
(import [javafx.scene.control Button])
(defn -main [& args]
(javafx.application.Platform/runLater
#(doto (Stage.)
(.setScene (Scene. (BorderPane. (Button. "Hello"))))
(.show))))
Thanks! :)
I haven't had a problem with this. Perhaps it has to do with your defonce?
I do my imports first. But I do make sure to init the FX-envoronment before instanciating any FX-classes. So after your -main-method I would put:
(defn -main [& args]
;;body here
)
;; initialze the environement
(javafx.embed.swing.JFXPanel.)
;; ensure I can keep reloading and running without restarting JVM every time
(javafx.application.Platform/setImplicitExit false)
;; then
(-main)
Hope this helps.
I just started on my first clojure project using lein, the code here:
(ns fileops.core
(:use
[clojure.core :only (slurp)]
[clojure-csv.core :only (parse-csv)]
[fileops.core]))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(read-file "sample.csv"))
(defn read-file
"open and read the csv file"
[fname]
(with-open [file (clojure.java.io/reader fname)]
(parse-csv (slurp fname))))
I tried to run this using "lein run" but I keep getting this error:
Caused by: java.lang.RuntimeException: Unable to resolve symbol: read-file in this context
at clojure.lang.Util.runtimeException(Util.java:219)
at clojure.lang.Compiler.resolveIn(Compiler.java:6874)
at clojure.lang.Compiler.resolve(Compiler.java:6818)
at clojure.lang.Compiler.analyzeSymbol(Compiler.java:6779)
at clojure.lang.Compiler.analyze(Compiler.java:6343)
... 52 more
What am I doing wrong?
You have used only slurp from clojure core, meaning that every other core function is now unavailable to you :) Try changing your ns to use :require instead of :use, as this is more idiomatic.
One thing to note is that order does matter in clojure, so if you don't declare a function at the top of your file, as in C and some other languages, the earlier functions will not be able to make reference to them. This is what was causing your error before and is why I like to define my -main function at the bottom. It's a matter of style.
Another thing is that your -main function is taking variable args right now and not using them. In Clojure it is idiomatic to use _ to refer to a parameter that doesn't get used. You could use & _ to avoid error messages, for when the user passes in unnecessary args, but I would just have the -main function parameterless from the start. This is because nothing needs to be provided to main when you run the program, and errors do make debugging easier. It is good to know what is getting used and where. The sample.csv file is already provided and is having read-file called on it, so the program should run if your read-file function is correct and the sample.csv file is in the proper location.
Regarding your -main function, it would be nice to put a little test in there to see if it executes properly when you run it, so I changed it to print out the contents of the csv file onto your console. This way of printing from a file is efficient and worth studying in its own right.
Finally, Make sure you include clojure-csv.core in your project.clj file.
core.clj:
(ns fileops.core
(:require
[clojure-csv.core :refer [parse-csv]]))
(defn read-file
"open and read the csv file"
[fname]
(with-open [file (clojure.java.io/reader fname)]
(parse-csv (slurp fname))))
(defn -main []
(println (clojure.string/join "\n" (read-file "resources/test.csv"))))
project.clj:
...
:dependencies [[org.clojure/clojure "1.5.1"]
[clojure-csv/clojure-csv "2.0.1"]
...]
:main fileops.core
You need to declare fileops.core as :main, as shown above. This tells Leiningen what function to execute when you enter lein run. Very important and tricky stuff.
So now make sure you are in the root of your project directory and at the terminal, run the following:
lein clean
lein deps
lein run
Good luck!
Further reading:
8th light blog on name-spaces
flying machine studios explanation of lein run
read-file should be before main in your source OR you should put before -main a declare cause like that:
(declare read-file)
I am a Clojure and Quil beginner.
As part of development, I would like to print information to the REPL in the draw function.
However, the following implementation doesn't work. There are no error generated, but it doesn't print anything either.
Why doesn't it work, and how can I print to the REPL from Quil draw functions?
Thank you in advance.
(ns quil-learning.core
(:use quil.core))
(defn setup []
(smooth)
(frame-rate 60)
(background 255))
(def saved-out *out*)
(defn draw []
(println "test 1") ; doesn't work
(let [*out* saved-out] (println "test 2")) ; doesn't work either
(stroke 0)
(stroke-weight 2)
(ellipse (mouse-x) (mouse-y) 2 2))
(defsketch example
:title "print test"
:setup setup
:draw draw)
nrepl is writing your output to the wrong buffer, this was supposed to be fixed for most contexts in nrepl 0.1.4 http://grokbase.com/t/gg/clojure/129jwz1yh9/ann-nrepl-el-0-1-4-released. I suggest using emacs24+ and using it's built in packaging system to keep nrepl up to date. see the Emacs starter kit for details. Or you can take a look at my fork of it which adds nrepl and clojure-mode to the default package list
check the terminal from which you started emacs, sometimes it lands there.