Light Table editor can't eval defprotocol in cljs source file - clojure

I use Windows 10, first I create project via Lein new mies
then I add some content in core.cljs
(+ 1 2)
(defprotocol MaybeMutable
(mutable? [this] "Returns true if the value is mutable."))
I select code above and hit ctrl + enter to eval the code, (+ 1 2) evals correct, but error occurs in defprotocol
clojure.lang.ExceptionInfo: Can't change/establish root binding of: unchecked-if with set

Related

Use repl instead of println in Clojure

Let's say we need evaluate a number of clauses in a source file, e.g.
test.clj
#(def x 1)
#(def y 2)
#(def z 3)
Only the last evaluation shows up if we plainly use either clj or lein repl,
user => (load-file "test.clj")
3
We can surround each of them by println to show all of them,
test-with-println.clj
(println #(def x 1))
(println #(def y 2))
(println #(def z 3))
user => (load-file "test-with-println.clj")
1
2
3
nil
What are the better alternatives to avoid invasive printlns all over the source code while able to print out all the intended evaluations under the umbrella of REPLs?
#Solution
Thanks to tap> from the answer #Sean Corfield we can have desired results as follows,
pipe every wannabe printed out to tap>.
test-with-tap.clj
(-> #(def x 1) tap>)
(-> #(def y 2) tap>)
(-> #(def z 3) tap>)
REPLs will receive none of their outputs until tap> is turned on by add-tap.
user=> (load-file "test-with-tap.clj")
nil
user=> (add-tap #(def out (bound-fn* println)))
nil
user=> (load-file "test-with-tap.clj")
1
2
3
user=> (remove-tap out)
nil
user=> (load-file "test-with-tap.clj")
nil
tap> is probably what you're looking for - https://clojuredocs.org/clojure.core/tap%3E - This lets you send full Clojure values (not just strings) to any function registered as a listener, which can be something like Portal for example - https://github.com/djblue/portal - which is something I use all-day, every-day, but you can also use something as simple as println in add-tap to get every tap>'d value displayed.
The tap> calls can stay in your code, even in production, since there is next to no overhead to using them.
Although tap> returns a Boolean, you can use (doto expr tap>) to evaluate and return expr but still send the value to any tap listeners. tap> is a great debugging tool.
I haven't had a chance to play with tap> yet. My favorite over the years has been the spy family of functions in the Tupelo library. Specifically, check out spyx, spyx-pretty, and let-spy.

Accessing defrecord methods in ClojureScript Figwheel

I have some code in cljc files which compiles to both Clojure and ClojureScript.
in protocols.cljc
(defprotocol Transformable ".."
(scale [this scalar] "" ) ...)
in pattern.cljc
(defrecord APattern [paths]
Transformable
(scale [this s] (...)) ...)
in another.cljc
(defn f [pattern] (.scale pattern (/ 1 2)) )
And in core.cljs
(another/f pattern)
However, I'm getting an error on the browser console
TypeError: pattern.scale is not a function
Checking the fields of pattern object in core.cljs (using js-keys) shows me that the object has something called
"patterning$protocols$Transformable$scale$arity$2"
which looks like my function. So am I just doing something wrong to access it in ClojureScript? Does . notation not work? Do I need to do something else?
Calls to protocol functions are just like calls to any other function. So your f function should look like:
(require 'protocols)
(defn f [pattern] (protocols/scale pattern (/ 1 2)) )

Clojure, console: println output not always visible

Subj. There's a working program, which basically copies filesystem trees recursively. Somehow println from inside the recursive function won't show any output.
build-album calls traverse-dir; I can see the "10" in the console, but never any "11"s -- should be a lot of them. (println "11") can't possibly miss the path of execution, since files get really copied (the line above). This is not quite nice, since the project is meant as a console application, reporting to the user each copied file, lest he should suspect freezing. This is no joke, because the app is intended to upload albums to mobile phones.
(defn traverse-dir
"Traverses the (source) directory, preorder"
[src-dir dst-step]
(let [{:keys [options arguments]} *parsed-args*
dst-root (arguments 1)
[dirs files] (list-dir-groomed (fs/list-dir src-dir))
dir-handler (fn [dir-obj]
"Processes the current directory, source side;
creates properly named directory destination side, if necessary"
(let [dir (.getPath dir-obj)
step (str dst-step *nix-sep* (fs/base-name dir-obj))]
(fs/mkdir (str dst-root step))
(traverse-dir dir step)))
file-handler (fn [file-obj]
"Copies the current file, properly named and tagged"
(let [dst-path (str dst-root dst-step *nix-sep* (.getName file-obj))]
(fs/copy file-obj (fs/file dst-path))
(println "11")
dst-path))]
(concat (map dir-handler dirs) (map file-handler files))))
(defn build-album
"Copy source files to destination according
to command line options"
[]
(let [{:keys [options arguments]} *parsed-args*
output (traverse-dir (arguments 0) "")]
(println "10")
output))
Might be the problem with lazy sequences: you build a lazy seq which is never realized and thus the code never executes. Try calling doall on the result of traverse-dir:
(doall (concat (map dir-handler dirs) (map file-handler files))))

Error defining anonymous function in Clojure

I'm going through the Joy of Clojure book and ran into the following series of errors in Ch. 2:
(def make-list0 #(list))
=> (var cursive-test.core/make-list0)
(make-list0)
IllegalStateException Attempting to call unbound fn: #'cursive-test.core/list clojure.lang.Var$Unbound.throwArity (Var.java:43)
(def make-list2 #(list %1 %2))
=> (var cursive-test.core/make-list2)
(make-list2 1 2)
IllegalStateException Attempting to call unbound fn: #'cursive-test.core/list clojure.lang.Var$Unbound.throwArity (Var.java:43)
(def make-list2+ #(list %1 %2 %&))
=> (var cursive-test.core/make-list2+)
(make-list2+ 1 2 3 4 5)
IllegalStateException Attempting to call unbound fn: #'cursive-test.core/list clojure.lang.Var$Unbound.throwArity (Var.java:43)
I'm not sure what is going on here. I'm using IntelliJ IDEA with the Cursive plugin. Any ideas?
Somehow you accidentally defined something called list in your own namespace, but didn't give it a value. One way you could accidentally do this would be to use a def inside a function, but never actually call that function:
(defn foo [x]
(def list x))
The solution is to not do that, and the easiest way to get back to normalcy is to restart your repl and reload the namespace once it no longer has this incorrect redefinition of list in it. If you can't find where you've defined it, note that reloading the namespace should also print a warning message telling you you're redefining list, which I think includes a line number, but I don't recall for sure.

custom config for riemann

I'm new to riemann and clojure as well. what is want is, when new event comes in, it checks its state and service, if both things got match it print some thing in console. here is my config file.
(let [index (default :ttl 300 (update-index (index)))]
; Inbound events will be passed to these streams:
(streams
; Index all events immediately.
index
; Calculate an overall rate of events.
(with {:metric 1 :host nil :state "ok" :service "events/sec"}
(rate 5 index))
; my code starts from here
; _______________________
(where (and (= :service "foo")
(= :state "ok"))
(println "It works"))
;________ENDS HERE ______________
; Log expired events.
(expired
(fn [event] (info "expired" event)))
))
when I start riemann, I can see "It Works" in console. but never afterwards.
tell me where i'm doing wrong.?
The problem looks to be your use of keywords in the where expression. The where function will re-write the conditions to use keywords internally, though this behaviour doesn't appear to be clearly stated outside the API documentation. If you look at the example in the howto the conditions in where expressions don't have colons on field names.
I have tested the following config:
(where (and (service "foo")
(state "ok"))
prn)