Fork me on GitHub

Thanks 🙂 there's no conclusive argument in that discussion as to why this shouldn't work, so I'll open a JIRA ticket


@pferron I cannot imagine it will be changed. You'd have to make a good case that changing the behavior would solve a real world problem that people actually need solving.


Does this mean that warts are generally left unfixed? ->> doesn't behave according to its docstring, but of course it's possible to work around it so I don't think anyone is blocked by that


I don't think it would be accurate to say "warts are generally left unfixed". I think it is taken on a case by case basis. Given the desire to maintain backwards compatibility, changes with perceived low benefit are correspondingly low in priority to change.


In this case, do you actually have a use case where it would be important for your example expression to work?


You may find that consistency between these two forms is considered more important (and perhaps the ->> docstring should just be updated to be more specific):

user=> (-> (constantly 1) ())
Syntax error (IllegalArgumentException) compiling at (REPL:1:1).
Can't call nil, form: (nil (constantly 1))
user=> (->> (constantly 1) ())
Syntax error (IllegalArgumentException) compiling at (REPL:1:1).
Can't call nil, form: (nil (constantly 1))


I had a ->> pipeline where at some point the value was a function. The next step in the pipeline was to call this function, and then continue the pipeline with the return value.


By all means, go ahead and create an issue in JIRA -- we're just setting your expectations that changes to core are generally few and far between.


Yeah, and I do appreciate the focus on stability and backward comp 🙂


That's why I've been comfortable running prerelease builds of Clojure in production for eight years 🙂


Actually I also wish that apply would accept taking only a function as arg so I could just write apply instead of ()


So many core changes that won't happen 😄


Why would you want to write (apply f) when you can just call (f) directly in that case? In general, you're going to have (apply f coll) and coll may be empty...


(genuinely curious)


I meant in the previous example that currently doesn't work:

