Fork me on GitHub

hi i am trying to use tools.cli to parse from command line can anyone tell me why this doesnt work


I think that should be :id :test not :id test


I have an array of maps with two key-values. I want to group-by one of the keys and only retain the values of the other key. Eg [{:foo :bar :baz 42} {:foo :bar :baz 43}] => {:bar [42 43]}


It seems to me that I have to go from list to hashmap, back to list, and then back to hashmap.


Or am I missing something?



(->> [{:foo :bar :baz 42} {:foo :bar :baz 43}]
     (group-by :foo)
     (map (fn [[k l]] {k (map :baz l)}))
     (into {}))


what's the most minimal http server that clojure has? i am looking for an ad-hoc webserver i can launch in my test cases




hmmm i always figured that http-kit was quite feature-rich, but guess my use case works for that


it's the one with the fewest dependencies


hi everyone, what is the best approach to transfrom sql result sets (I'm using honeysql) to nested data structures (to represent relations), should I look at some libs ( for ex. or it is better to transform manually? for ex. I have this result set

({:account_id 1, :name "Name 1",  :chat_id 1}
 {:account_id 1, :name "Name 1",  :chat_id 2})
and I want
({:account_id 1, :name "Name 1", :chats [{:chat_id 1} {:chat_id 2}]})


andeee: clojure is good at this kind of things, I'd say manually. Depending on the client lib you're using you can do this very efficiently too (ex with squee)


I have this very ugly solution, can anyone help me to make it pretty?

(map (fn [v] (dissoc
              (merge (first v)
                     {:chats (reduce conj [] (map (fn [e] {:chat_id (:chat_id e)}) v))})
     (vals (group-by :account_id '({:account_id 1, :name "Name 1",  :chat_id 1}
                                   {:account_id 1, :name "Name 1",  :chat_id 2}
                                   {:account_id 2, :name "Name 2", :chat_id 2}))))
=> ({:account_id 1, :name Name 1, :chats [{:chat_id 1} {:chat_id 2}]} {:account_id 2, :name Name 2, :chats [{:chat_id 2}]})


ok, i have an interesting question: i have a function which accepts a certain url and makes a http post request to this url. how would you verify that the function is actually making the correct http call? as in, preferably i would launch an ad-hoc webserver, wait until a http request arrives, store the request, and shut down the server. are there any better ways to do this? personally, i don't like mocking things since you're missing important integration tests when you do that


@andfadeev: if you're using postgres, I'd use the array_agg function and handle this on the SQL call side.


honeysql looks to support SQL function calls:

(-> (select (from :foo) sql/format)
=> ["SELECT max(id) FROM foo"]


Working with deftype it seems that it does not provide the map->Foo function that defrecord does. Any reason for this?


@jethroksy: hm, thanks, I'll try!


@andfadeev: If your seq is order by :account_id:


@slipset deftype does not act like a map like defrecord (unless you provide that functionality). The implementation of the map-> function relies on the instance acting like a map and since that is not available with deftypes, the function is not constructed.


@lmergen: Check out clj-http's tests... It runs a jetty server on a strange port in order to do it's testing. I think http-kit does something similar as well.


i ended up writing a small macro, with-http-server


that just stores the last request's body in a dynamic var


WebAssembly might enable Clojure to replace ClojureScript in the future!!!


Not sure that I agree with uncle bob on this one. Clojure and C solve entirely different problems. Rust may replace some C in the future though.


it’s not about high/low level


it’s about “foundation” for something larger


Actually, it is about high/low level for many uses of C. Embedded apps, robot control, hardware drivers, etc. "Think small, think C" as a game developer I used to know liked to say. C isn't going away any time soon.


true, C is very strong in embeded


IMHO anything java-related / garbadge collected probably will have hard time on embeded


@crocket > WebAssembly might enable Clojure to replace ClojureScript in the future!!! do you have a link for that?


@crocket: yea, I saw that, but understood it as "no Clojure target platform" (at least yet). you read it differently?


I'm trying to use midje's provided feature to do some white-box testing of a piece of code. The problem I have is that the calls I would like to provide are called via map and midje hence actually never sees these calls. Any ideas (besides enforcing the realization via dorun in the code under test)?


As far as I can tell from the WebAssembly pages, it's C++ only at this point. They want to support garbage collected languages in the future, but mention no specific targets. I would think Java would be first among those, and that could be of use to the Clojure community. Seems like it's a long way off, though.


@nkraft: yep, that's the way I understood it as well


hmm, apparently, my midje question is a known issue:


hey guys, quick question, how to do multiple maybes in Schema? for example, i have a parameter for a function that could be either string or num, so i’d like to do something like x :- (schema/maybe schema/Str schema/Num) but it doesnt work of course


it could be nil or a string or a num? Then you'll want something like (schema/maybe (schema/cond-pre schema/Str schema/Num))


is there a mocking library for sql tests?


maybe takes a schema, so you need to describe what the non-nil values would look like in a single schema


tmtwd: when I was doing an sql lib that supported multiple backends, we used h2 with an in-memory only db for unit tests, and then used the real backends for the more complex integration tests. Of course you'll have engine to engine compatibility issues, but being able to test with in-mem h2 was nice




tmtwd: another benefit is h2 can be pulled in by lein, since it's a pure jvm db


yeah, I’ve used h2 before


@tmtwd: I usually use with-redefs and with-redefs-fn for mocking out functions (like SQL fns). If I have a more advanced mock, then I’ll put the with-redefs in a closure to call.


@crocket: The Robert Martin talk you're referring to seems to almost two years old, I wonder if he still thinks that clojure is the new C


@ghosss: thanks! yes i solved it by using cond-pre.


@tmtwd: this is how I do it: You can as well replace the sqlite connection by h2:mem like @noisesmith said. However, I would discourage everyone from using different databases in test and production as, well, they are different and you will hit errors sooner or later.


@lewix @crocket Martin’s talk makes sense if you look at macroscopic programming trends. His case is: * 30ish years ago, world dominated by many assemblies and hardware architectures: C generalized over them all. * Now the world is dominated by many VMs (Java, Ruby, Python, etc): Clojure is in position to be the general VM language.


He rambles around for the first 30 minutes, but it slowly builds to the 30-40 minute mark.


Right around 39 minutes he says to learn both C and Clojure for those two reasons.


“Who among us, 25 years ago, would have believed that the majority of enterprise software would be running on simulators?"


@jamesleonis: You're right, who would have thought that back then! I certainly didn't.


Hey all, is there any reason I couldn't use (comp) to compose my ring middleware components instead of the threading macro?


you can compose it in a way where (comp a b app c d) is a request lifecycle (`a` and b modify the request. c and d modify the response from app). A drawback is you cannot bail early in a or b (authorization for example)


yes, you can use comp. ((comp with-foo #(with-bar % :baz)) app) is of course the same as (-> app (with-bar :baz) with-foo)


Is there an idiomatic way to do (if test (do-a-thing-to thing-that-I-only-want-to-type-once-because-its-really-long-or-involved-or-something-but-I-don-t-want-to-assign-it-to-a-symbol-maybe-or-the-symbol-has-to-be-really-long) thing-that-I-only-want-to-type-once-because-its-really-long-or-involved-or-something-but-I-don-t-want-to-assign-it-to-a-symbol-maybe-or-the-symbol-has-to-be-really-long)?


why not a function definining that long-form that returns the symbol? (defn long-symbol [] :thing-that-I-only-want-to…)?


(Or a name for the thing, so I can use the terrible code I’ve written to do that and not call it if-or-noop which is a terrible name.)


I might not be passing in a symbol. I might want to define a big data structure inline to be processed (or fed through as-is) once.


unless I’m missing something, that’s just idiomatic programming in general…

// bad
if (a && b && c !== d || e && f || g) {

// better
function value() { return (a && b && c !== d || e && f || g) }
if (value()) {


basically dont define something huge inline, use a fn… or you can just use let to bind it once ahaid of time...


(let [big-thing (eval-the-data-struct)
  (if test big-thing)


I may have buried the lede here.


yea I dont think I understand.


I would like to put a data structure through a bunch of composed transformations that will either fire or not. While I could write (if test (transform a) a) every time, it seems like there might already be a macro or something for this use case.


(cond-> x test thenf)


Aha! Yes! That! I was a dumb, but now I am enlightened!


You are Extra Credit. Thank you!