This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-08-27
Channels
- # aleph (1)
- # announcements (5)
- # babashka (13)
- # beginners (68)
- # brompton (7)
- # calva (10)
- # cider (1)
- # clara (15)
- # cljsrn (2)
- # clojure (63)
- # clojure-austin (1)
- # clojure-europe (44)
- # clojure-france (2)
- # clojure-nl (5)
- # clojure-norway (1)
- # clojure-poland (1)
- # clojure-uk (8)
- # clojurescript (8)
- # clojureverse-ops (7)
- # conjure (13)
- # core-async (27)
- # cryogen (10)
- # cursive (17)
- # datomic (13)
- # deps-new (1)
- # events (1)
- # fulcro (3)
- # gratitude (4)
- # helix (6)
- # honeysql (6)
- # introduce-yourself (1)
- # jobs (2)
- # malli (13)
- # meander (9)
- # music (1)
- # news-and-articles (2)
- # off-topic (8)
- # pedestal (1)
- # reitit (4)
- # sci (25)
- # shadow-cljs (13)
- # spacemacs (2)
- # tools-build (5)
- # tools-deps (20)
- # vscode (50)
- # xtdb (2)
Hey team, noob question. Right now, I have a nested data structure of vecs. Example:
[:pair [:symbol "="] [:pair [:symbol "lit"] [:pair [:symbol "clo"] [:pair [:symbol "nil"] [:pair [:symbol "args"] [:pair [:pair [:symbol "if"] [:pair [:pair [:symbol "no"] [:pair [:pair [:symbol "cdr"] [:pair [:symbol "args"] [:symbol "nil"]]] [:symbol "nil"]]] [:pair [:symbol "t"] [:pair [:pair [:symbol "some"] [:pair [:symbol "atom"] [:pair [:symbol "args"] [:symbol "nil"]]]] [:pair [:pair [:symbol "all"] [:pair [:pair [:symbol "fn"] [:pair [:pair [:symbol "_"] [:symbol "nil"]] [:pair [:symbol "id"] [:pair [:symbol "_"] [:pair [:pair [:symbol "car"] [:pair [:symbol "args"] [:symbol "nil"]]] [:symbol "nil"]]]]]] [:pair [:pair [:symbol "cdr"] [:pair [:symbol "args"] [:symbol "nil"]]] [:symbol "nil"]]]] [:pair [:pair [:symbol "and"] [:pair [:pair [:symbol "apply"] [:pair [:symbol "="] [:pair [:pair [:symbol "map"] [:pair [:symbol "car"] [:pair [:symbol "args"] [:symbol "nil"]]]] [:symbol "nil"]]]] [:pair [:pair [:symbol "apply"] [:pair [:symbol "="] [:pair [:pair [:symbol "map"] [:pair [:symbol "cdr"] [:pair [:symbol "args"] [:symbol "nil"]]]] [:symbol "nil"]]]] [:symbol "nil"]]]] [:symbol "nil"]]]]]]] [:symbol "nil"]]]]]]]
Now, is there a way I can pretty print this, to be indented in a readable way? Currently, pprint and println all print in 1 lineHmm... It may have to do with REPL middleware.
I use leiningen to start REPLs. It prints out fine in terminal as well. I've never used clj
so I don't know what the equivalent would be.
Ah here's the https://docs.cider.mx/cider/repl/configuration.html#pretty-printing-in-the-repl in CIDER docs. I'm guessing other tools should have options / configurations in a similar vein.
Oh wait, clojure.pprint/pprint
works fine for me with clj
as well!
I'm out of guesses right now.
pretty printin in Repls is a setting you can configure. Here a couple of links that might help: https://groups.google.com/g/clojure/c/ydtWuNrq_6I https://clojure.org/guides/repl/data_visualization_at_the_repl these helped me with the same problem a few months back.
If I have ([1 0] [2 1] [3 0] [4 1] [5 0] [6 1])
, how can I generate sth like ((1 3 5) (2 4 6))
i.e. group by the second item in the sub-vector.
My idea is to create a key-val pair, where key is the second value of sub-vector, and the val is a empty vector. Then add first value of sub-vector to the empty vector in the map accordingly. (glad to hear a better idea)
What I am stuck in is how to create that key-val pair in advance.
(->> '([1 0] [2 1] [3 0] [4 1] [5 0] [6 1])
(group-by second)
(map val)
(map #(map first %)))
An alternative using for
to make it more readable:
(let [example '([1 0] [2 1] [3 0] [4 1] [5 0] [6 1])
groups (vals (group-by second example))]
(for [group groups]
(map first group)))
Hi all, how to check if
with multiple values?
Not exactly
(if (apply function arg) ....)?
Thanks!
No I mean if the arg has the list of values
(If (function arg) (do this) (do that))
and arg can be 2, 3, 4...
So, it should take each and return the result for each
isn’t this a case for map
?
(map #(if (function %) (do this) (do that)) '(2 3 4 ...))
Not multiple args
arg which is having multiple elements as it's value
ok, what the function
is doing?
arg1 = (1 2 3) arg2= 4 (if (function arg1 arg2) (do this) (do that))
that function returns true or false based on the arg values
This function should execute for each value of arg and return for same
https://clojuredocs.org/clojure.core/map take a look at map function
(function arg1 arg2 ... argn)
is a function of n
arguments. It returns once.
Those arguments might be lists: (x1 y1 ...)
, (x2 y2 ...)
, etc. In which case the function would have to work on (multiple) lists.
But it sounds like what you want is a function which takes a single list argument. Something like (function '(x1 y1 ...))
?
So (if (function '(x1 y1 ...)) (do this) (do that))
?
Maybe https://clojuredocs.org/clojure.core/reduce is your friend.
The situation is to run an if condition with a function and make multiple calls to that function? Example code where arg1 (1 2 3) arg2 (1)
(if (function arg1 arg2)
(perform addition)
(perform subtraction))
It has to execute for arg1 1 and arg2 1, again execute for arg1 2 and arg2 1, and finally for arg1 3 and arg2 1.what should be the return value? if you want a vector of values for each value of arg1
, use map
. if you want to add everything into a single result, use reduce
look at the map function
(map #(if (function % arg2) (perform addition) (perform substraction)) arg1)
map
will iterate over every item in arg1 and execute if expressionFrom the core library - could someone explain the difference between the "name" and "identity" functions in practical terms?
The name
function will return the non-namespace part of a symbol or keyword, or just the argument if the arg is a string. The identity
function will return the given value as is. They are not really related.
Check out https://clojuredocs.org/clojure.core/name and https://clojuredocs.org/clojure.core/identity
thanks. I was using (doc name) and (doc identity) in the REPL to try to understand it. It's helpful to see examples.
hi, i'm trying to write a simple method that will return true iff all given url's are responsive (2xx) my first approach would be using promises with http-kit:
(defn valid-urls? [urls]
(let [callback (fn [{:keys [status body error]}]
(when-not error
(<= 200 status 299)))
futures (doall (map http/get urls (repeat callback)))]
(every? true? (map deref futures))))
i'm wondering:
1. in the impl above - this means for each url i'm creating a new thread -- is the thread number is limited somehow? couldn't find much on http-kit code
2. what would be the core async approach for such task, assuming i have no http-kit promise-like functionality, but simple sync http get method, maybe a simple go routine that writes to channel then read from that channel by main thread?
thxthanks!
Oh strange:
cljs.user=> (let [x "blue"]
#_=> (= x (cljs.reader/read-string (name x))))
false
(Oh wait, i was confusing name
with a different function. But ya, still odd that was false.)
Oh...woops:
cljs.user=> (let [x 'blue]
#_=> (= x (cljs.reader/read-string (name x))))
true
And
cljs.user=> (let [x (symbol "hello world")]
#_=> (= x (cljs.reader/read-string (name x))))
actually i don't think that works
(let [sym (symbol "a/b/c")]
(= sym (read-string (str sym))))
So maybe something like:
(defn valid-id? [id]
(try
(= (name id) (name (read-string (name id)))))
(catch ...
false))
In cljs:
cljs.user=> (let [sym (symbol "a/b/c")]
#_=> (= sym (cljs.reader/read-string (str sym))))
#error {:message "Invalid symbol: a/b/c.", :data {:type :reader-exception, :ex-kind :reader-error}}
What is the best practice to set a default argument with multi-arity and use optional keywords arguments?
;; Code not correct
(defn fullname
([lastname & opts]
(fullname "Mr " lastname opts))
([firstname lastname & {:keys [name-title middlename] :or {name-title nil middlename nil}}]
(str (when name-title (str name-title " "))
firstname
(if middlename middlename " ")
lastname)))
fullname("Potter") ; => Mr Potter
fullname("Albus" "Dumbledore" :name-title "Professor" :middlename "Percival") ; => "Professor Albus Percival Dumbledore"
fullname("Voldemort" :middlename "Lord") ; => Mr Lord Voldemort