beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
sova-soars-the-sora 2021-06-10T00:04:44.494100Z

as @dpsutton said you can turn that vector into an (ordered) map by making the key the index [77 76 75 74 73] becomes

{0 77 
 1 76
 2 75
 3 74
 4 73}
So you can access the first element doing (get m 0) because 0 is effectively the key name Might be better to use something like this for long-term sanity

Angel Rojas 2021-06-10T00:20:47.496Z

hello, could you please tell me where find a tutorial for create an API REST

Angel Rojas 2021-06-11T13:22:12.076200Z

thank so much @sova 😉

Angel Rojas 2021-06-10T00:21:28.496700Z

its great to be here...!😃

Chris K 2021-06-10T00:57:55.498500Z

Hello I'm using doom emacs and cider for clojure and for some reason, if I run cider on certain directories, emacs will go crazy and use 100% of my cpu, the cider repl will also not open

Chris K 2021-06-10T00:58:09.498900Z

this happens only on like 1 directory and others projects are fine

sova-soars-the-sora 2021-06-10T01:25:58.499Z

Hello Angel This is a nice tutorial that does not make it too complex: https://medium.com/swlh/building-a-rest-api-in-clojure-3a1e1ae096e https://github.com/chris-emerson/rest_demo/blob/master/src/rest_demo/core.clj

sova-soars-the-sora 2021-06-10T01:26:20.499400Z

2 links there, one is the tutorial, one is the source code of the main core.clj file

sova-soars-the-sora 2021-06-10T01:26:31.499600Z

if you need any help feel free to ask in the channel ! ^.^

seancorfield 2021-06-10T01:59:02.499800Z

If you haven’t asked there already, you may have a better chance of getting answers in #cider and/or #doom-emacs

Chris K 2021-06-10T02:56:01.000300Z

thanks

Chris K 2021-06-10T02:57:19.001400Z

I have a map that I need the keys to be characters like '(' or ';' but I'm not sure what's the best way to make a map with keys like this I currently have it as : {";" "SemiColon"}

jaihindhreddy 2021-06-10T03:00:17.001500Z

Character literals are preceeded by a backslash in Clojure:

