Defining a map object inside a loop recur - Clojure - json

I am trying to use a loop recur that creates an empty map inside the loop. For each entry in the loop (loops through a vector of maps) it will see if there is a key in the newly created map that matches the iterated values key and if not create one.
I have created this code:
(def meteor-map (json/read-str (clojure.string/lower-case
(slurp "https://data.nasa.gov/resource/y77d-th95.json"))))
(defn most-falls [values]
(loop [values map count-tracker{}]
(if (empty? values)
(count-tracker)
(do
(def key (keyword (get (first values) "year")))
(if (contains? (first values) key)
(do
(def count-tracker (update count-tracker key inc))
(recur (rest values) count-tracker)
)
(do
(def count-tracker (assoc count-tracker key 1))
(recur (rest values) count-tracker)
)
)
)
)
)
)
(most-falls meteor-map)
However when I call this function and pass in meteor-map (which is a vector of maps) i get an error saying
wrong number of args (0) passed to persistentarraymap
I think this could be due to how I am creating the initial count-tracker object inside the loop creation but I am unsure.
Any ideas?
Thanks
PS am aware this question is a bit vague so any questions just ask!

There's multiple things here to be brought up. Your main problem though is with (count-tracker). You're surrounding the map in parenthesis, which means that you want to call it as a function. You can't arbitrarily add parentheses to code like you can in other languages; it has a very specific meaning in Clojure. (f) always means that the function f is being called. Just change it to count-tracker to return the value.
Other things:
NEVER use def inside of a function unless it's necessary. In this case though, it's entirely unnecessary. Every use of def creates globals that last for the length of the program (yes, they exist even after the function exits!). Use let instead:
(let [key (keyword (get (first values) "year")))]
... ) ; Use key here
(loop [values map ...] will cause errors as well. map is a function, so this throws away the argument passed in to most-falls, overwriting it with the map function. This will cause an error when you try to use values as a sequence, since the map function doesn't support empty? or first, or anything else you're trying to use it for. I think you just intended to just rebind the argument to be used in the loop. Just change it to (loop [values values ...]. Arguably, you shouldn't shadow arguments by creating other bindings with the same name, but that's not exceedingly important here.
There's still a couple other petty things that could be improved. By using destructuring you could skip the calls to first and rest, and using reduce could simplify the explicit looping using loop, but those would detract from the main issues. Taking into consideration what I mentioned above, I'd write your function as:
(defn most-falls [values]
(loop [values values
count-tracker {}]
(if (empty? values)
count-tracker
(let [key (keyword (get (first values) "year"))]
(recur (rest values)
(if (contains? (first values) key)
(update count-tracker key inc)
(assoc count-tracker key 1)))))))

First, there is definitely an issue in that clause:
(loop [values map count-tracker{}]
I'm not use what did you try to achieve, but please take a look.
Next, never use def or defn forms inside the code, only on the top level of a namespace.
Finally, the loop/recur is pretty low-level form and should be used with a strong knowledge of what you are doing. More often, it might be replaced with more user-friendly ones. The reduce would be a good one I believe. It takes an initial value (an empty map in your case), a collection and a function of two arguments where the second one is the current collection's item and the second one either an initial value or a result of the previous function call.
Inside that function, you decide whether your map has some specific key and add it if it does not.
Short example:
# here are some data you've read from a file
(def items [{...} {...} {...}])
# reduce process function
(defn process
[result item]
(if (:some-key result) ;; here, you check the current map for a key
result ;; return the old map if everything is ok
(assoc result :some-key some-data))) ;; accumulate a new key into a map
(reduce process {} items)

Related

How exactly does Clojure process function definitions?

I'm studying Clojure, and I've read that in Clojure a function definition is just data, i.e. parameters vector is just an ordinary vector. If that's the case, why can I do this
(def add (fn [a b]
(+ a b)))
but not this
(def vector-of-symbols [a b])
?
I know I normally would have to escape symbols like this:
(def vector-of-symbols [`a `b])
but why don't I have to do it in fn/defn? I assume this is due to fn/defn being macros. I tried examining their source, but they are too advanced for me so far. My attempts to recreate defn also fail, and I'm not sure why (I took example from a tutorial):
(defmacro defn2 [name param & body]
`(def ~name (fn ~param ~#body)))
(defn2 add [a b] (+ a b)) ;;I get "Use of undeclared Var app.core/defn2"
Can someone please explain, how exactly does Clojure turn data structures, especially symbols, into code? And what am I missing about the macro example?
Update Apparently, macro does not work because my project is actually in Clojurescript (in Clojure it does work). I did not think it matters, but as I progress - I discover more and more things that somehow don't work for me in with Clojurescript.
Update 2 This helps: https://www.clojurescript.org/about/differences
A function is a first-class citizen as other data in Clojure.
To define a vector you use (vector ...) or reader has syntaxic sugar [...], for a list it's (list ...) or '(...) the quote not to evaluate the list as a function call, for a set (set ...) or #{...}.
So the factory function for a function is fn (in fact fn*, that comes from Java core of Clojure, fn is a series of macros to manage to destructure and all).
(fn args body)
is a function call that returns a function, where args is a vector of argument(s) event. empty and body is a series of Clojure expressions to be evaluated with args bind to the environment. If nothing is to be evaluated it returns nil. There is also a syntactic sugar #(...) with %x as argument x and % as argument 1.
(fn ...) return a value that is a function. So
(def my-super-function (fn [a b c d] (println "coucou") (+ a b c d)))
binds the symbol my-super-function with the anonymous function returned by (fn [a b c d] (println "coucou") (+ a b c d)).
(def my_vector [1 2 3])
binds the symbol my_vector with the vector [1 2 3]
List of learning resources: https://github.com/io-tupelo/clj-template#documentation
As #jas said, your defn2 macro looks fine.
The main point is that macros are an advanced feature that one almost never needs. A macro is equivalent to a compiler extension, and that is almost never the best solution to a problem. Also keep in mind that functions can do some things macros can't.
Another point: the syntax-quote (aka backquote) ` is very different from a single quote '. In your example you want the single quote for ['a 'b]. Even better would be to quote the entire vector form '[a b].
As to your primary question, it is poorly explained how source-file text is converted into code. This is a 2-step process. The Clojure Reader consumes text string data (from a file or a literal string) and produces data structures like lists, vectors, strings, numbers, symbols. The Clojure compiler takes these data structures as input and produces java byte code that can be executed.
It is confusing because, when printed, one can't tell the difference between the text representation of a vector [1 2 3] and the text string that is input to the reader [1 2 3]. Ideally it would be color-coded or something. This problem doesn't exist in Java, etc since they don't have macros and hence there is no confusion between the source code (text) and the data structures used by a macro (not text).
For a more detailed answer on creating macros in Clojure, please see this answer.

Clojure: How to use Mapping with an Anonymous Function?

(defn recurse
[temp total] ;total is: (and true true(and false))
(map (fn [i]
(cond
(seq? i) (println "");If total is not a single parenthesis (single sequence), recur until it is
(= i 'and) (System/exit 0) ;I want this to be called only when the **second** "and" is called
:else (println "This should never print I think")
))
idealreturn)
)
I want (System/exit 0) to be called only when the second "and" is detected in total and not before. How would I go about doing this?
You are on the right track with mapping a function over the data to transform it. There are a couple of ways to get what you are looking for:
Don't use map, and use reduce instead. Reduce is for building up state over time. So you could reduce it into an expression, and each time you encounter an and, you look to see if there is already an and in the result you are building up, and if that and is already there, call the exit.
Have the function you are mapping over the input do only one thing, convert single items into more meaningful things. Then once it is done, pass that result to a second function that checks if it's time to exit.
Giving each thing one responsibility makes for code that's much easier to write, and composing them afterwords is efficient and easy. It's also much easier on you later when you come back to work on the code later.

is this swing tablemodel code badly designed?

Context: I have a clojure-based crossword app whose main ui is a JTabbedPane with two tabs, a grid and a clue table. The clue table is a view over a vector of clues, but the vector itself is not the authoritative store of the data, but dynamically generated from a couple of internal data structures via an (active-cluelist) function, triggered by the clue tab being selected.
So this is the implementation of the clue table:
(def cluelist [])
(def update-cluelist)
(def model)
(defn make []
(let [column-names ["Sq" "Word" "Clue"]
column-widths [48 200 600]
table-model (proxy [AbstractTableModel] []
(getColumnCount [] (count column-names))
(getRowCount [] (count cluelist))
(isCellEditable [row col] (= col 2))
(getColumnName [col] (nth column-names col))
(getValueAt [row col] (get-in cluelist [row col]))
(setValueAt [s row col]
(let [word (get-in cluelist [row 1])]
(add-clue word s) ; editing a cell updates the main clue data
(def cluelist (assoc-in cluelist [row 2] s))
(. this fireTableCellUpdated row col))))
table (JTable. table-model)
]
; some pure display stuff elided
(def model table-model)
)
(defn update-cluelist []
(def cluelist (active-cluelist))
(.fireTableDataChanged model))
Someone in another discussion noted that it is a major code smell for (update-cluelist) to be manually calling fireTableDataChanged, because nothing outside the TableModel class should ever be calling that method. However, I feel this is an unavoidable consequence of the table being dynamically generated from an external source. The docs aren't too helpful - they state that
Your custom class simply needs to invoke one the following
AbstractTableModel methods each time table data is changed by an
external source.
which implicitly assumes that the CustomTableModel class is the authoritative source of the data.
Also there is a bit of a clojure/java impedance mismatch here - in java I would have had cluelist and update-cluelist be a private member and method of my TableModel, whereas in clojure cluelist and the table model are dynamically scoped vars that update-cluelist has access to.
My main problem is that there is not a lot of clojure/swing code around that I can look to for best practices. Does anyone have any advice as to the best way to do this?
Suggestion: use an atom for cluelist. Constantly redefining cluelist is not the right way to represent mutable data. Honestly, I would expect it to throw an exception the second time you define cluelist.
If you use an atom for cluelist, you can call the fireTableDataChanged method from a watcher instead of calling it manually. This would mean that anytime (and anywhere) you change the atom, fireTableDataChanged will be called automatically, without an explicit call.
The issue with def is that calling def multiple times doesn't work well in a multi-threaded environment and Clojure tries to make everything default to fairly threadsafe. As I understand it, the "proper" way to use a var is to leave its root binding alone (ie, don't call def again) and use binding if you need to locally change it. def may work the way you are using it, but the language is set up to support atoms, refs, or agents in this sort of situation and these will probably work better most of the time (ie you get watchers). Also, you don't need to worry at all about threads if you add them later.

Clojure simple sort function error

I'm new in clojure, i try create functions thats will be sort collections and store it in object.
My code:
(defn uniq [ilist]
([] [])
(def sorted (sort ilist)))
I try to run it:
(uniq '(1,2,3,6,1,2,3))
but get error:
#<CompilerException java.lang.IllegalArgumentException: Key must be integer (NO_SOURCE_FILE:0)>
What's wrong?
Thank you.
As with your other question, you're trying to use pattern-matching where it just doesn't apply. Your function would work fine1 if you deleted the ([] []) entirely.
1 You also shouldn't use def here; as the other respondents have noted, you want to use let for establishing local bindings. However, here you don't need any bindings at all: just return the result of the sort call. In fact, the def will cause you to return a Var instead of the actual sorted list.
Since there's no need at all to use either 'let' or 'def', I have to agree with amalloy about Bart J's answer. Sure it warrants the upvotes because it's useful info, but it's not the right answer.
Actually, defining the function is kind of useless, since (sort ilist) would do the trick. The result of the function is the 'object' you want. That is, unless you want to use the result of the sort multiple times at different places in the function body. In that case, bind the result of sort to a function local variable.
If you only need the sort once, don't bother binding it at all, but just nest it inside other functions. For instance if you want to use it inside a unique function (which I guess is what you're wanting to do):
(defn uniq
"Get only unique values from a list"
[ilist]
; remove nils from list
(filter #(not(nil? %))
; the list of intermediate results from (reduce comppair sortedlist)
; (includes nils)
(reductions
; function to extract first and second from a list and compare
(fn comppair
[first second & rest]
(if (not= first second) second))
; the original sort list function
(sort ilist))))
(uniq '(1,2,3,6,1,2,3))
(1 2 3 6)
Then again, you could also just use the built-in distinct function, and take a look at it's source:
(distinct '(1,2,3,6,1,2,3))
(1 2 3 6)
(source distinct)
(defn distinct
"Returns a lazy sequence of the elements of coll with duplicates removed"
{:added "1.0"}
[coll]
(let [step (fn step [xs seen]
(lazy-seq
((fn [[f :as xs] seen]
(when-let [s (seq xs)]
(if (contains? seen f)
(recur (rest s) seen)
(cons f (step (rest s) (conj seen f))))))
xs seen)))]
(step coll #{})))
To store the sorted collection into a variable do this:
(let [sorted (sort your-collection)])
To understand the difference between a let and a def, this should help:
You can only use the lexical bindings made with let within the scope of let (the opening and closing parens). Let just creates a set of lexical bindings. def and let do pretty much the same thing. I use def for making a global binding and lets for binding something I want only in the scope of the let as it keeps things clean. They both have their uses.

Interactive "r" elisp defun with additional args?

Is it possible to write an interactive defun with code "r" that has an additional optional argument (so that it does things within the selected region, but with another argument)? I would like something like the following:
(defun my-function (start end &optional arg)
"Do something with selected region"
(interactive "r")
(if arg
(setq val arg)
(setq val 2))
(do things...))
Looking at the documentation it says
'r': Point and the mark, as two numeric
arguments, smallest first. This is the
only code letter that specifies two
successive arguments rather than one.
No I/O.
I'm not sure if the 'No I/O' and 'two successive arguments' means that it takes 2 and only 2 arguments (i.e., limited to the region's start and end point as args). Although it allows me to evaluate and run the defun with an additional argument, Emacs appears to be ignoring it.
Thank you.
To make interactive ask for multiple parameters, separate them with a newline character. For instance, if you want your third parameter be bound to the value of the prefix argument, define your function like this:
(defun my-function (start end &optional arg)
"Do something with selected region"
(interactive "r\np")
(if arg
(setq val arg)
(setq val 2))
(do things...))
M-x describe-function interactive gives you further information.
A function can be called in two ways:
Interactively: This is what happens when a user calls the command, e.g. when it has been bound to a key.
From lisp: When the function is called from another lisp function. e.g. (r 100 200 t).
In your case, you have to make sure that the arguments match the interactive specification, in this case it must accept two arguments. The third will not be used when called interactively (so then it will get the value nil).
NO I/O means that it will not prompt the user for input (like it does when it asks for a file name).
If you want your function to act differently depending in when the region is active, you could ask the function (use-region-p).