This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-06-10
Channels
- # announcements (2)
- # asami (2)
- # babashka (29)
- # beginners (115)
- # cider (5)
- # clara (9)
- # cljdoc (14)
- # cljs-dev (1)
- # cljsrn (18)
- # clojars (3)
- # clojure (73)
- # clojure-australia (7)
- # clojure-europe (74)
- # clojure-nl (3)
- # clojure-norway (8)
- # clojure-spec (3)
- # clojure-uk (36)
- # clojured (1)
- # clojurescript (15)
- # conjure (18)
- # datomic (6)
- # deps-new (11)
- # depstar (7)
- # fulcro (2)
- # instaparse (1)
- # jobs (9)
- # nrepl (8)
- # off-topic (21)
- # pathom (5)
- # polylith (42)
- # proletarian (1)
- # rdf (10)
- # re-frame (2)
- # react (1)
- # reagent (20)
- # releases (3)
- # remote-jobs (4)
- # rum (9)
- # shadow-cljs (79)
- # sql (11)
- # tools-deps (64)
- # vim (3)
- # xtdb (26)
hello, could you please tell me where find a tutorial for create an API REST
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
2 links there, one is the tutorial, one is the source code of the main core.clj file
if you need any help feel free to ask in the channel ! ^.^
thank so much @U3ES97LAC 😉
its great to be here...!😃
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
If you haven’t asked there already, you may have a better chance of getting answers in #cider and/or #doom-emacs
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"}
Character literals are preceeded by a backslash in Clojure:
{\; "SemiColon" \( "OpeningParenthesis"}
I have json structure like below
{
"fruit": "Apple",
"size": "Large",
"color": "Red"
}
{
"fruit": "orange",
"size": "Small",
"color": "Red"
}
(reduce
(fn [acc {:strs [fruit size]}]
(-> acc
(update "fruit" (fnil conj []) fruit)
(update "size" (fnil conj []) size)))
{}
[{...}])
as an example of using reduceupdate-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
just use update
could you share your code?
(reduce
(fn [acc dt]
(-> acc
(update "fruits" (fnil conj []) (:fruits dt))
(update "size " (fnil conj []) (:size dt ))))
{}
data)
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 ?I wrote in below way
(defn hellointepe [data]
(reduce-kv
(fn [acc k v]
(assoc acc k (interpose "," v)) )
{}
data))
you could use zipmap:
(zipmap (keys hellovar)
(map (partial interpose ",")
(vals hellovar)))
but your reduce-kv looks fine, too
I am trying to type cast each value like (map Integer. (get fruts :size))
this giving syntax error at Integer.
any reason?
if your fruits is something like [{:size "90" :name "apple"} {:name "banana" :size "89"}]
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.
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
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
Thanks @U051SS2EU, helped
Any one aware of https://github.com/korma/Korma ?
This is a question of personal preferences but in my opinion honeysql is much more “clojure” than korma - https://github.com/seancorfield/honeysql
or if you want to try sql from a higher level of abstraction, there's http://walkable.gitlab.io
is there a wrapping library for clojure.string that makes the functions nil-pun?
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?
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
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
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?
can you build the select operation programmatically, at run time? collapsing the 10,000 into 1
or maybe i misunderstand the goal
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
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
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
I have passed using In operation for database ( passing multiple argument at same time)
if you want to call the same function multiple times with different arguments, map
works
that's what map
repeatedly
and run!
and doseq
are for, but it's a bad idea to do this with db calls
it depends on the context
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?
the answers to those questions are how you decide between map, repeatedly, run!, doseq, iterate, etc. etc.
(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)
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
there's no one size fits all answer
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
is there any repetition in the calls or repetition in the result you can exploit
@U01J3DB39R6 if yes to all of those, use map
if it's not io / side effecting / manipulating state, and mapv
if it is
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?Doing it in the REPL works:
user> (add-tap #(println "----" % "----"))
;; => nil
user> (tap> "hi")
;; => true---- hi ----
"hi"
I'm sure my add-tap was triggered since I added this line above it:
(println "adding tap****************")
(add-tap tap)
it could be your tap function is crashing, I'm not sure you'd see a stack trace if it did
you could put a try / catch with a println in the catch to check
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]]}
@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?
I'm actually surprised tap> swallows stack traces silently, that seems like a big gotcha
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
.
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
Yeah, the try/catch
could be very instructive here.
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.)
You were right, it was due to an error 🙂
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.map-indexed is lazy
nothing has tried to read the first element yet
happens to literally every beginner :)
so good job getting it out of the way!
(map #({:a %1 :b %2}) [1 2 3] [10 12 12])
(map #(assoc {} :a %1 :b %2) [1 2 3] [10 12 12])
in the first line you are execing the map as a function, use a do
in these cases
like (map #(do {:a %1 :b %2}) [1 2 3] [10 12 12])
or use the hash-map
constructor
(map #(hash-map :a %1 :b %2) [1 2 3] [10 12 12])
Right I think I understand what you mean by execing the map as a function. Thanks both of you for your answers
Anyone know why the first line does not work while the second one does?
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 \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"prompt": "This is a test", "max_tokens": 5}'
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
clj-http
or http-kit
’s client lib.
(we just switched all our HTTP API calls to use the latter since it has no dependencies and a nice, simple async interface)
curl
with -d
is going to be a POST
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
cheshire
(http/post "
— something like that
I recommend org.clojure/data.json
since it has no dependencies. Cheshire relies on Jackson which has caused me pain.
(we switched from Cheshire to data.json recently, just before we switched from clj-http to http-kit)