Fork me on GitHub

How can I redef a spec for a unit test?


I'm having a bit of trouble with deploying to clojars (windows install). When I ran lein deploy clojars it asks for credentials then I got:

Could not sign D:\home\devel\dithcord\target\dithcord-0.0.8.jar
gpg: no default secret key: secret key not available
gpg: signing failed: secret key not available
Tried to gpg --gen-key, filled in all the blanks, tried again, got:
Could not sign D:\home\devel\dithcord\target\dithcord-0.0.8.jar
gpg: cannot open tty `no tty': No such file or directory
However, I (mistakenly) did this in another project (had 2 windows opened) and it worked flawlessly. anyone know how I can fix this?


(ironically the project that works doesn't have org.clojars.username/project in the defproject)


I'm having some trouble chaining some functions together. I have a function that makes an API call, get's back the data and then calls another function, which uses FOR to clean up the response and pass the data onto a final function to do a batch insert into a database. If I call this on it's own it works fine, but if I try and run it in a function with the other 2 versions of this, I don't get any errors, I added a pprint to the start of each individual function so I can see they all run, but only the final function writes to the database.


@geek-draven can you show the implementation?


maybe run into some issues with lazy seqs not being realised.... however, just wildly guessing without having the code 😉


@nblumoe sure, this the workflow:


(defn insert-dashboard [data] (try (j/insert! db :ga_dashboard [:campaign :date :medium :source :adClicks :adCost :impressions :transactionRevenue :sessions :transactions :pageviews :bounces :sessionDuration] data) (catch Exception e (str "caught exception: " (.getMessage e))))) (defn clean-dashboard [data] (println "inserting data into dashboard") (for [idx (range 0 (count data))] (let [campaign (get-in (nth data idx) [0]) date (get-in (nth data idx) [1]) medium (get-in (nth data idx) [2]) source (get-in (nth data idx) [3]) adClicks (get-in (nth data idx) [4]) adCost (get-in (nth data idx) [5]) impressions (get-in (nth data idx) [6]) transactionRevenue (get-in (nth data idx) [7]) sessions (get-in (nth data idx) [8]) transactions (get-in (nth data idx) [9]) pageviews (get-in (nth data idx) [10]) bounces (get-in (nth data idx) [11]) sessionDuration (get-in (nth data idx) [12])] (insert-dashboard (conj [] campaign date medium source adClicks adCost impressions transactionRevenue sessions transactions pageviews bounces sessionDuration))))) (defn data-export [view-id metrics dimensions] (println "generating report") (->> (results view-id metrics dimensions) :records (map #(mapv :value %)))) (defn dashboard [] (-> (data-export view dashboard-metrics dashboard-dimensions) (clean-dashboard)))


then I'm calling the 3 like this:


(defn run-reports [] (dashboard) (Thread/sleep 5000) (vbc) (Thread/sleep 5000) (redirect))


Would be great if you could wrap the code in ` so that slack format that more nicely. Could you try if it works when you change (dashboard) to (doall (dashboard))?


You could also put a println into insert-dashboard to check if that actually gets called (I think it does not because the lazy seq returned by for in clean-dashboard does not get realised)


