0

(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

hzhu
  • 3,759
  • 5
  • 29
  • 39

1 Answers1

2

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]))
xsc
  • 5,983
  • 23
  • 30
  • I had a map, and did a `(sort-by last {map})` which returned an `cljs.core/IndexedSeq` I'm trying `(get-in @app-state ["my-seq" :by-uid uid])` but it's returning nil. – hzhu Jan 22 '15 at 01:46
  • My second suggestion should work then - basically: `(first (filter (comp #{uid} first) (get @app-state "my-seq")))`. But I recommend that you store your data according to its use, e.g. if you need lookup by UID store it as a map, but if you need to access it in-order use a seq. If you need both, use both representations (which was the first suggestion in my answer). – xsc Jan 22 '15 at 09:09