specter

Latest version: 1.1.3
mbjarland 2018-01-01T20:29:15.000041Z

how would I go about calculating the sum of the string lengths in the following data structure (oh and yes, Nathan, I did cave in and pull in specter into my project, the convenience is just too alluring):

[{:delim ["-"]} {:align :L} {:delim ["-" :F "-"]} {:align :R} {:delim ["-"]}]
I’m assuming using:
(specter/traverse-all [ALL :delim ALL string?])
together with transduce would somehow do the trick? the answer from the above data structure should be 4 as there are 4 strings of length 1 in the :delim maps in the exapmle

mbjarland 2018-01-01T20:31:02.000001Z

I suspect I’m just not friendly enough with transducers and am tripping over myself here

mbjarland 2018-01-01T20:38:41.000040Z

one way would be to step out from specter and do:

(reduce + 
        (map count 
          (specter/select [ALL :delim ALL string?] my-d)))
but I figured there might be some way of being even terser here

nathanmarz 2018-01-01T20:44:20.000013Z

@mbjarland this works: (reduce + (traverse [ALL :delim ALL string? (view count)] data))

nathanmarz 2018-01-01T20:44:49.000021Z

using view lets you avoid needing transducers to make this efficient