Fork me on GitHub
#beginners
<
2020-09-28
>
sova-soars-the-sora03:09:18

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

sova-soars-the-sora03:09:32

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

sova-soars-the-sora03:09:02

any pointers (that are not null) would be appreciated

😆 1
phronmophobic03:09:58

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

suren03:09:37

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

[java.sql :refer [Timestamp]]

phronmophobic03:09:13

for java classes:

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

phronmophobic03:09:39

or from the repl:

(import java.sql.Timestamp)

suren03:09:44

yup that works, thanks

seancorfield03:09:51

If you want to import multiple Java classes from a package

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

suren05:09:52

Thanks guys import was what I needed.

0xclj03:09:03

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

seancorfield04:09:53

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

seancorfield04:09:23

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.

seancorfield04:09:13

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

seancorfield04:09:58

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.

0xclj04:09:45

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"

seancorfield04:09:49

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

seancorfield04:09:29

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

0xclj04:09:18

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?

seancorfield04:09:38

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

seancorfield04:09:53

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

seancorfield04:09:10

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.

seancorfield04:09:04

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.

0xclj04:09:35

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

seancorfield04:09:52

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)

seancorfield04:09:51

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.

seancorfield04:09:38

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

seancorfield04:09:01

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.

0xclj04:09:04

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.

bnstvn08:09:50

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 Sless09:09:42

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
bnstvn09:09:29

ah, right, makes sense. thank you

0xclj09:09:44

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.

dakra09:09:45

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))

bnstvn10:09:11

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})

Audrius12:09:09

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 Makarov12:09:21

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

💪 1
💯 1
misto quente13:09:48

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 ?

dpsutton14:09:13

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

misto quente14:09:39

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

dpsutton14:09:43

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

dpsutton14:09:27

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

alexmiller14:09:08

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)

dpsutton14:09:17

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

alexmiller14:09:38

that's not an env var

oly14:09:48

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 ?

dpsutton14:09:18

ah good point.

oly14:09:18

just env the value ?

oly14:09:00

although env tends not to work so well with frontend libraries

misto quente14:09:14

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_john14:09:58

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

dpsutton14:09:31

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

dpsutton14:09:39

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

misto quente14:09:19

yes, trying jack-in with that profile

dpsutton14:09:59

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

dpsutton14:09:20

in .dir-locals.el you can paste:

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

practicalli_john14:09:11

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

misto quente14:09:11

problem solved

Audrius15:09:49

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

delaguardo15:09:26

have a look at clojure.walk namespace

Baye16:09:44

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

andy.fingerhut16:09:04

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.

Baye17:09:29

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 Newton16:09:44

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 ?

dpsutton16:09:37

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

Jim Newton17:09:33

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

ghadi17:09:19

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

ghadi17:09:04

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

NoahTheDuke20:09:25

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

NoahTheDuke20:09:32

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

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

NoahTheDuke20:09:46

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

NoahTheDuke20:09:14

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

NoahTheDuke20:09:16

as simple as:

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

NoahTheDuke20:09:16

what am I missing?

Jan K20:09:27

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

☝️ 1
phronmophobic20:09:48

both defmethod and defcard-multi will be qualified as well

NoahTheDuke20:09:09

ah, thank you