Search code examples
data-structuresclojuresequenceclojurescriptseq

Clojurescript, how to access a map within a indexed sequence


(println (get-in @app-state ["my-seq"]))

Returns the following sequence with type cljs.core/IndexedSeq

([-Jg95JpA3_N3ejztiBBM {create_date 1421803605375, 
  website "www.example.com", first_name "one"}]
 [-Jg95LjI7YWiWE233eK1 {create_date 1421803613191, 
  website "www.example.com", first_name "two"}] 
 [-Jg95MwOBXxOuHyMJDlI {create_date 1421803618124,     
  website "www.example.com", first_name "three"}])

How can I access the maps in the sequence by uid? For example, the map belonging to
-Jg95LjI7YWiWE233eK1


Solution

  • If you need the order of the data, you have the following possibilities:

    1. Store the data once in order and once as a map. So, when adding a new entry, do something like:

      (defn add-entry
        [uid data]
        (swap! app-state update-in ["my-seq"]
               #(-> %
                    (update-in [:sorted] conj data)
                    (update-in [:by-uid] assoc uid data))))
      

      With lookup functions being:

      (defn sorted-entries
        []
        (get-in @app-state ["my-seq" :sorted]))
      
      (defn entry-by-uid
        [uid]
        (get-in @app-state ["my-seq" :by-uid uid]))
      

      This has the best lookup performance but will use more memory and make the code a little bit more complex.

    2. Search the entry in the seq:

      (defn entry-by-uid
        [uid]
        (->> (get @app-state "my-seq")
             (filter (comp #{uid} first))
             (first)))
      

      In the worst case, this has to traverse the whole seq to find your entry.

    If order does not matter, I recommend storing the data as a map in the first place:

    (defn add-entry
      [uid data]
      (swap! app-state assoc-in ["my-seq" uid] data))
    
    (defn entry-by-uid
      [uid]
      (get-in @app-state ["my-seq" uid]))