This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-06-24
Channels
- # beginners (61)
- # boot (1)
- # cider (55)
- # cljs-dev (17)
- # cljsrn (1)
- # clojure (31)
- # clojure-india (5)
- # clojure-spec (9)
- # clojure-uk (42)
- # cursive (3)
- # datomic (29)
- # editors (3)
- # fulcro (23)
- # hoplon (2)
- # lumo (12)
- # off-topic (8)
- # pedestal (6)
- # re-frame (3)
- # reagent (8)
- # reitit (23)
- # shadow-cljs (82)
- # specter (14)
- # tools-deps (8)
I'm storing maps in an atom and I want to find a map with a given :k-v ... like :post_id 10018 and i'd like to return the whole associated map.
@sova So your atom is a sequence of maps?
(filter (fn [{:keys [post_id]}] (= 10018 post_id)) @my-atom)
-- something like that ? (which would return all matching maps)
yo! that's wonderful
that's exactly right, the atom is a sequence of maps.
Thanks a lot @seancorfield 😃
How to guess a encoded string's encoding method? like base64, etc?
How can i update a map that matches on a particular :k-v, that lives in a sequence of maps?
I'm trying something like (swap! page-threads update-in [:pagename boardname] assoc :thread-id thread-id)
but it's not right
(def threads (atom [{:thread_id "10029"
:posts [17778 17780]}
{:thread_id 10031
:posts [17779]}
{:thread_id 10033
:posts [18881 18882 18883]}]))
@sova two thoughts: given these access patterns, maybe a map of maps would be more appropriate than a sequence of maps. Otherwise, use a vector of maps. Find the index in the vector of the maps you're interested in, then you can update them positionally
(def page-threads (atom [{:pagename "hax"
:threads [10029]}
{:pagename "top"
:threads [10031 10033]}]))
@michael.gaare that makes sense. so find out which index :pagename matches on boardname and then update via index...
these are both vectors of maps, right?
Although they make me kind of nervous. Are you using several atoms to get multiple views of the same data
That's an anti-pattern for atoms - the whole idea is that they provide atomic updates, a guarantee which is lost if you have to coordinate updates across more than one
well it might make sense to have everything live in one atom, but i am using 3 that are each one level deep, instead of one that is 3 levels deep
i would much rather find-and-replace by matching on :keys instead of looking up indices since there is no guarantee the index will not have changed...
but i'm not really sure how to use swap! to "seek and update" a particular {:mappity map}
of course, i'm open to any suggestions, including overall approach 😃
Well take the atom out of the picture for a second. How would you write a function to do this if it was just the map
assoc
seems to be the way to go if the map already exists in the vector, otherwise, conj
methinks
more broadly, you have to two do things in this function - find the thing(s) to update (since you're searching across the vals of a collection of maps), and then do the updates
yes. find index, use index to update?
so if you can write that, there's your swap function, and you don't have to worry about the index or something else changing underneath you since it will happen in an atomic swap operation
I don't know how to get the index for a matching map.
i naively tried (.indexOf v {:matching map}) but that does not seem to work.
since it only needs to be a partial match
on the one key
perhaps something like:
(reduce-kv (fn [_ idx m]
(if (= desired-value (:desired-key m))
(reduced idx)))
nil
vector-of-maps)
this one is probably a little nicer. Returns a collection of all matching indexes:
(keep-indexed (fn [idx m]
(when (= desired-value (:desired-key m))
idx))
vector-of-maps)
how does this even math
(reduce-kv) and (reduced idx) are magical
well, now i have the index, so far so good
(update-in [{:a 0} {:a 0}] [1 :a] inc) => [{:a 0} {:a 1}]
. vectors are associative just like maps. so the "key" for each entry is its index
that is very nice, i'm wrapping my mind around it gradually.
so now that I have a result that I want to store back in at atom, is it advisable to do an atomic replace on the whole atom? what if i have many many entries in my atom?
You should replace your whole atom with a new value (atomically).
using something like reset!
?
What if I want to have a sequence that is precisely count 100, and after reaching 100, the oldest entry gets kicked out once a new entry is added ? Can I achieve this behavior with a queue or is there something more native to clojure?
Perhaps queues are more efficient; I haven't used them frequently
(as-> #queue [1 2 3 4] x (conj x 5) (pop x))
^ this uses the ClojureScript queue literal syntaxThanks @mfikes take
is just what i was looking for. (take 100 (distinct ...))
works very nicely
I am getting confused, requiring it in my core.cljs leads to an error (can't find cognitect/transit.cljs). Should it only be used in the .clj?
which is best language for embedded development node or python? Someone explain briefly ?
Not sure how this is related to #beginners in this Slack group, but I would say neither of the two. Embedded systems are usually very simple computers running only your app (not even OS), so they’re usually written in C that operates very close to the actual hardware 🙂
Hey, I'm trying to use a library yet not sure whats the correct ns for requiring it. the lib is clj-kafka-client
and I tried:
(ns project.core
(:require [clj_kafka_client]))
That didn't work.
Any help would be much appreciated, thanks.it depends on if you want the consumer or producer. you want to require the specific namespace you need to use. most probably https://github.com/ylgrgyq/clj-kafka-client/blob/master/src/clj_kafka_client/consumer.clj#L1