beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
zhuxun2 2020-09-28T01:51:14.212600Z

@jr0cket Unfortunately, no. :main-opts ["-m" "antq.core" "--error-format='::error file={{file}}::{{message}}'"] is interpreted as ["-m" "antq.core" "--error-format='::error" "file={{file}}::{{message}}'"]

zhuxun2 2020-09-28T01:52:42.212800Z

@jason358 Unfortunately, neither \\ nor "--param='{:a 1}'" worked for main-opts

sova-soars-the-sora 2020-09-28T03:15:18.213800Z

Hi, I keep getting this strange error about file uploads https://pastebin.com/VxY2Z4N3

sova-soars-the-sora 2020-09-28T03:15:32.214100Z

I guess somebody is trying to upload a file to my box? i don't understand, it's a GET /

sova-soars-the-sora 2020-09-28T03:16:02.214400Z

any pointers (that are not null) would be appreciated

πŸ˜† 1
phronmophobic 2020-09-28T03:19:58.215500Z

you could probably replace wrap-params or whichever middleware that calls wrap params with a version that tries to wrap-params, catches errors, logs more info about the request, and then re throws the error

suren 2020-09-28T03:31:37.216500Z

How to import java.sql ? I tried following but does not seem to work.

[java.sql :refer [Timestamp]]

suren 2020-09-29T05:24:52.289Z

Thanks guys import was what I needed.

phronmophobic 2020-09-28T03:34:13.216900Z

for java classes:

(ns my.ns
  (:import java.sql.Timestamp))

phronmophobic 2020-09-28T03:34:39.217300Z

or from the repl:

(import java.sql.Timestamp)

0xclj 2020-09-28T03:36:03.217900Z

How do I do Select * from Users where id in (1, 2, 3); in toucan[https://github.com/metabase/toucan]?

suren 2020-09-28T03:38:44.218100Z

yup that works, thanks

seancorfield 2020-09-28T03:44:51.218300Z

If you want to import multiple Java classes from a package

(ns my.ns
  (:import [java.sql Connection DataSource Timestamp]))

seancorfield 2020-09-28T04:20:53.219300Z

You probably won't get answer in any channel but #toucan (and I see you've already posted this question there).

seancorfield 2020-09-28T04:21:23.220Z

I suspect not many people use Toucan -- it kind of looks very ORM-ish and that's not how folks deal with databases in Clojure.

seancorfield 2020-09-28T04:22:13.220500Z