@nblumoe Thanks, that worked 👍 Also, thanks for the tip about ` - didn't know that either


Ok great. Is it clear to you why you ran into that issue? The side effects of inserting into the DB not being triggered within the for? And also why this would work when running (dashboard) on the repl?


Not really, I think I need to do some more reading up on lazy functions - as a rule I try and avoid for as it tends to cause issues


lazy sequence, I mean


@nblumoe after reading up on doall on clojuredocs, it seems clearer now - I assume that the first functions weren't actually completing as I was returning a lazy sequence, but by running doall, it forced it to complete


exactly. usually lazy seq behave as you would want them to as long as you are using them as values for something. However, as in your case, when you have some side effect (like inserting into a db, changing some state, printing, etc.) inside a lazy seq, the side effect might not happen at all and not completely (in the end depending on how much of the lazy seq actually gets realised “downstream”)


doall forces it to be realised. calling a function that returns a lazy seq on the REPL also realises it because the REPL then prints the seq


When using Clojure I would encourage you not to avoid lazy seqs at all, because they are actually very nice. You just need to be aware of dealing with side effects when needed and maybe be aware of infinite seqs (e.g. not printing them 😉 )


@nblumoe Thanks, that's really helpful


@nblumoe Clojure's the first language I've learnt and only been doing it for 18 months


btw what I proposed was a bit of a quick hack. in the end the cleaner solution would be to replace your for list comprehension with a doseq


Great choice for your first language. keep going! 🙂 I would like to encourage you to try to understand concepts that you encounter (like lazy seqs) in depth. In Clojure you don’t have that many core concepts that you need to learn about so this does not have to be intimidating or something like that


Hi, first of all, I want to state that I'm fairly new to clojure and I'm trying to learn as I develop my web app (and not really by only experimenting with repl). I'm trying to save data obtained from an API endpoint to a local MySql database. I have obtained the data from the API but I stuggle a bit with transforming the data into a data structure that my luminus web app accepts. The issue I have is that some db columns do not match 1:1 with the field names in the API data and that some expected fields are missing in the API data. The following code works IF the to-be renamed field is found in the data. Unfortunately certain fields are missing in some of the items in the collection. This is the problem I'm currently dealing with.

; all fields (keys) named missing and missing_description need to be renamed to is_lost and is_lost_description
(def renamedData (map
  #(postwalk-replace % discsParsed)
  [{:missing :is_lost, :missing_description :is_lost_description}])) 
A friend of my provided me with the following, partial solution:
(conj data {:is_lost_description "" :another_key "foo" :a_third_key "bar"})
However, I don't know, which keys are missing so I need to check that for each item in the collection. In PHP I would just have stored changes to a local temporary variable but afaik, this is not the way you should do things in clojure. Would appreciate some help, cheers! 🙂


@janimatti Maybe something like this could help:

(defn rename-keys
  "Rename keys in a map"
  [original new-names]
  (reduce-kv (fn [m k v]
               (if (new-names k)
                 (-> m
                     (assoc (new-names k) v)
                     (dissoc k))
Use it like this:
(rename-keys {:missing 88 :missing_description "oh no"} {:missing :is_lost, :missing_description :is_lost_description})
{:is_lost 88, :is_lost_description "oh no"}


thanks! I'll try it as soon as I have time for some clojuring! 🙂


hey, my google fu is letting me down — i’m finally exploring the world of macros a bit, and am wondering what the :forms parameter is that seems to be provided sometimes. where can i find more about it ?


@lmergen it's not really documented, but macros take two additional (silent) parameters, forms and env. env is a hashmap of locals that exist in the current context to Compiler internal objects that represent those locals. The values are mostly useless


Forms is the bare data handed to the macro, not the arguments but the form itself.


user=> (macroexpand '(defmacro foo [x y] nil))
(do (clojure.core/defn foo ([&form &env x y] nil)) (. (var foo) (setMacro)) (var foo))


hi, trying to learn.

clojure-noob.core> ((fn [v coll]
   #(assoc %1 %2 v)
   [:a :b :c])
{:a 1, :b 1, :c 1}
but this throws an exception
clojure-noob.core> ((fn [v coll]
    #(assoc {} %1 v)
 [:a :b :c])
ArityException Wrong number of args (2) passed to: core/eval1783/fn--1784/fn--1785  clojure.lang.AFn.throwArity (
even though this does not
clojure-noob.core> (#(assoc {} %1 %2) :a 3)
{:a 3}
why is that?


@sakalli that example with the ArityException using the function shorthand accepts one argument. The shorthand uses the highest argument (`%n`) to set the arity it expects.


@sakalli an example of not using the #() shorthand, which ignores the second argument:

((fn [v coll]
     (fn [arg1 _] (assoc {} arg1 v))
  [:a :b :c])


thanks for helping. admittedly still struggling. the @joshjones example evaluates to:

{{:a 1} 1}
rewriting the first example without the shorthand works as desired:
((fn [v coll]
    (fn [arg1 arg2] (assoc arg1 arg2 v))
    {} coll))
 [:a :b :c])
{:a 1, :b 1, :c 1}
if i redo what i did above without the shorthand it would be like this:
((fn [v coll]
    (fn [arg1] (assoc {} arg1 v))
 [:a :b :c])
I get the same ArityException but I was under the impression that both in the case with the shorthand and without i am passing only one argument to the anonymous function that should be also expecting only one. Am I getting this wrong? Could it be that assoc function is expecting three arguments and does not recognize the empty map as one?


@sakalli the reducing function must accept two arguments. assoc will take a map, a key, and a value. Your (assoc {} arg1 v} is nonsensical in a reducing situation since you are assoc'ing to an empty map every time.


ah.`{} coll` are for the reduce not for the anonymous, duh. might be getting there. thank you 🙂


gen-class v. reify. case: implementing alexa skills in clj, running on google app engine. you inplement a speechlet, then subclass SpeechletServlet, then, in you subclass's ctor, inject your speechlet into the super SpeechletServlet. you can do this using reify to create your speechlet, or you can genclass it. i'm guessing gen-class will give faster startup times, but i'm not really sure about the pros and cons in this case. which strategy is better?


when would seque be useful?