{\; "SemiColon" \( "OpeningParenthesis"}

Chris K 2021-06-10T03:02:37.001800Z

Thanks!! Been a while also 😄

1➕
popeye 2021-06-10T08:04:10.002800Z

I have json structure like below

{
    "fruit": "Apple",
    "size": "Large",
    "color": "Red"
}
{
    "fruit": "orange",
    "size": "Small",
    "color": "Red"
}

popeye 2021-06-10T08:04:25.003200Z

How can I group like below structure

popeye 2021-06-10T08:04:27.003500Z

fruit=[Apple, Large]
size=[Large,Small]

2021-06-10T08:07:13.003900Z

(reduce
 (fn [acc {:strs [fruit size]}]
   (-> acc
       (update "fruit" (fnil conj []) fruit)
       (update "size" (fnil conj []) size)))
 {}
 [{...}])
as an example of using reduce

popeye 2021-06-10T08:12:38.004100Z

What us :strs ?

2021-06-10T08:13:47.004300Z

destructuring instruction, same as :keys but for string

1👀
popeye 2021-06-10T08:22:45.004800Z

I got update does noot found , I am using clojure 1.9

popeye 2021-06-10T08:31:15.005100Z

I got {f{r{u{it{s{mango orange}}}} after using update-in

popeye 2021-06-10T08:31:46.005300Z

@delaguardo ^^

2021-06-10T08:33:15.005500Z

update-in expect to have a sequential path provided as a second argument. I guess you have a string instead. The problem that in clojure string is also sequential data

2021-06-10T08:33:23.005700Z

just use update

popeye 2021-06-10T08:33:38.005900Z

it says update not found

2021-06-10T08:40:21.006100Z

oO

2021-06-10T08:40:44.006300Z

could you share your code?

popeye 2021-06-10T08:46:26.006500Z

@delaguardo

(reduce
               (fn [acc dt]
                 (-> acc
                     (update "fruits" (fnil conj []) (:fruits dt))
                     (update "size " (fnil conj []) (:size dt ))))
               {}
               data)

popeye 2021-06-10T08:47:44.006700Z

oh sorry , i had the code updat eexclude above

popeye 2021-06-10T09:43:13.007600Z

I have below code

(def a (interpose "," (get hellovar ":a" )))
(def b (interpose "," (get hellovar ":b" )))
(def c (interpose "," (get hellovar ":c" )))
(def d (interpose "," (get hellovar ":d" )))
Can this be improved ?

popeye 2021-06-10T09:55:43.007800Z

I wrote in below way

(defn hellointepe [data]
  (reduce-kv
    (fn [acc k v]
      (assoc acc k (interpose "," v)) )
    {}
    data))

Juλian (he/him) 2021-06-10T10:06:04.008Z

you could use zipmap:

(zipmap (keys hellovar)
        (map (partial interpose ",")
             (vals hellovar)))

Juλian (he/him) 2021-06-10T10:12:04.008200Z

but your reduce-kv looks fine, too

popeye 2021-06-10T11:41:37.009800Z

I am trying to type cast each value like (map Integer. (get fruts :size)) this giving syntax error at Integer. any reason?

indy 2021-06-10T11:52:03.009900Z

What is the value of (get fruits :size)It has to be a seq.

popeye 2021-06-10T11:53:34.010200Z

it is giving 11

indy 2021-06-10T11:54:03.010400Z

It has to be a sequence-like collection, example, vector, list, set...

indy 2021-06-10T11:58:21.010600Z

(map (comp #(Integer/parseInt %) :size) fruits)

indy 2021-06-10T12:03:49.011Z

if your fruits is something like [{:size "90" :name "apple"} {:name "banana" :size "89"}]

1✅
popeye 2021-06-10T13:07:57.011700Z

Any one aware of https://github.com/korma/Korma ?

2021-06-10T13:40:05.012100Z

This is a question of personal preferences but in my opinion honeysql is much more “clojure” than korma - https://github.com/seancorfield/honeysql

NoahTheDuke 2021-06-10T14:29:25.012900Z

is there a wrapping library for clojure.string that makes the functions nil-pun?

2021-06-10T15:42:41.013100Z

or if you want to try sql from a higher level of abstraction, there's http://walkable.gitlab.io

popeye 2021-06-10T16:00:35.014100Z

That was my production code , so cannot change now

2021-06-10T16:03:17.018100Z

Integer. is not an object that can be passed to map, it is a syntax expanding to calling new on a class and for (IMHO foolish) domain design reasons methods are not first class citizens in java (and thus the JVM), but instead they are special operations on an object. #(Integer. %) takes that constructor invocation and puts it inside a new Object of class clojure.lang.IFn which is an obect with .invoke and .call methods, which is what map expects as its arg.

ej 2021-06-10T16:03:53.019300Z

I seem to remember reading about a project for getting better error messages in Clojure, but I can't seem to find anything when Googling right now. As far as I remember, you could add it to your project and it would look up error messages on a server and give you something more easily understandable. Does anyone know what I'm talking about?

2021-06-10T16:04:41.019400Z

finally, as Indy suggests Integer/parseInt is better than using the Integer constructor Long/parseLong is almost always better than that even because integral literals in clojure code are always Long and on modern computers there are few cases where using an Integer is better

2021-06-10T16:05:30.019600Z

same with Float vs. Double - clojure always prefers the 64 bit type, and down casts to the smaller type for you when it's needed for method call interop

2021-06-10T16:07:31.020500Z

this? https://github.com/bhb/expound

2021-06-10T16:07:36.020800Z

I almost made something like that but before I got motivated enough to flesh it out I just learned the internal classes / methods that keep showing up in stack traces and moved on

2021-06-10T16:10:04.023300Z

as a brand new user I saw Long can't be cast to IFn and got confused and angry, then I went code diving and learned what IFn was and why my code would make that happen, then I started thinking that I could make a guide or program that turned that into a more intuitive message, but before that plan got fleshed out I just developed intuitions for things like IFn and seq coersion and I had other priorities

2021-06-10T16:12:26.023500Z

@jeffrey.wayne.evans because it doesn't need to create a function for each example, and also because it doesn't need to create a lazy seq to provide args to some-fn

1👍
popeye 2021-06-10T16:18:45.024Z

Thanks @noisesmith, helped

popeye 2021-06-10T16:23:26.026100Z

Hi Team, I have a scenario where I have written a function where it calls the database select operation multiple times lets say 10000 times, How can I implement that function, which can be used reduce , map or pmap any suggestion?

2021-06-10T16:29:34.026200Z

can you build the select operation programmatically, at run time? collapsing the 10,000 into 1

2021-06-10T16:29:47.026400Z

or maybe i misunderstand the goal

2021-06-10T16:30:50.026600Z

right, calling a db in a tight loop like that is like opening a file to write each character, then close it again, across a string

2021-06-10T16:31:19.026800Z

it's going to have the correct result but it's never the thing that you want to do in a real app that isn't a toy

popeye 2021-06-10T16:31:56.027Z

I have to call the function multiple times, so using reduce might take more time , so how in clojure we can handle calling same function several time? may be more than >10000

popeye 2021-06-10T16:33:07.027200Z

I have passed using In operation for database ( passing multiple argument at same time)

2021-06-10T16:33:09.027400Z

if you want to call the same function multiple times with different arguments, map works

2021-06-10T16:33:21.027600Z

that's what map repeatedly and run! and doseq are for, but it's a bad idea to do this with db calls

popeye 2021-06-10T16:33:37.027900Z

But how calling same function multiple times is handled in clojure

2021-06-10T16:33:50.028100Z

it depends on the context

2021-06-10T16:34:21.028300Z

do you have args in a collection? do you have a function ready to take those args? do you need a discrete return value from each call in a new collection?

2021-06-10T16:34:45.028500Z

the answers to those questions are how you decide between map, repeatedly, run!, doseq, iterate, etc. etc.

2021-06-10T16:34:48.028700Z

(defn query-db
  [db lookup-id]
  'data)

(def lookup-ids [1 2 3])

(def db 'db)

(map (partial query-db db) lookup-ids)
;; => (data data data)

2021-06-10T16:36:12.028900Z

but! calling map to get db results means that your db lookups are going to happen in unpredictable places at runtime, and if you already know multiple id's it's always better to do one select to get all of the ids rather than call the db in a loop

2021-06-10T16:36:21.029100Z

there's no one size fits all answer

popeye 2021-06-10T16:39:20.029300Z

do you have args in a collection? do you have a function ready to take those args? do you need a discrete return value from each call in a new collection? - Yes

sova-soars-the-sora 2021-06-10T16:49:49.029500Z

is there any repetition in the calls or repetition in the result you can exploit

Endre Bakken Stovner 2021-06-10T17:43:00.032800Z

I am having trouble using taps. My tap code looks like this:

(defn tap [[type & args]]
  (let [[jobid outtype line] args
        type-color (if (= type :err) red green)
        cmdline (str/join " " [(yellow (str/join " " jobid)) (type-color type) line])]
    (println "------")
    (println cmdline)
    (println "------")
    ))

(add-tap tap)
The place where I call tap> looks like this:
(tap> [jobid type line])
Still my output looks like:
[[:bwa-map ({:sample "A", :genome "hg38"})]
 :err
 "[main] Real time: 0.853 sec; CPU: 0.646 sec"]
I.e. it seems like just println is used, not my custom tap function which calls three printlns. What might I be doing wrong?

Endre Bakken Stovner 2021-06-10T17:47:09.033600Z

Doing it in the REPL works:

user> (add-tap #(println "----" % "----"))
;; => nil
user> (tap> "hi")
;; => true---- hi ----
"hi"

Endre Bakken Stovner 2021-06-10T17:54:24.034Z

I'm sure my add-tap was triggered since I added this line above it:

(println "adding tap****************")
(add-tap tap)

2021-06-10T18:27:15.034100Z

@popeyepwr if yes to all of those, use map if it's not io / side effecting / manipulating state, and mapv if it is

2021-06-10T18:30:12.035300Z

it could be your tap function is crashing, I'm not sure you'd see a stack trace if it did

2021-06-10T18:30:55.036300Z

you could put a try / catch with a println in the catch to check

2021-06-10T18:33:44.036600Z

@endrebak85

Clojure 1.10.1
(cmd)user=> (add-tap (fn [& args] (/ 1 0)))
nil
(cmd)user=> (tap> nil)
true
(ins)user=> (add-tap (fn [& args] (try (/ 1 0) (catch Exception e (println "oops!\n" (pr-str e))))))
nil
(cmd)user=> (tap> nil)
true
user=> oops!
 #error {
 :cause "Divide by zero"
 :via
 [{:type java.lang.ArithmeticException
   :message "Divide by zero"
   :at [clojure.lang.Numbers divide "Numbers.java" 188]}]
 :trace
 [[clojure.lang.Numbers divide "Numbers.java" 188]
  [clojure.lang.Numbers divide "Numbers.java" 3901]
  [user$eval142$fn__143 doInvoke "NO_SOURCE_FILE" 1]
  [clojure.lang.RestFn invoke "RestFn.java" 408]
  [clojure.core$fn__8821$fn__8822$fn__8824$fn__8831 invoke "core.clj" 7861]
  [clojure.core$fn__8821$fn__8822$fn__8824 invoke "core.clj" 7860]
  [clojure.lang.AFn run "AFn.java" 22]
  [java.lang.Thread run "Thread.java" 829]]}

2021-06-10T18:36:56.037600Z

@endrebak85 maybe you have “stale” namespaces in your REPL that work, and when you run from the command line, it builds fresh namespaces that don’t work?

2021-06-10T18:37:44.039Z

I'm actually surprised tap> swallows stack traces silently, that seems like a big gotcha

2021-06-10T18:38:07.039700Z

Or maybe the namespaces don’t load in the same order? I’d try putting (println ">>>> Here goes tap>") in before your tap> line and see if somehow your tap> is executing before the add-tap.

2021-06-10T18:38:21.040Z

I'm guessing the lack of output from tap> is because of an exception because running the app gives you different data types than the repl experiment

2021-06-10T18:38:44.040300Z

Yeah, the try/catch could be very instructive here.

2021-06-10T18:40:48.041500Z

a simpler way to do it might be (add-tap (fn [& args] (println (try .... (catch Exception e ...))))) then you know you always get a println (unless it's an Error instead of Exception etc.)

Endre Bakken Stovner 2021-06-11T16:11:16.076500Z

You were right, it was due to an error 🙂

2021-06-10T20:55:50.043600Z

Hi, Can someone help me and explain the following behaviour. I evaluate this in the repl and expect to see an error:

(let [foo [1 2 3]]
  (nth foo -1))
I get an error, which is great.
Error: No item -1 in vector of length 3
I evaluate this:
(let [foo [1 2 3]]
  (map-indexed (fn [idx i] (nth foo -1)) foo))
I see no output to the repl at all, no error and no indication its even evaluated it??? I thought it should have generated an error same as above.

alexmiller 2021-06-10T20:56:55.043900Z

map-indexed is lazy

alexmiller 2021-06-10T20:57:10.044400Z

nothing has tried to read the first element yet

2021-06-10T20:57:12.044500Z

ah okay! That explains it God so simple.

alexmiller 2021-06-10T20:57:25.045Z

happens to literally every beginner :)

2021-06-10T20:57:35.045300Z

Thank you

alexmiller 2021-06-10T20:57:37.045500Z

so good job getting it out of the way!

Erik B Good 2021-06-10T21:14:14.046100Z

(map #({:a %1 :b %2}) [1 2 3] [10 12 12]) (map #(assoc {} :a %1 :b %2) [1 2 3] [10 12 12])

Erik B Good 2021-06-10T21:14:30.046600Z

Anyone know why the first line does not work while the second one does?

lucas severo 2021-06-10T21:17:05.046700Z

in the first line you are execing the map as a function, use a do in these cases

lucas severo 2021-06-10T21:17:10.046900Z

like (map #(do {:a %1 :b %2}) [1 2 3] [10 12 12])

alexmiller 2021-06-10T21:20:20.047200Z

or use the hash-map constructor

alexmiller 2021-06-10T21:21:04.047400Z

(map #(hash-map :a %1 :b %2) [1 2 3] [10 12 12])

Chase 2021-06-10T21:50:33.049200Z

Is there a way to call out to an api using curl but do so in clojure? Does that make sense? Specifically a command like this:

curl <https://api.openai.com/v1/engines/davinci/completions> \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"prompt": "This is a test", "max_tokens": 5}'

dpsutton 2021-06-10T21:52:40.049800Z

do you mean make a request to the api from clojure or use curl from clojure?

Chase 2021-06-10T21:54:53.050500Z

well I would make a request to an api but I was being lazy in figuring out how to use the java api library. There are no clojure bindings yet as far as I've found

Chase 2021-06-10T21:55:06.051Z

I haven't done that before but it would be good practice

seancorfield 2021-06-10T21:55:12.051200Z

clj-http or http-kit’s client lib.

seancorfield 2021-06-10T21:55:44.052100Z

(we just switched all our HTTP API calls to use the latter since it has no dependencies and a nice, simple async interface)

Chase 2021-06-10T21:55:52.052300Z

I actually have those currently open on other tabs so I'm on the right path

Chase 2021-06-10T21:58:02.052800Z

so is making a curl request equivalent to a "GET" request?

Chase 2021-06-10T21:58:46.053200Z

I also have mdn's http docs up to try and learn some web basics here

seancorfield 2021-06-10T22:00:51.053800Z

curl with -d is going to be a POST

Chase 2021-06-10T22:01:45.055200Z

awesome. I'll fiddle with that. the response is going to come back as json. What lib do you recommend there. I just saw an article going over all of them

2021-06-10T22:02:10.055800Z

cheshire

seancorfield 2021-06-10T22:02:50.056500Z

(http/post "<https://api.openai.com/v1/engines/davinci/completions>" {:headers {"content-type" "application/json", "authorization" (str "Bearer " your-api-key)} :body "{\"prompt\": \"This is a test\", \"max_tokens\": 5}"}) — something like that

seancorfield 2021-06-10T22:03:18.057100Z

I recommend org.clojure/data.json since it has no dependencies. Cheshire relies on Jackson which has caused me pain.

seancorfield 2021-06-10T22:03:46.057900Z

(we switched from Cheshire to data.json recently, just before we switched from clj-http to http-kit)

Chase 2021-06-10T22:04:01.058200Z

awesome! I will try this out. Thanks folks

Erik B Good 2021-06-10T23:11:33.058300Z

Right I think I understand what you mean by execing the map as a function. Thanks both of you for your answers