Fork me on GitHub
#clojure
<
2016-04-04
>
tungsten00:04:39

hi i am trying to use tools.cli to parse from command line can anyone tell me why this doesnt work https://gist.github.com/anonymous/699b95cd77d3c6503d44a1b67b0945cc

seancorfield01:04:51

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

grav09:04:04

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]}

grav09:04:03

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

grav09:04:28

Or am I missing something?

grav09:04:48

Eg

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

lmergen10:04:11

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

dm310:04:01

http-kit?

lmergen10:04:58

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

dm310:04:43

it's the one with the fewest dependencies

andfadeev10:04:40

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. https://github.com/friemen/aggregate) 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}]})

mpenet11:04:31

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)

andfadeev12:04:28

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))})
              :chat_id))
     (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}]})

lmergen12:04:22

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

jethroksy12:04:37

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

jethroksy12:04:34

honeysql looks to support SQL function calls:

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

slipset12:04:47

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

andfadeev12:04:08

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

rauh12:04:47

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

Alex Miller (Clojure team)13:04:45

@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.

jstew13:04:22

@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. https://github.com/dakrone/clj-http/blob/master/test/clj_http/test/core_test.clj

lmergen13:04:55

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

lmergen13:04:11

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

crocket14:04:50

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

jstew14:04:19

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.

fxposter14:04:08

itā€™s not about high/low level

fxposter14:04:20

itā€™s about ā€œfoundationā€ for something larger

nkraft14:04:40

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.

syzer14:04:27

true, C is very strong in embeded

syzer14:04:33

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

tolitius14:04:51

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

tolitius14:04:04

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

schaueho15:04:29

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)?

nkraft15:04:50

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.

tolitius15:04:41

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

schaueho15:04:05

hmm, apparently, my midje question is a known issue: https://github.com/marick/Midje/issues/332

lumengxi17:04:56

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

ghosss17:04:57

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))

Tim17:04:05

is there a mocking library for sql tests?

ghosss17:04:10

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

noisesmith17:04:03

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

Tim17:04:29

interesting

noisesmith17:04:42

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

Tim17:04:55

yeah, Iā€™ve used h2 before

jamesleonis17:04:58

@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.

lewix18:04:33

@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

lumengxi18:04:34

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

sveri18:04:15

@tmtwd: this is how I do it: https://github.com/sveri/closp/blob/master/resources/leiningen/new/closp/test/clj/db/user_test.clj 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.

jamesleonis19:04:18

@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.

jamesleonis19:04:55

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

jamesleonis19:04:51

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

jamesleonis19:04:25

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

nkraft19:04:00

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

timgilbert21:04:49

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

jr21:04:43

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)

tomjack21:04:14

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

j-po23:04:46

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)?

lwhorton23:04:51

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

j-po23:04:43

(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.)

j-po23:04:18

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.

lwhorton23:04:39

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()) {
}

lwhorton23:04:14

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

lwhorton23:04:30

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

j-po23:04:19

I may have buried the lede here.

lwhorton23:04:30

yea I dont think I understand.

j-po23:04:01

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.

tomjack23:04:42

(cond-> x test thenf)

j-po23:04:45

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

j-po23:04:11

You are Extra Credit. Thank you!