Fork me on GitHub
#specter
<
2017-11-30
>
eoliphant20:11:07

Hi, I have the following data

(def testtree    {:form/id 1
                  :form/name "Test Form"
                  :form/description "My Description"
                  :form/version 1
                  :form/groups [{:group/id    2
                                 :group/title  "Page 1"
                                 :group/type   :page
                                 :group/layout :vertical
                                 :group/fields []}
                                {:group/id    4
                                 :group/title  "Page 1"
                                 :group/type   :page
                                 :group/layout :vertical
                                 :group/fields []}]}
  )
to insert a new map in form/groups I’m doing the following:
(sp/setval [:form/groups
            ;sp/ALL 
            (sp/srange-dynamic
              (fn [x] (first (keep-indexed #(when (= (:group/id %2) 2) %1) x)))
              (fn [x] (first (keep-indexed #(when (= (:group/id %2) 2) %1) x)))
              )]
          [ {:group/id 4
            :group/title "Inserted"}]
           testtree )
Which generally does what I want, but I’m just wondering if there’s any cleaner (more specterish) way to compute the indicies for the srange-dynamic

nathanmarz21:11:49

@eoliphant you're trying to insert before the map with :group/id=2, regardless of where it is in that sequence?

nathanmarz21:11:17

you can factor out that sort of navigation like this:

(defn ^:direct-nav before-index-dynamic [p]
  (path
    (srange-dynamic
      (fn [aseq] (first (keep-indexed (fn [i e] (if (p e) i)) aseq)))
      (end-fn [_ s] s))
    BEFORE-ELEM))

nathanmarz21:11:47

then you can use it like this:

user=> (setval (before-index-dynamic #(= % 2)) :a [1 2 4 9 2 10])
[1 :a 2 4 9 2 10]

eoliphant21:11:05

ah gotcha. Yes that’s much cleaner 🙂

nathanmarz21:11:36

using end-fn also avoids computing the index twice

eoliphant21:11:03

yeah that’s cool, that was annoying me as well

eoliphant21:11:10

it’s funny, I first started playing with specter a month or 2 ago and it kind of gave me a migraine lol. But sometimes you have to step away, and come back. Just picked it back up this morning and blew through a couple scenarios quite easily

nathanmarz21:11:04

any new way of thinking takes time to learn

nathanmarz21:11:17

eventually it becomes really easy

eoliphant22:11:22

yeah definitely grokking it better now. One more quick question, this data structure is for an interactive form builder. So as you can see a group has fields, etc. There will cases where say you move a field from one group to another. Is there a way to ‘move’ something from one path to another, or do the delete and add together, kind of transactionally?

nathanmarz22:11:29

index-nav can be used to move an element from one location in a sequence to another

nathanmarz22:11:49

otherwise you'll have to do removal and addition separately

eoliphant22:11:09

Ok, will give that a whirl