This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-11-30
Channels
- # beginners (126)
- # boot (2)
- # cider (6)
- # cljs-dev (46)
- # cljsjs (8)
- # clojure (122)
- # clojure-greece (57)
- # clojure-italy (6)
- # clojure-poland (3)
- # clojure-russia (2)
- # clojure-serbia (5)
- # clojure-spec (26)
- # clojure-uk (99)
- # clojurescript (39)
- # cursive (15)
- # datascript (7)
- # datomic (21)
- # dirac (7)
- # duct (1)
- # emacs (19)
- # fulcro (58)
- # garden (4)
- # graphql (15)
- # hoplon (2)
- # immutant (1)
- # instaparse (3)
- # jobs (5)
- # juxt (15)
- # klipse (4)
- # leiningen (2)
- # lumo (52)
- # off-topic (8)
- # om (27)
- # onyx (22)
- # other-languages (3)
- # portkey (2)
- # protorepl (2)
- # re-frame (7)
- # reagent (7)
- # ring (11)
- # rum (7)
- # shadow-cljs (114)
- # spacemacs (20)
- # specter (16)
- # test-check (5)
- # timbre (1)
- # unrepl (43)
- # yada (17)
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@eoliphant you're trying to insert before the map with :group/id=2
, regardless of where it is in that sequence?
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))
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]
using end-fn
also avoids computing the index twice
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
any new way of thinking takes time to learn
eventually it becomes really easy
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?
index-nav
can be used to move an element from one location in a sequence to another
otherwise you'll have to do removal and addition separately