beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
Faris 2021-06-21T04:44:12.392500Z

Hi Iโ€™m working with monger the mongoDB Clojure wrapper, Iโ€™m trying to create a user on the database, the function seems simple enough

add-user
(add-user db username password)
Adds a new user for this db
but when I do something like this
(db/add-user db "test" "12345")
It gives this error.
java.lang.String cannot be cast to [C
What does this error mean? If its any help, this is what the function looks like in the monger source code
(defn add-user
  "Adds a new user for this db"
  [^DB db ^String username ^chars password]
  (.addUser db username password))
Thanks in advance

phronmophobic 2021-06-21T04:47:10.392600Z

Both [C and ^chars are used for arrays of characters (which is different from String). Does (db/add-user db "test" (.toCharArray "12345")) work?

Faris 2021-06-21T04:49:47.392800Z

Oh that works! If you donโ€™t mind me asking, whats the difference between (char "12345") and (.toCharArray "12345")

phronmophobic 2021-06-21T04:53:16.393Z

char will try to cast its argument to a single character (type is char) and (.toCharArray "asf") will return an array of chars (type is similar to char[]) for a given string

Faris 2021-06-21T04:57:26.393200Z

That makes sense, thanks!!!

sova-soars-the-sora 2021-06-21T16:56:50.395800Z

Living Clojure is a great book -- your local library might have some clojure books or you could request your local library to get some ^.^

popeye 2021-06-21T17:20:48.396500Z

why (- 5 -10) gives 15 ?

sova-soars-the-sora 2021-06-25T19:13:48.032400Z

this is called prefix notation.

chaow 2021-06-21T17:21:34.397200Z

(- 5 -10) in clojure is equivalent to (5 - (-10)) in regular math

joshkh 2021-06-21T17:31:08.398300Z

can i define clojure.test tests in a loop?

(defn generate-tests
  []
  (deftest "major test"
    (for [case [1 2 3]]
      (testing (str "is " case " = 1")
        (is (= 1 case))))))
(macro error)

2021-06-24T16:00:42.493800Z

I do it like this:

(defn exercise-case
  [x]
  (is (= 1 x)
    (str "is" x "= 1")))

(deftest exercise-1
  (exercise-case 1))

(deftest exercise-2
  (exercise-case 2))

(deftest exercise-3
  (exercise-case 3))
note that the second arg to is is printed on test failure, just like testing but it can be calculated on demand in its scope

dpsutton 2021-06-21T17:33:47.398500Z

use doseq

2021-06-21T17:34:52.398700Z

for is not a loop

2021-06-21T17:35:28.399700Z

putting defs inside other forms is pretty much never good

joshkh 2021-06-21T17:35:34.399800Z

heh, yeah i knew the for wouldn't work there

joshkh 2021-06-21T17:35:48.400100Z

why didn't i think of doseq.. thanks @dpsutton

joshkh 2021-06-21T17:38:27.400200Z

yup, i get triggered every time i read through https://github.com/ptaoussanis/sente

2021-06-21T17:45:37.400600Z

https://clojuredocs.org/clojure.template/do-template, maybe

jcd 2021-06-21T20:06:00.401400Z

is there a function to check if a given symbol is a multimethod?

Jan K 2021-06-21T20:13:59.401800Z

(instance? clojure.lang.MultiFn x)

jcd 2021-06-21T20:14:07.402Z

ty ty

2021-06-21T20:27:04.402200Z

a given object

2021-06-21T23:41:22.403100Z

Is it best practice under Linux to install leiningen from the distro's package manager, or should I install something under my user?

practicalli-john 2021-06-22T18:30:23.440600Z

The package manage will be slightly simpler and familiar process and should ensure the binaries do not conflict with anything else on the execution path. If you wish to have a specific version, then install in _/.local/bin which is on most exec paths by default if that directory exists. If configuring a multi-user system and then install in /user/local/bin using sudo instead.

dpsutton 2021-06-21T23:45:50.404400Z

i'd recommend not using the distro's version. Those come with upgrades prevented. If you install it yourself you can easily run lein upgrade <version> and easily change the version if there are bugs, incompatibilities, etc

4๐Ÿ‘1โž•
dpsutton 2021-06-21T23:47:01.404600Z

<https://github.com/technomancy/leiningen/blob/master/bin/lein-pkg#L107>

1๐Ÿ‘