Fork me on GitHub
#clojure
<
2015-10-09
>
ravicious07:10:50

hi, should I use (while true …) or (loop [] … (recur)) for an infinite loop? I think the main difference is the ability to end the loop, but since it’s supposed to be infinite…

jakub.janczak07:10:33

you could also use recur over a function calls

ravicious07:10:38

good idea, it could make the intention behind the code more visible

slipset08:10:04

Not knowing your problem, I find map over an infinite sequence more interesting simple_smile

jakub.janczak08:10:17

zatz even beta

nha09:10:24

Hello simple_smile I am looking for a way to measure response times of my compojure server. I am thinking about writing a middleware in it. Is there a clojure function for this kind of timers ?

pastafari09:10:28

nha: https://clojuredocs.org/clojure.core/time might be an option. wrap it in with-out-str, and send the string off to be analyzed.

jrychter10:10:47

I'm confused as to why transduce requires that the reducing fn also have an arity of 1. I thought the reducing fn would be identical to the one used with reduce.

robert-stuttaford10:10:06

jrychter: to produce the terminating value, i think

robert-stuttaford10:10:27

i remember Rich Hickey discussing this in one of his transducer talks - the internals one, i believe

robert-stuttaford10:10:01

it was something like that. i could be way off. i know there’s a good reason, though simple_smile

jrychter10:10:19

Hmm. It's difficult to find an example, because everybody uses + as a reducing function. I'm assuming the arity-1 function is supposed to just return the argument…

sander12:10:28

does clj have a builtin function or macro that acts like (fn [f v] (f v) v)?

sander12:10:51

i find myself writing things like (let [ch (chan)] (tap my-mult ch) ch) often

Ben Kamphaus12:10:07

@jrychter: see “Creating Transducers” here. partition-all is an example that has to flush its intermediate collection.

Ben Kamphaus12:10:55

that section also points to [`completing`](http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/completing) which will add a completing arity to a reducing fn without one, default case being nothing to be done (identity).

Alex Miller (Clojure team)12:10:55

it's really designed for calling side-effecting interop calls, but would work here too I think

sander12:10:49

@alexmiller: thanks! tap has side effects so it fits well simple_smile

Alex Miller (Clojure team)12:10:11

(doto v f) is what you have above for example (invokes (f v) and returns v)

Alex Miller (Clojure team)12:10:27

(doto (chan) #(tap my-mult %))

Alex Miller (Clojure team)12:10:48

would be the equivalent to yours - it threads the first value into the first arg position like ->

Lambda/Sierra12:10:54

@jrychter, @robert-stuttaford The reducing function arities are `(fn ([] ...return initial value...)

Lambda/Sierra12:10:06

(fn
  ([] ... return initial value ...)
  ([accumulator]  ... complete the accumulated value ...)
  ([accumulator input]  ... add input to accumulated value ...))

jrychter13:10:28

Ok, I see now. The documentation for transduce could use @stuartsierra 's example.

robert-stuttaford14:10:30

thanks for the ping!

bronsa16:10:13

@jrychter: an example would be a reducing function building a string and internally using a StringBuffer during the reduction as a performance enhancement, (StringBuffer.) in the 0-arity, (.toString acc) in the 1-arity and (.append acc i) in the 2-arity

fdserr16:10:00

clojure.data/diff : has someone come up already with a way to get all the paths that have changed, like [[:here :there 5][;here :too][:there :so 4 4]] ?

ghadi19:10:39

It kind of upsets me how many tools/products still configure using their code DSL rather than just shipping data. Totally prevalent

tdantas22:10:49

I'm feeling dirty 😞 seems like I'm punching clojure's face 😞 could anyone tell me if the code below is idiomatic or not

(defn chain [context funcs success-handler error-handler]
  (let [chain (apply comp (reverse funcs))]
    (try (success-handler (chain context))
    (catch Throwable e
      (error-handler e)))))

(defn create-account-success [result]
  {:body result :status 201})

(defn create-account-error-handling [params]
  (println (str "failed " params))
  {:body "Create accounts failed !" :status 400})

(defn create [account]
  (chain account
    [ account/validate
      account/create<!] 
      create-account-success
      create-account-error-handling))

tdantas22:10:04

I will export the chain function outside

tdantas22:10:41

create is a ring handler