This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-12-12
Channels
- # adventofcode (49)
- # announcements (22)
- # beginners (33)
- # calva (5)
- # clj-kondo (6)
- # clojure (30)
- # clojure-europe (6)
- # clojure-japan (1)
- # clojure-nl (9)
- # core-logic (17)
- # data-oriented-programming (1)
- # datalevin (1)
- # datomic (2)
- # gratitude (2)
- # jobs (2)
- # kaocha (8)
- # lsp (1)
- # malli (8)
- # meander (8)
- # off-topic (18)
- # rdf (1)
- # re-frame (4)
- # reagent (16)
- # releases (1)
- # sci (1)
- # sql (1)
- # tools-deps (4)
- # xtdb (3)
(instance? clojure.lang.IPersistentStack (clojure.lang.PersistentQueue/EMPTY))
returns true.
Isn't this a bug, because peek
and pop
aren't doing the right thing according to the stack's interface?
It does seem a bit odd to me.
The method names and signatures are the same for lists and queues, of course, which is probably why it was coded this way.
(Note: I have never asked this question of anyone who actually wrote the code and/or knew if there was a deeper reason to make that class arrangement as it is, other than common method names and signatures.)
Given that (a) the core team strongly desires to maintain backwards compatibility with earlier Clojure releases and (b) PersistentQueue is less used than most other kinds of Clojure collections, and (c) When PersistentQueue is used, it seems unlikely that many people would use the result of (instance? clojure.lang.IPersistentStack foo) to determine the behavior of their code, it seems unlikely to change.
But if you want a more authoritative answer than mine, you could try asking your question on https://ask.clojure.org/
by the way clojure.lang.PersistentQueue/EMPTY
is a static value, not a function or method, parenthesizing it works for the same reason (Math/PI)
does - it's an incidental feature of how interop forms are expanded
@U883WCP5Z what definition of the interface are you going by? there's no commentary in the code for the interface
PersistentQueue extends PersistentList, and PersistentList extends IPersistentStack, IIRC
both PersistentList and IPersistentList implement IPersistentStack, PersistentQueue extends IPersistentList
(ins)user=> (contains? (supers clojure.lang.PersistentList) clojure.lang.IPersistentStack)
true
(ins)user=> (contains? (supers clojure.lang.PersistentQueue) clojure.lang.PersistentList)
false
(ins)user=> (contains? (supers clojure.lang.PersistentQueue) clojure.lang.IPersistentList)
true
(ins)user=> (contains? (supers clojure.lang.IPersistentList) clojure.lang.IPersistentStack)
true
Doing todays advent of code, I'm in a situation where I have
[{"start "A"} {"start "b"} {"A" "b"} {"A" "c"} {"A" "end"}]
Is their a way I can merge those into one map where I get a vector for the value
i.e.
{"start" ["A" "b"]
"A" ["b" "c" "end"]}
?
I thought merge, but I'm unclear. Also tried merge-with conj
but that didn't work either. Also tried group-by key
Take a look at https://clojurians.slack.com/archives/C053AK3F9/p1639156872127500
merge-with is indeed the answer, but you need to flip the parameters, take a better look at the docstring (nevermind, you've edited your message, it's ok now)
If some-fn
is like an aggregate or
and every-pred
is an aggregate and
, is there any function like no-pred
or not-any-fn
that I'm not seeing? Or do I just use (not (some-fn p1 p2...))
?
not-any? ?
I thought not-any
applies a single predicate to all elements in a collection. How would I use it to apply multiple predicates to a single value?
• (map (partial (some-fn odd? neg-int?)) [-2 -1 1 2])
returns (true true true false)
• (map (partial (every-pred odd? neg-int?)) [-2 -1 1 2])
returns (false true false false)
• (map (partial (complement (some-fn odd? neg-int?))) [-2 -1 1 2])
returns (false false false true)
<-- I don't see how to use not-any
here.
You're right, @sritchie09, that the following works, but I don't think it's worth it. 🙂
• (map (partial ((comp complement some-fn) odd? neg-int?)) [-2 -1 1 2])
returns (false false false true)
Are you sure you need the partial?
Or just (def no-pred (comp complement some-fn))
and then use that…?
Yup that's what I was suggesting!
@U01HHBJ56J1 partial is not doing anything here btw
So I have a map of maps, I want to assoc a new key into the submaps, but something like (map #(assoc % new-key new-value) the-map)
says "assoc needs integer for Key"
Oh I think I see what I'm doing wrong. Need to provide a key to assoc to
Mapping over a map is mapping over key value pairs and it seems like vectors at this point. You can assoc on vectors but understandably an index is the key
Interesting
So mapping over a map is actually mapping over key-value vector
will have to tuck that away into the ol' brain machine
Alright, feelin' like a total newb
(def om {"bob" {:name "bob"
:age 21
:hobbies "fuudbaal"}
"jill" {:name "jill"
:age 33
:hobbies "hotness"}
"rhona" {:name "rhona"
:age 26
:hobbies "surfing"}})
(map
(fn [m [k v]]
(assoc-in m [k :email-verified?] false))
om)
Trying to add an email-verified?
key to each submap@sova use reduce-kv
instead of map
.