(hmm, only 23 people in #toucan which also speaks to it not being widely used)

seancorfield 2020-09-28T04:28:58.224800Z

Looking at the docs, I suspect you can do (db/select User :id [:in [1 2 3]]) depending on what you've defined as the "model" for your users table.

seancorfield 2020-09-28T04:29:36.225400Z

It looks like it builds some queries on top of HoneySQL, based on https://github.com/metabase/toucan/blob/master/docs/db-functions.md#more-advanced-key-value-args

0xclj 2020-09-28T04:32:45.226600Z

I followed some articles which mentioned the usage of Toucan in production. The documentation has pointers on how to write queries for common scenarios but couldn't find an example that dealt with the question I asked. Reference: https://github.com/metabase/toucan/blob/master/docs/db-functions.md Article: https://www.demystifyfp.com/clojure/blog/restful-crud-apis-using-compojure-api-and-toucan-part-1/

clj::Twitter.core=> (db/select Tweet :id [:in 1 2 3])
; Execution error (PSQLException) at org.postgresql.core.v3.QueryExecutorImpl/receiveErrorResponse (QueryExecutorImpl.java:2553).
; ERROR: syntax error at or near "$1"

seancorfield 2020-09-28T04:33:49.227400Z

That isn't what I suggested you try. Check what I suggested again.

seancorfield 2020-09-28T04:34:29.227800Z

(you're missing [ ... ] around the sequence of IDs)

0xclj 2020-09-28T04:37:18.229800Z

Yup, fixed that and it works. Thanks! Also, I'll look into HoneySQL. > that's not how folks deal with databases in Clojure What's the recommended way?

seancorfield 2020-09-28T04:37:38.230100Z

Just plain data -- hash maps and vectors of hash maps.

seancorfield 2020-09-28T04:37:53.230500Z

We don't have "objects" so "ORM" makes no sense.

seancorfield 2020-09-28T04:39:10.231700Z

The other thing that's bad about Toucan is that it relies on a global database connection behind the scene -- global singletons are an anti-pattern.

seancorfield 2020-09-28T04:40:04.232700Z

It's better to pass the DB connection/datasource into code that accesses the DB so that it is easier to test code and also to reuse code across multple datasources.

0xclj 2020-09-28T04:45:35.234200Z

Any examples I can refer to see how that's done? Came across this: http://clojure-doc.org/articles/ecosystem/java_jdbc/home.html

seancorfield 2020-09-28T04:48:52.236200Z

next.jdbc is the replacement for clojure.java.jdbc and the documentation is better https://cljdoc.org/d/seancorfield/next.jdbc/ -- unfortunately the clojure-doc server hasn't been updated in ages (I have a number of java.jdbc doc updates committed that haven't been published)

seancorfield 2020-09-28T04:49:51.237Z

Yeah, http://clojure-doc.org hasn't been republished since mid-2018 😞 but a lot of changes have been committed to the repo since then.

seancorfield 2020-09-28T04:50:38.237600Z

(I maintain HoneySQL and next.jdbc -- and I've maintained clojure.java.jdbc since 2011)

seancorfield 2020-09-28T04:54:01.238400Z

There are #honeysql and #sql channels if you get stuck with either HoneySQL or any more general SQL-related issues. And those channels are fairly active.

0xclj 2020-09-28T04:58:04.241500Z

That's great! I'll go through the doc and try out some HoneySql on new projects. I'll post the solution in #toucan too. Thank you for your help.

Jim Newton 2020-09-28T06:15:38.241600Z

πŸ‘

Jim Newton 2020-09-28T06:15:59.241800Z

great so I'm now a contributor, not just a user. πŸ™‚

bnstvn 2020-09-28T08:31:50.245700Z

can i describe a n*n matrix with spec? so all vectors should have the same length β€” [[1 2] [3 4]] would be fine but [[1 2] [ 3]] and [[1 2]] are not

Ben Sless 2020-09-28T09:06:42.246600Z

You can write your own predicate functions. This is a starting point you can experiment around

(defn square?
  [m]
  (let [n (count m)]
    (every? #(== n (count %)) m)))

(comment
  (square? [[1 2]])
  (square? [[1 2] [3 4]])
  (square? [[1 2] [3 4] [5 6]]))

(s/def ::square?
  (s/and sequential? square?))

πŸ‘ 1
bnstvn 2020-09-28T09:40:29.249500Z

ah, right, makes sense. thank you

0xclj 2020-09-28T09:49:44.252200Z

What is the recommended way to filter maps from a vector of maps based on the value of specific keys?

existing-ids => [1 2 32 24]
api-response => [{:id 1, :text "abcd"},{:id 2 :text "efgh"},{:id 3 :text "asdf"}, {:id 4 :text "gsdsd"}]
result => [{:id 3 :text "asdf"}, {:id 4 :text "gsdsd"}] ;;api-response with ids present in existing-ids are removed.

dakra 2020-09-28T09:55:45.252300Z

I'm relatively new to Clojure myself but I would do this:

(let [existing-ids (set [1 2 32 24])
      api-response [{:id 1, :text "abcd"},{:id 2 :text "efgh"},{:id 3 :text "asdf"}, {:id 4 :text "gsdsd"}]]
  (remove #(existing-ids (:id %)) api-response))

0xclj 2020-09-28T09:59:14.252500Z

Thanks @daniel415!

bnstvn 2020-09-28T10:00:11.252700Z

an extra dep, but i found medley useful https://weavejester.github.io/medley/medley.core.html#var-filter-keys

(m/filter-keys #{:a :b} {:a 1 :c 2 :b 3})

0xclj 2020-09-28T10:32:42.253Z

Thanks @ban.istvan!

Audrius 2020-09-28T12:30:09.255500Z

Hi, What would be the best way to transform such map {:a "text" :bb "more -text"} to {:a "text" :bb 9} so that :bb value is the length of the previous map's :bb value?

Stas Makarov 2020-09-28T12:35:30.255600Z

https://clojuredocs.org/clojure.core/update

πŸ‘ 1
βœ”οΈ 1
Stas Makarov 2020-09-28T12:37:21.255900Z

(update m :bb #(count %)) or just (update m :bb count)

πŸ’― 1
πŸ’ͺ 1
misto quente 2020-09-28T13:57:48.258600Z

When i cider-jack-in to a lein project, the repl environment is set up with env var conf="./dev-config.edn" where is this configured? How can I achieve this when using deps.edn ?

dpsutton 2020-09-28T14:06:13.259200Z

that sounds like it is configured in code in your project. certainly lein doesn't set any kind of environmental variable called conf.

misto quente 2020-09-28T14:06:39.259500Z

I think cider reads the :jvm-opts key in my project.clj dev profile. I think i would need something similar in a deps.edn alias… any example would be appreciated

dpsutton 2020-09-28T14:08:43.260400Z

CIDER doesn't but yes lein will certainly start up the jvm with those options. running clj --help i see > The dep-opts are used to build the java-opts and classpath: > -Jopt Pass opt through in java_opts, ex: -J-Xmx512m which sounds like it might be what you are looking for

dpsutton 2020-09-28T14:09:27.260800Z

or, that's a way to pass jvm options. that doesn't sound like how you are setting an environmental var though

alexmiller 2020-09-28T14:14:08.261600Z

you can't set env vars from inside a jvm, so this would need to be something around the invocation (clj / deps.edn does not have anything like this)

dpsutton 2020-09-28T14:15:17.261800Z

clj -J-Dconfig="dev"
Clojure 1.10.1
user=> (System/getProperty "config")
"dev"
user=>

alexmiller 2020-09-28T14:16:38.262900Z

that's not an env var

oly 2020-09-28T14:16:48.263200Z

what's the best way to do defaults in a library so (def api-url "http://api/") but then allow the user to pull in the library but replace the default ?

dpsutton 2020-09-28T14:17:18.263500Z

ah good point.

oly 2020-09-28T14:17:18.263600Z

just env the value ?

oly 2020-09-28T14:18:00.264200Z

although env tends not to work so well with frontend libraries

misto quente 2020-09-28T14:34:14.267600Z

Sorry, i was mistaken when i said env var, i meant passing a system prop in jvm opts, what I’m looking for is is to have cider-jack-in use my dev alias like: clj -A:dev , where dev is something like…

:aliases
 {:dev {:jvm-opts ["-Dconf=dev-config.edn"]}}

practicalli-john 2020-09-28T14:42:58.267800Z

As an aside, https://github.com/juxt/aero is a great project for managing configuration.

misto quente 2020-09-28T14:46:01.268100Z

thanks @jr0cket .. reading through your post at http://jr0cket.co.uk/2019/07/CIDER-jack-in-to-Clojure-CLI-projects-from-Spacemacs.html seems to be what I need..

dpsutton 2020-09-28T14:47:31.268600Z

your example there works. Does that mean your problem is solved?

dpsutton 2020-09-28T14:47:39.268800Z

or is your question now how to jack in with that profile?

misto quente 2020-09-28T14:48:19.269Z

yes, trying jack-in with that profile

dpsutton 2020-09-28T14:49:59.269200Z

you can use a prefix argument before cider-jack-in and add -A:dev to the jack in string. alternatively, you can set the variable cider-clojure-cli-global-options to "-A:dev" to have this by default

dpsutton 2020-09-28T14:50:20.269400Z

in .dir-locals.el you can paste:

((nil . ((cider-clojure-cli-global-options     . "-A:dev:test"))))

misto quente 2020-09-28T14:52:07.269800Z

thanks @dpsutton @jr0cket

practicalli-john 2020-09-28T14:52:11.270Z

I am fairly sure the JVM options are part of the cider-clojure-cli-global-options

misto quente 2020-09-28T14:52:11.270200Z

problem solved

Audrius 2020-09-28T15:27:49.271600Z

How to iterate every [key value] of a deeply nested map?

2020-09-28T15:33:26.271700Z

have a look at clojure.walk namespace

2020-09-28T15:34:05.271900Z

or tree-seq β€” https://clojuredocs.org/clojure.core/tree-seq

βœ”οΈ 1
Baye 2020-09-28T16:06:44.272300Z

I am new to programming. I have started to learn C++ and even some python My background is Economics. I have strong quantitative background (Math and Econ) and I know STATA. In general, I would like to use programming (1) for ML/AI in economics topics+other utilities such as webcraping, and other data driven analyses (2) for it, fintech matchmaking platforms In particular, I just recently discovered I love programming. I am old (35) as a beginner but I have set a goal to become a fully capable programmer by 40...Hopefully I can learn 2 languages well enough by then. If jobs opportunities are present then, I can switch careers although I still like my career path. I could always use my programming skills for either my job, a start up, or become a full-fledge prgrammer. Online research has led me to think that Clojure might be an excellent language to focus on because they say it is an excellent for rapid development (If I want to use it for a start up, lone developer), data-driven (econ and ML), and provide a path to be a great programmer. My Apprehension is that Clojure doesn't seem widely used so might issues with doing ML projects with few libraries (compare to Python for ex), and future job prospects, etc... Anyway, I welcome any insights/advice, tips. Thanks in advance, Baye

Jim Newton 2020-09-28T16:10:44.273300Z

The documentation for defn- says it generates a non-public def. What does that mean? Does it mean that the symbol does not get imported when I use :refer :all in a namespace :require ?

dpsutton 2020-09-28T16:11:37.273900Z

correct. that var can only be referenced from that namespace. you can of course get around this with #'the-var but it's "private"

2020-09-28T16:54:04.274200Z

Being a Clojure enthusiast, I do not want to dissuade you from learning it, but whether it is a good language for you to learn does depend upon your goals, and the goals you have described lead me to recommend that you might try asking in the #data-science channel here to better learn the strengths vs. weaknesses Clojure has relative to more widely used platforms like Python for such tasks.

Baye 2020-09-28T17:04:29.274500Z

Thanks for your reply. I have posted the question at #data-science channel. But my interest, in programming is not limited to data science/ML...I am potentially interested in building apps (web/desktop, etc) for potential future projects in education, health etc...

Jim Newton 2020-09-28T17:11:33.275100Z

that's cool. I've been naming such functions with funny names. I think this could be useful.

ghadi 2020-09-28T17:19:19.275700Z

@jimka.issy another common thing is to make a namespace with "impl" in the name, and treat it as private (by convention)

ghadi 2020-09-28T17:20:04.276600Z

yourcompany.queue.protocols
yourcompany.queue.impl.sqs
yourcompany.queue.impl.rabbit
yourcompany.queue.impl.kafka

NoahTheDuke 2020-09-28T20:16:25.277600Z

quick macro question: i want to define a method on a multimethod, from within a macro

NoahTheDuke 2020-09-28T20:17:32.278100Z

i have (defmulti defcard-multi (fn [title] title)) as the base. and then i have the macro

(defmacro defcard [title] `(defmethod defcard-multi ~title [_] {}))

NoahTheDuke 2020-09-28T20:18:46.278900Z

and then calling (defcard "anything") throws a large spec error

NoahTheDuke 2020-09-28T20:19:14.279400Z

i've reduced it to still happening if i leave the multimethod out of the picture, just using defn instead

NoahTheDuke 2020-09-28T20:20:16.279700Z

as simple as:

(defmacro defcard [title] `(defn temp [_] ~title))

NoahTheDuke 2020-09-28T20:21:16.280800Z

what am I missing?

Jan K 2020-09-28T20:21:27.281100Z

that _ will become namespace-qualified and cause the problem, use ~'_

☝️ 1
phronmophobic 2020-09-28T20:21:48.281400Z

both defmethod and defcard-multi will be qualified as well

alexmiller 2020-09-28T20:22:05.281900Z

That’s ok

phronmophobic 2020-09-28T20:22:15.282300Z

right

NoahTheDuke 2020-09-28T20:25:09.282600Z

ah, thank you

Audrius 2020-09-28T20:39:18.283600Z

how to put a string into a vector? I tried (into [] "jfjfjgjgjf") but this one breaks string into symbols. I need it as a whole...

dakra 2020-09-28T20:43:30.283800Z

(conj [] "foo") or with into it has to be a seq (into [] ["foo" "bar"])

βœ”οΈ 1
practicalli-john 2020-09-28T21:57:11.287500Z

(vector "string")

βœ”οΈ 1
2020-09-28T22:53:39.287800Z

to be pedantic, that doesn't break it into symbols (symbols are a specific type, used for looking up variables and local bindings most often), it breaks it up into characters

πŸ‘ 1