Fork me on GitHub
#beginners
<
2022-03-23
>
fabrao00:03:00

hello, how to solve this -> [[a b c] [[d e] [f g]]] to [[a b c] [d e] [f g]]

Dustin Paluch00:03:47

you’re missing a paren in the first one

fabrao00:03:18

sorry, I fixed

Dustin Paluch01:03:38

(def foo '[[a b c] [[d e] [f g]]])
(apply vector (first foo) (second foo))

fabrao01:03:08

if I have more than only 2 elements?

fabrao01:03:38

(def foo '[[a b c] [[d e] [f g]] [[i j] [k l]]])

ahungry03:03:28

(reduce #(if (coll? (first %2)) (apply conj %1 %2) (conj %1 %2)) [] foo)

ahungry03:03:02

or if you prefer slightly more readable:

(reduce (fn [xs x] (if (coll? (first x)) (apply conj xs x) (conj xs x))) [] foo)

👍 1
fabrao11:03:02

It worked very well

fabrao11:03:07

thank you

👍 1
oxalorg (Mitesh)20:03:39

(def foo '[[a b c] [[d e] [f g]] [[i j] [k l]]])

(into [(first foo)]
      (mapcat identity (rest foo)))
;; => [[a b c] [d e] [f g] [i j] [k l]]
Maybe this works too?

popeye11:03:40

@U031CHTGX1T Thank you for the response

popeye11:03:54

But where to add this in above function?

Ferdinand Beyer11:03:30

You don’t, this is not meant to be defined per call, it’s a global setting

popeye11:03:02

hmmm.. is it like add it in configuration file ?

Ferdinand Beyer11:03:12

Consider using async/pipeline to use dedicated threads and where you can specify the parallelism

Ferdinand Beyer11:03:48

You can specify that when starting the JVM (`-D` flag I believe)

popeye11:03:02

probably on project.clj ?

Ferdinand Beyer11:03:29

:jvm-opts ["-Dclojure.core…=X"]

Ferdinand Beyer11:03:59

But it depends where you deploy your app as well

Ferdinand Beyer11:03:19

E.g. I think you will not run it in Leiningen in prod right

popeye12:03:31

i have to use in prod

popeye15:03:07

Can we change the value of clojure.core.async.pool-size at runtime? I mean I do not want to set it in project.clj or as in system property

bridget15:03:12

I believe the answer to this is no. It's a jvm system property. But, also, it wouldn't be a good idea, because it could lead to unpredictable behavior. My recommendation is to go ahead and set the pool-size to a higher value (if that's really what you want to do), as pool size is something giving threads at disposal. But you should likely make sure that setting the pool size to a higher value is really what you want to do - if it will actually solve your problem. I would do a lot of experimentation first.

popeye15:03:31

thanks for the response @U0G3Y3NA0, Is there any other property where we can increase or decrease threads in runtime?

delaguardo15:03:54

what is the problem you are trying to solve?

upvote 1
delaguardo15:03:22

this var is private probably for a good reason. As you can see in the docstring there are some problems you may encounter trying to change the value

popeye15:03:39

by default it will be 8 right? So what we can do if we want some higher number of threads when we running below code

(->> data
       (fn []  (.....transformation logic..))
       (async/merge)
       (async/reduce conj [])
       (async/<!!))

Ferdinand Beyer15:03:32

@U01J3DB39R6 — I think async/pipeline is what you really want. The async “dispatch” thread pool is for go blocks only, e.g. small non-blocking things. Unless you have a machine with A LOT of cores, changing this will probably not give you much. pipeline and friends allow you to use dedicated threads where you can easily tune the number of parallel tasks to run.

upvote 2
Ferdinand Beyer15:03:28

Since you are asking for the number of threads, it seems you want to do computation expensive or even blocking operations, in this case you should not use the dispatch pool anyway

popeye16:03:27

Thanks for your response @U031CHTGX1T, But I did not get what you meant by dispatch here

delaguardo16:03:56

https://eli.thegreenplace.net/2017/clojure-concurrency-and-blocking-with-coreasync/ here is a good article explaining what is thread-pool in core async and how it is connected with blocking go blocks

👀 1
Ferdinand Beyer16:03:08

Probably explained in the article @U04V4KLKC shared. “dispatch thread pool” is just the name of the fixed-size thread pool that can be adjusted with the pool size property

emccue06:03:49

I would recommend https://github.com/clj-commons/claypoole if you want too customize an executor of sorts

Ferdinand Beyer18:03:25

Sorry, I don’t understand your question. Thoughts on what?

Noah Bogart18:03:01

Not sure where to ask this, but is there any news about https://clojuredocs.org/ ? It hasn't had any code changes since last August, there are a bunch of issues and pull requests that haven't been touched in that time, and it hasn’t updated to include any changes to core libraries (core.async for example), let alone clojure 1.11 updates.

oxalorg (Mitesh)20:03:15

I was wondering the same thing! 😿

Noah Bogart20:03:25

@U4J1HQKFH hey there, any news?

andy.fingerhut01:03:35

I mean, Clojure 1.11 was just released days ago. Give them a bit of time 🙂. OK to ask, of course.

🙌 1
👍 1
Noah Bogart01:03:32

Hah that's true, but it seemed prudent to ask given that no other work has been done on the site since last year either.

Benjamin18:03:23

is there a way to enable me to say ::foo/key without loading the foo namespace, I just want the key ns sugars. And maybe I can't load foo right now

Alex Miller (Clojure team)18:03:30

in Clojure 1.11 you can now use (:require [the.foo :as-alias foo])

👍 1
Alex Miller (Clojure team)18:03:08

pre-1.11 you could do

(create-ns 'the.foo)
(alias 'foo 'the.foo)

👍 1
Alex Miller (Clojure team)18:03:46

(and yes, it's ok to later load the.foo if you need to)

👍 1
dpsutton18:03:08

I’d also question if you need auto resolve keyword namespaces at all. Sometimes the answer is yes, but often the answer is :foo/key is just as valid

oddsor19:03:09

I thought :foo/key would be correct in this case? ::foo/key is necessary when you alias/shorten a namespace into foo

Sam Ritchie23:03:39

I have found it is a nice catch for typos (at least in the namespace piece); also, for these java-ish long namespaces like :sicmutils.expression.simplifier/option

Prithvi Hv20:03:13

i been using calva as my repl development tool. I'm able to load and execute imports and funcs i have written, but unable to load the whole namespace or file. Can someone point out what im doing wrong?

Prithvi Hv20:03:17

groww-scraper.core=> (load-file "src/groww_scraper/core.clj")
((require (quote [clj-http.client :as client]) (quote [cheshire.core :as json]) (quote [environ.core :refer [env]]) (quote [clj-time.core :as t]) (quote [clj-time.coerce :as tc]) (quote [next.jdbc :as jdbc]) (quote [next.jdbc.types :as jdbc.types]) (quote [next.jdbc.date-time :as jdbc.date]) (quote [honey.sql :as sql]) (quote [honey.sql.helpers :as sqlh])) (:gen-class)) - failed: Extra input spec: :clojure.core.specs.alpha/ns-form

flowthing20:03:42

There's something wrong with the ns form in that file. Looks like you might be using require instead of :require? Also, I don't use Calva so I'm not sure, but I think you might want to use the "Load current file" command instead of load-file: https://calva.io/commands-top10/

🙏 1
pez20:03:23

It is certainly more convenient to use the Calva command for it. Then you can stay in the file. Have you found the Getting Started REPL, @U037THMHCD8? There's a command that starts it. It will open up some files for you which form a guide where you can learn Calva, some structural editing and even some Clojure. (And I now realize I should spend some more time on introducing the ns form in that guide.)

flowthing20:03:19

The ns form is indeed a bit of a beast. https://gist.github.com/ghoseb/287710/ is a pretty good cheat sheet.

pez21:03:03

Hovering the ns form shows the docs for it, as well as ClojureDocs examples.

👍 1
flowthing21:03:48

Wonder if clj-kondo could/should flag require vs :require in the ns form…?

pez21:03:10

It probably should!

Prithvi Hv04:03:31

@U0ETXRFEW im able to reproduce this in the repl by calling the load function

Prithvi Hv04:03:42

this is what my import looks like:

(ns groww-scraper.core
  (require '[clj-http.client :as client]
           '[cheshire.core :as json]
           '[environ.core :refer [env]]
           '[clj-time.core :as t]
           '[clj-time.coerce :as tc]
           '[next.jdbc :as jdbc]
           '[next.jdbc.types :as jdbc.types]
           '[next.jdbc.date-time :as jdbc.date]
           '[honey.sql :as sql]
           '[honey.sql.helpers :as sqlh])
  (:gen-class))

Prithvi Hv04:03:30

when i switch from require to :require and remove the ' it worked 🎉 @U4ZDX466T @U0ETXRFEW the NS form cheatsheet link and docs/comments on calva were very helpful. to sum it up: with the ns form i was never suppose to use the require function. It was should be a dataobject, right?

pez06:03:47

Correct. The ns macro handles the quoting and the calling. Glad you solved it!

Jon Olick20:03:58

man there are like soooo many random functions in the core libs

Sam Ritchie23:03:29

nah, just 4 😉

practicalli-johnny02:03:24

4 out of about 600 things that can be called as functions in Cloure.core seems quite a small number

Sam Ritchie07:03:19

@U05254DQM I think @U036UA9LZSQ was saying that the standard library is much larger than one might expect, using “random” as in “why is this here?” I was attempting a joke by taking “random” very literally!

1
Jon Olick20:03:11

definitely more CISC than RISC

andy.fingerhut01:03:36

If it helps to see a large fraction of them broken down into categories of related functionality, check out the cheatsheet: https://clojure.org/api/cheatsheet or here https://jafingerhut.github.io/

👀 1
bad_ash21:03:38

hi, i'm building a prototype REST API for accessing and adding a collection of songs using Compojure and Datomic. i made d/db an atom and when I POST to add a song, i also update the db atom by using swap!. however, i am running into errors doing this. my question is, how is keeping the datomic cached db up to date usually done in a web application?

dgb2321:03:17

I’m not entirely sure I understand, but I assume you are swapping the db value with some other thing related to a POST request? However I don’t think doing that (and maybe even holding the db in an atom) is really necessary or what you actually want? You might just want to use d/transact on the db value with your new data.

dgb2321:03:40

If you want to cache reads on top of that, you might want to run any queries first and then cache the actual view you want to present in your app. Or you can do something very simple with E-Tag or Last-Modified so the browser takes care of caching responses. Datomic should be able to help you there.