beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
grazfather 2021-05-09T13:26:56.425800Z

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

grazfather 2021-05-09T13:27:21.426100Z

(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 thing

yuhan 2021-05-09T13:45:09.426200Z

do you have the deinition of env-set! and some sample invocation of this function?

yuhan 2021-05-09T13:45:25.426400Z

I suspect it could be written in a more idiomatic way without use of atoms

Dave Suico 2021-05-09T14:05:02.429900Z

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?

NoahTheDuke 2021-05-09T14:10:49.430400Z

Maybe ask in #sql?

❤️ 1
alexmiller 2021-05-09T14:13:18.431Z

You can use fnil to patch the nil case

yuhan 2021-05-09T15:36:51.433700Z

yeah, but it looks like env in this case is local to the function and could be written as a loop binding

piyer 2021-05-09T15:41:23.433900Z

run it in parallel?

1
piyer 2021-05-09T15:42:01.434100Z

use core.async

2021-05-09T16:12:50.434300Z

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.

👍 2
grazfather 2021-05-09T16:13:11.434600Z

it’s not, we return it

grazfather 2021-05-09T16:14:45.434800Z

but I don’t want cons called at all if expr is nil

GGfpc 2021-05-09T19:51:05.442700Z

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?

phronmophobic 2021-05-09T19:57:04.443200Z

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.

phronmophobic 2021-05-09T20:02:36.443400Z

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.

sova-soars-the-sora 2021-05-09T20:10:03.444Z

This talk is amazing 😂 https://www.youtube.com/watch?v=6mTbuzafcII

GGfpc 2021-05-09T20:21:22.444300Z

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?

phronmophobic 2021-05-09T20:23:26.444500Z

I don't see how you could merge some-info and more-info without waiting until you have some-info and more-info. right?

phronmophobic 2021-05-09T20:24:48.444700Z

And that's per tv-channel, so it wouldn't prevent having multiple tv-channel lookups happening at the same time.

GGfpc 2021-05-09T20:31:58.444900Z

Oh I see, I was misreading your code. And in this case would http-call 1 and 2 run in parallel?

phronmophobic 2021-05-09T20:35:59.445100Z

not the way it's currently written. I couldn't tell if making the 2nd http call required information from the 1st http call.

phronmophobic 2021-05-09T20:36:28.445300Z

It would be an easy change to have them run in parallel if the two http calls are independent

seancorfield 2021-05-09T20:37:29.445900Z

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.

👆 1
1
grazfather 2021-05-09T22:21:08.446200Z

This is a great blog post about them: https://eli.thegreenplace.net/2017/reducers-transducers-and-coreasync-in-clojure/

👍 1