Fork me on GitHub
Jason Lee07:06:14

Does someone know how to refer a spec def from another namespace?


You need to ensure the other namespace is loaded by requiring it, after that its specs can be accessed as usual using qualified keywords


what am i doing wrong where by the case doesn't match on the class in the second example?

(= clojure.lang.PersistentVector (type []))
=> true

(case (type [])
  clojure.lang.PersistentVector "vector"
=> "unknown"


in case test-constants are not evaluated and clojure.lang.PersistentVector is a symbol in case context


it is better to use cond or condp when you want dispatch by type

(condp instance? [1 2 3]
  clojure.lang.PersistentVector "[]"
  clojure.lang.PersistentHashMap "{}") 
something like this


that's interesting, thank you very much for explaining


also case is a macro so you always can use macroexpand to see it’s true form 🙂


(case (symbol (str (type []))) ...)
should work as well


Cool trick, didn't think about it)

Timofey Sitnikov11:06:36

Good morning, I am looking at, and I cannot figure out this line: (map->SystemMap (apply array-map keyvals)) , how is it that the map->SystemMap is a function? I thought that -> was a threading operator. If I do this:

Clojure 1.10.0
user=> (type map->SystemMap)             ; does not exist in this ns
Syntax error compiling at (REPL:1:1).
Unable to resolve symbol: map->SystemMap in this context
user=> (defrecord SystemMap [a b])
user=> (type map->SystemMap)              ; now it exists, how?
user=> (type map->)
Syntax error compiling at (REPL:1:1).
Unable to resolve symbol: map-> in this context
user=> (macroexpand map->SystemMap)
#object[user$eval138$map__GT_SystemMap__158 0x4e25147a "user$eval138$map__GT_SystemMap__158@4e25147a"]
Where can I read about the map->?

nmkip11:06:13 > when a defrecord Bar is defined a corresponding function `map->Bar` is defined that takes a map and initializes a new record instance with its contents (versions 1.3 and later only)

Darin Douglass11:06:29 > Given (defrecord TypeName ...), two factory functions will be defined: ->TypeName, taking positional parameters for the fields, and map->TypeName, taking a map of keywords to field values.

Timofey Sitnikov11:06:46

OK, thank you @juan.ignacio848 and @ddouglass, it simply constructs an object of the type of defined defrecord , like so:

user=> (defrecord SystemMap [a b])
user=> (def mm (map->SystemMap {:a 10 :b 20}))
user=> (println mm)
#user.SystemMap{:a 10, :b 20}
Would it be correct to say that map->SystemMap is a macro? Or is it an actual function. Becasue macroexapnd outputs: #object[user$eval137$map__GT_SystemMap__157 0x35d3ab60 "user$eval137$map__GT_SystemMap__157@35d3ab60"] So does that mean its a macro?


That I don't know 🙂 > I thought that `->` was a threading operator. Yes, -> that's one of the threading macros. But try this on your repl: (defn foo->bar [x] x) -  and `>` , among others, are characters that are valid in function names

Timofey Sitnikov11:06:09

That is what I have been suspecting but was not sure. For that reason, I was making an assumption that it is simply a macro.


is there a way to capture current let bindings and automatically cast them into a map?


I am currently getting {:lang "fr"} with the following destructuring: [{{:keys [params]} :request}] how can I destructure it further to only get fr?


this might work:

[{{{lang :lang} :params} :request}]
typically, I don’t use destructuring if the nesting is more than 1 level. I find the following more readable: (-> request :params :lang)


or even (get-in request [:params :lang])


Thank you so much, the first one worked


Just confused how come :keys is not needed 😐


[{{{:keys [lang]} :params} :request}] would also work


it’s just an alternative way to write it


Got it, thanks


I only use destructuring minimally , so might be a better resource

🙏 4
Phil Hunt18:06:12

and learned a bit about Luminus's innards in the process


If I have :foo {:extra-deps <some-jar> :main-opts ["some command"]} in aliases, clojure -R:foo -M:foo should work correct?

Alex Miller (Clojure team)20:06:17

oh, you have a jar file

Alex Miller (Clojure team)20:06:48

should then be :foo {:extra-deps {:local/root "path/to/file.jar"} :main-opts ["some" "command"]}


thanks for confirming, now to check why isn't working

Alex Miller (Clojure team)20:06:07

what's the error and what's your command in main-opts?


sorry yeah, bad command line args


Hey guys, what I missing here: I am using bidi router and I have this handler:

(defn index-handler [request] (res/response request))
And this middleware but x-bob is not appended to the output of the above:
(defn wrap-lang [handler]
  (fn [request]
    (let [response (handler request)]
      (assoc-in response [:headers "x-bob"] "Robert"))))
Any idea why?


@somedude314 all I can think is you aren't using the handler that your middleware created

(defn wrap-lang [handler]
  (fn [request]
    (let [response (handler request)]
      (assoc-in response [:headers "x-bob"] "Robert"))))
(defn handler [request]
  {:code 200 :headers {} :body "OK"})
(ins)user=> (def h (wrap-lang handler))
(ins)user=> (handler {})
{:code 200, :headers {}, :body "OK"}
(ins)user=> (h {})
{:code 200, :headers {"x-bob" "Robert"}, :body "OK"}


Sorry totally confused here. How can I not be using it when I have it listed like this:

(def app
  (-> handler

(def reloadable-app
  (wrap-reload #'app))

(defn -main [& _args]
  (run-server (if dev? reloadable-app app) {:port 8080}))


wrap-strip-trailing-slash is working fine for me


it could be that some other middleware is clobbering :headers - but that wouldn't be the case if you are using the ring versions of those handlers


I can't explain your bug, but I can say your code works as it should :D


Thanks 🙂 I will see if I can figure it out


@U051SS2EU I had to change my middleware to:

(defn wrap-lang [handler]
  (fn [request]
    (handler (assoc-in request [:headers "x-bob"] "Robert"))))


oh you meant to change the headers coming in, rather than the response? weird


Lol. Sorry. Sort of. I only modified :headers as a test, not thinking it mattered. What I am really trying to do is injecting the current language which sometimes come from the URL prefix and sometimes from the logged in user in the context variables to pass it to my templates.

Rob Aguilera23:06:05

Hello all, working my way through some clojure exercises and I'm a little confused on what I'm getting back. I'm given a string ACGTGGTCTTAA and I need to replace each letter with a specific value. After I split it, and replace the values I get the following vector below. I need to join it back together but I'm confused on a couple of things. 1. Why is the vector itself surrounded in "" 2. What are the \ characters behind and in front of each letter mean?

;; => "[\"U\" \"G\" \"C\" \"A\" \"C\" \"C\" \"A\" \"G\" \"A\" \"A\" \"U\" \"U\"]"
I'll place my code in a thread if that helps.

Rob Aguilera23:06:36

(defn to-rna
  (let [dna-split (str/split dna #"")
        rna-split (str/replace dna-split #"C|G|A|T" {"C" "G" "G" "C" "A" "U" "T" "A"})]


@U011DBXN8DU That can't be your actual code -- your let has no body so it will return nil.


I suspect you're just calling str on rna-split which produces a string that looks like it has a vector inside it.


you are calling str/replace on something that is not a string


Ah, I see where it's going wrong... dna-split is a sequence of strings, not a string, so when you call str/replace on it, it's getting converted to a string

😁 8

I don't think you need that first step at all -- just call str/replace on the dna string you are passed in.

Rob Aguilera23:06:10

Ah, I didn't have to split it in the first place.


user=> (defn to-rna
         (str/replace dna  #"C|G|A|T" {"C" "G" "G" "C" "A" "U" "T" "A"}))
user=> (to-rna "ACGTGGTCTTAA")

Rob Aguilera23:06:44

Sorry one more question, is that the best way to go about replacing those values? I find it a little hard to read.

Rob Aguilera23:06:05

At least the map at the end {"C" "G" ...}


would you find it easier to read if you separated each pair with a comma?


{"C" "G",  "G" "C",  "A" "U",  "T" "A"}

👍 4
Rob Aguilera23:06:25

Yup that helps. Forgot that Clojure ignores commas. Thanks @U04V70XH6


I never think of using a map or function in the third arg of str/replace so that's quite elegant...

Rob Aguilera23:06:47

The commas definitely do help.