(->> (constantly 1)


My next attempt was:

(->> (constantly 1)


But that doesn't work either because apply wants a coll of args


The cool thing with the second version is that it would work both with -> and ->> and is arguably more readable as well


Are you familiar with as-> ? It can be useful to change the position at which each expression appears in the following expression, rather consistently the first position like ->, or consistently the last like ->>


I had forgotten about as->. It works fine for calling a function in the middle of the pipeline:

(as-> (constantly 1) x
so it's a nice alternative even though it's not quite as concise. Thanks for bringing it up 🙂


(-> (constantly 1)
    (as-> f (f)))
It's designed to be used inside -> hence the "weird" order of arguments (expression, symbol, body)


I'd probably define a helper function for use in situations like this

(defn call [f] (f))

(-> (constantly 1)


Oh, thanks for the tip about using as-> inside the ->, the arg order definitely makes more sense this way!


Can do

(-> (constantly 1)
    (apply []))
as well


hello, i have this line that gives me reflection warning on lein check: (if (empty? (.getAllFrames desktop))) i can solve this with type hinting: (.getAllFrames ^javax.swing.JDesktopPane desktop) my question is basically how to pack this properly into a macro that takes as arguments the method & object and returns the above line..i'm slightly(understatement) confused with quotes backquotes etc..


(set! *warn-on-reflection* true)

(defmacro string-method
  [obj method-name & args]
  `(. ~(vary-meta obj assoc :tag `String) ~method-name ~@args))

;; warnings
(fn [s] (. s endsWith "a"))

;; no warnings
(fn [s] (string-method s endsWith "a"))


thanks, i hadn't seen this so question no


thanks for the macro too, looks like what i was aiming at wasn't so simple 🙂


@sb Ctrl-shift close paren

👍 4

@borkdude Yes, I see. at this moment I deleted. thanks 🙂


Hi, How to convert the below java code to clojure: private final static Logger LOGGER = Logger.getLogger(<class_anme>.class.getName());


@sreerama.naga probably something like this (Logger/getLogger (.getName TheClazz))


In this TheClazz give compilation error.


replace TheClazz with the class which you wanted to use in your specific case


thank you for your response. can we add an alias to it similar to LOGGER in the java code? if yes how can we do it.


@sreerama.naga yes: (ns your.namespace (:import [java.blablabla.bla Logger]))


thank you for response. Here I mean like an Object in java private final static Logger LOGGER = Logger.getLogger(<class_anme>.class.getName()); here we create LOGGER reference and we use where ever we refer methods/functions in it. similarly can we have a reference created similar lines in clojure. Below solution compiles fine. but I do not have any reference to use methods of the created java object. (Logger/getLogger (.getName Class))


if you give me the exact java code, I can translate it to Clojure for you


I think that perhaps @sreerama.naga is simply looking for how to define a var called LOGGER with that value. In that case it would be:

(def LOGGER (Logger/getLogger (.getName TheClazz))


or maybe @sreerama.naga is coming form Java and is trying to translate his knowledge to Clojure code? If this is the case, have a peek at


@UEH6VEQQJ thank you. I was looking this way.. But however I realized it wont be sufficient for me to proceed further.


Sorry if my post is not clear to you. @UE21H2HHD Infact currently we use tools.logging, but the concern for me here is, it chooses either slf4j or log4j or util logging based on the logging available for it at runtime. As our large project the underlying runtime may vary due to different 3rd party libraries we use. 1) So here we want to restrict it to use only java util logging always. 2) Plus we also want to enable package level logging at runtime. So to achive above 2 points, I was trying to do like the way we do in Java as below. Class A{ //In Java the below like creates logger for this class and hence we can the logger at runtime and can change the log levels. java.util.logging.Logger LOGGER = java.util.logging.Logger.getLogger(A.class.getName()); //where ever I want to log I use"Failed to trigger notification"); //info logging LOGGER.fine("Failed to trigger notification"); //fine logging } any thoughts and solution to achieve this beahaviour in clojure?


I don’t know if I am helping here but: I think allows you to force which logging implementation is chosen? Also by default, uses the namespace as the logger.


tools.logging chooses the logging whether slf4j, log4j or util logging based on the runtime which was available in the path. I am not aware how we can force it to use only util logging.. any suggestion or guidance to overcome the above said 2 points will greatly help me.


In our case we have slf4j, log4j and other logging as it comes from other different libraries which we can not delete or avoid.


Are you using logback to unify the various logging technolgies in use?


Some people also find unilog helpful


It is a large project and mix of all the loggins but at the end all of them will write to the util logging way.. So for clojure it is not happening to consider only util logging in this case.


ah ok, so for legacy reasons you can’t introduce tools like logback which help to amalgamate various logging tech?


yah.. we can not introduce new logging now as every other logging was already tried and we encounter different issues, as we use 3rd party components and which depend on different log dependencies. Importantly the package level logging is most required feature we should always keep as it is. So keeping all these in the back of mind we want to go/force util logging which is more effective and proven for our case.


Thank you for the link. let me try..


@Iread this does not work as it is. I am using tools.logging-0.2.6.jar, and some of the packages as shown in the above link does not exists as it is in 0.2.6 version. But I tried different options it doesn't work. May be I am missing something.


oh my goodness tools.logging-0.2.6 was released in feb 2013. Are you unable to upgrade any libs?


At this moment I do no think so. I need to make this work with this as it is until I have very strong reason to change to new library..


I expect you noticed I linked to the stackoverflow question rather than the answer?


any thoughts to make this to work with current libraries ?


I’m sure you have good reasons to not upgrade libraries but I am sure it gives you many headaches?


@Iread, it is like, make it work as it is instead of inviting new issues by upgrading based on the current criticality.


I’m sorry, I can’t help you here. It does not mean that you cannot do what you want to do, it just means I’m not the guy to help you figure out how to work with old libraries. I sincerely wish you the best of luck.


@Iread solution in above url works perfectly. 👍 thank you all for your help.


That’s great @sreerama.naga, thanks for letting me know of your success!


another small victory: i was able to separate everything out in my little project. prior i had everying in core.clj to POC things and was able to get everything running with proper namespaces. i have been using metabase as a guide on project structure so if anyone has an additional examples i would really appreciate it! curious what is the “correct” directory structure for clojure projects. coming from a spring background things have sub directions like controllers, domain, service etc


If I understand it correctly you’re generally discouraged to organise by type and instead organise them by domain. So instead of: x.controllers.login and x.service.login, you would have x.login.controller and x.login.service.

Steve Hobbs16:09:59

Hi all, as a beginner to Clojure I would be interested to see if there’s a more idiomatic way to write this code. It basically takes a Json Web Token and converts the claims within to a Clojure map. It uses the Auth0 java-jwt library to parse the token. .getClaims returns a Map<String, Claim>. to-map attends to take this Java map and return a Clojure map, converting the k to a keyword and retrieving the value of the claim. map-claim attempts to retrieve the value of the claim. asString is called, which returns nil if the claim value is not a string. This naive implementation then tries to call asInt in that case. Would love to see how this could be improved. I’m sure into could be used but I can’t quite get the syntax.


looks good to me! :thumbsup: into doesn’t really help here unfortunately since you want to convert stuff before creating the map anyway (otherwise you could do (into {} your-java-map). to-map as you have written in will return a list of maps, with one token per map. if you intended to get a single map with all the tokens, then

(into {} (for [[k v] hash] [(keyword k) (map-claim v)]))
will get you there!


map-claim can be written more concisely like this:

(defn- map-claim [claim]
  (or (.asString claim) (.asInt claim)))

Steve Hobbs17:09:25

Thanks @schmee ! Just the kind of feedback I was looking for 🙂


Could someone be kind enough to help me eliminate the loop/recur in this function? I've been trying unsuccessfully for awhile and expect it's probably simpler than I'm making it.

(defn author-by-name [name authors]                                          
  "given a string and set of author maps returns map if author is present"          
  (loop [name name                                                           
         authors authors]                                                    
    (if (= name (:name (first authors)))                                     
      (first authors)                                                        
      (recur name (rest authors)))))


I feel like I've grown paranoid of using loop/recur after some advice I've gotten here saying you can almost always use some built in core sequence functions instead.


I've also read the same in some Clojure books (I think maybe Programming Clojure) but I'll be honest I find loop/recur much easier. But I want simple instead right?!


So the name parameter is a string of an author and authors is a set with different author maps that have a :name "name" key value pair inside. I need to match the input name with the value "name" and then return the whole author map.


and in general, what would you put in the doc string to explain that expected input and output?


Plus is it a "code smell" to be using first and rest when looping or iterating through a set? It feels a little wrong because they are unordered for some reason.


Doesn't that code have an infinite loop if there are no matching entries for the name?


As far as a non-loop way, filter could be used to return a sequence of only those elements of authors that return true for a function you pass to filter. That could return a sequence of all matching elements of authors.


filter is lazy, so if you want to guarantee only one return value, call first on the result. The filter function will stop doing work as soon as the first match is found, if any.


e.g. (filter #(= name (:name %)) authors)


Or if you prefer a slightly longer form:


(filter (fn [author] (= name (:name author))) authors)


Awesome! Thank you! Yeah I've definitely been trying with filter but just wasn't quite getting the full author map out of the set.


You know it's funny the exercise gave a hint of "remember first" and that's what got me on that weird loop/recur with calling first on a set and such. But your sequence output is why!


And not realizing the infinite loop was embarrassing but I just today started playing around with a new editer/repl combo and was thinking I just didn't know where to see my nil output. Of course it was user (me!) error as it was spinning it's wheels in the background in an infinite loop


filter always returns full 'elements' out of its input sequence, so I don't see how you could try to use it and not get full author maps out.


keep is different than filter in this regard.


I guess I should clarify: If you get anything out of filter, then it should always be full elements from the input sequence. It is quite easy to call filter with a slightly wrong predicate function and get the empty sequence back.


ahhh, this is all good stuff. The real answer is I just suck at this still (but love it!) and just flounder away at the repl hoping to stumble onto the right solution. I think at one point I had done `(map :name authors) so had a sequence of all the name strings and was comparing but then was like, but wait, how do I get the map now? So yeah, I suck. hahaa


No worries. A new style of programming can take a lot of getting used to, and practice.


Definitely, just programming in general. I technically know more clojure and can do far more with it than any other programming languages.


You now have that bit of experience that should remind you in a future similar problem: "If I want the original full elements of some collection as part of my output, map won't do it, because it calculates new replacement things and returns those"


very true. I've learned so much more in the last few weeks than I have in the last year I think just flitting around. I spent a lot of time trying beginner tutorials in various languages and projects before finally settling down and consistently trying to solve problems every day. I've always been drawn to Clojure despite my struggles so decided to just stick with it until it clicks.


What is the generally idiomatic way in clojure to achieve what people use so-called “algebraic data types” for in static-functional langs like Scala/Rust/Haskell, where you want to model data as being one of multiple cases? For example like (pseudocode):

enum Auth {
  UsernamePassword(username: String, password: String)
  Token(token: String)
Would you typically use a map with a :tag keyword like
{:tag ::username-password ::username "bob" ::password "pw" }
{:tag ::token ::token "123" }
Or just never mind with the tag at all, and just pass in the fields?


If the set of fields is non-overlapping, I probably wouldn't bother with a tag and would just use a plain hash map.

👍 4

(if-let [token (::token auth)]
  (,,, deal with token-based auth ,,,)
  (,,, deal with credentials-based auth ,,,))