Is there an idiom for this sort of thing?
(if expr (cons expr rexprs) '())
Basically I am looping and do a first/rest, but in some cases I want the whole list again, if i just cons
the first and rest i can get a list with a list in it if the first was already empty(defn env-new
([] (env-new nil))
([outer] (env-new outer [] []))
([outer binds exprs]
(let [env (atom {:outer outer :data {}})]
;; (mapv #(env-set! %1 %2 env) binds exprs)
(loop [bind (first binds)
rbinds (rest binds)
expr (first exprs)
rexprs (rest exprs)]
(when bind
(if (= bind (symbol "&")) ;; Variadic arg
(env-set! (first rbinds) (if expr (cons expr rexprs) '()) env)
(do
(env-set! bind expr env)
(recur (first rbinds) (rest rbinds) (first rexprs) (rest rexprs))))))
env)))
Here’s the whole thingdo you have the deinition of env-set! and some sample invocation of this function?
I suspect it could be written in a more idiomatic way without use of atoms
Hi guys, I have 2.7k rows of csv data and I have function for reading and inserting rows into the database. The reading is fast but the jdbc/insert-multi! takes so long. The postgress database is in Ireland and I'm from Philippines, and now my client expects it to be able to insert those rows in less than a minute. Is there a better implementation for this to make the inserts fast?
Maybe ask in #sql?
You can use fnil to patch the nil case
yeah, but it looks like env
in this case is local to the function and could be written as a loop binding
run it in parallel?
use core.async
Make sure you’re using the right form of insert-multi!
. From the http://clojure-doc.org/articles/ecosystem/java_jdbc/home.html :
> If you wish to insert multiple rows (in hash map form) at once, you can use insert-multi!
; however, insert-multi!
will write a separate insertion statement for each row, so it is suggested you use the column-based form of insert-multi!
over the row-based form. Passing multiple column values to insert-multi!
will generate a single batched insertion statement and yield better performance.
it’s not, we return it
but I don’t want cons called at all if expr is nil
hello! I'm pretty new to core.async and I'm trying to build the following program. I have a list of tv channels and for each of these channels I will make an HTTP call that will return the programming for that channel. Then I want to make an HTTP call to another endpoint for each of the programs. Right now I'm mapping over all of the channels and making the request with http-kit and in the callback function I push the result to a core.async channel. Then I have a go-loop that listens from the channel and maps the programs in the response into another set of http calls that publish to a second channel in the callback method. Then I have another go-loop that listens from the second channel and compiles the results. The problem I find with this is that it seems like the logic flow is not much different from callbacks inside callbacks, which is something core.async is supposed to avoid. Can you guys give me some insight on what is the proper way to do this?
If you have some code, you can try posting in #code-reviews. From a design perspective, I would recommend working the problem with pencil and paper or on white board using whatever notation, pictures, or diagrams that feel natural. Basically, try to clarify how you think about the problem and then figure out how to best represent how you think about the problem in code.
From your description, each channel requires 2 http calls to gather the required information. I would try combining both steps into one:
(defn get-tv-info [tv-channel]
(go
(let [some-info (<! (http-call1 tv-channel))
more-info (<! (http-call2 tv-channel some-info))
all-info (merge-info some-info more-info)]
all-info))
Then you can use pipeline
or pipeline-async
to retrieve the channel info for each tv channel.This talk is amazing 😂 https://www.youtube.com/watch?v=6mTbuzafcII
Thanks! Your option seems interesting, but it seems like it blocks to wait for some-info and more-info to be finished before merging right?
I don't see how you could merge some-info
and more-info
without waiting until you have some-info
and more-info
. right?
And that's per tv-channel, so it wouldn't prevent having multiple tv-channel lookups happening at the same time.
Oh I see, I was misreading your code. And in this case would http-call 1 and 2 run in parallel?
not the way it's currently written. I couldn't tell if making the 2nd http call required information from the 1st http call.
It would be an easy change to have them run in parallel if the two http calls are independent
It takes a while to get your head around transducers but I find it amazing how much we use them at work these days: they're very powerful and can be much more efficient than combining multiple lazy sequence operations.
This is a great blog post about them: https://eli.thegreenplace.net/2017/reducers-transducers-and-coreasync-in-clojure/