specter

Latest version: 1.1.3
unbalanced 2018-09-03T02:10:39.000100Z

specific and general question. General question: where does one go to level up in specter? I feel like I've already tapped out on talks/tutorials and I know I'm only scratching the surface. Case in point -- Specific question: given

(def contrived-data {:time 1535940129129
                       :data [{:a [{:start 389
                                    :stop  456}]}
                              {:b [{:start 113
                                    :stop  889}]}]})
I'm attempting to sort by :start. It's blessedly simple to select the values via
(sm/select [:data s/ALL s/MAP-VALS s/ALL :start] contrived-data)
my intuition to do the operation would be
(sm/transform [:data s/ALL s/MAP-VALS s/ALL :start] sort contrived-data)
but I realized my thinking may have gone awry somewhere along the way

nathanmarz 2018-09-03T02:13:52.000100Z

@goomba what do you want the result of that transform to be?

unbalanced 2018-09-03T02:16:19.000100Z

unbalanced 2018-09-03T02:16:24.000100Z

sorry, should've posted that

unbalanced 2018-09-03T02:16:42.000200Z

as in, :b should come first because its :start value is lower

nathanmarz 2018-09-03T02:17:36.000200Z

what if the inner collection has multiple :start values?

nathanmarz 2018-09-03T02:17:43.000100Z

use the lowest?

unbalanced 2018-09-03T02:17:58.000100Z

sure 😅

unbalanced 2018-09-03T02:18:12.000100Z

hadn't thought that far ahead with my contrived example

unbalanced 2018-09-03T02:18:52.000100Z

it's the fact that the keys :a and :b "pre-fix" the data makes any sort of normal update-in/`sort-by` completely unusable, at least with any sort of elegance/performance

unbalanced 2018-09-03T02:19:12.000100Z

but I encounter this problem all the time

nathanmarz 2018-09-03T02:22:18.000100Z

you can use something like (fn [m] (reduce min (traverse [MAP-VALS ALL :start] m)) for a "keyfn" to sort-by

nathanmarz 2018-09-03T02:23:16.000100Z

and then (transform :data #(sort-by keyfn %) data)

nathanmarz 2018-09-03T02:24:55.000100Z

beware that sort-by will change that vector to a list

unbalanced 2018-09-03T02:25:15.000100Z

ohhhh mannn that is cooooolllllll 😄

unbalanced 2018-09-03T02:27:11.000100Z

thank you! how did you... well, I guess because you wrote it, I would've thought to use select instead of traverse

nathanmarz 2018-09-03T02:28:09.000100Z

always use traverse if all you'll be doing is a reduce over the result

1
nathanmarz 2018-09-03T02:28:34.000100Z

more efficient since no intermediate list is materialized

unbalanced 2018-09-03T02:30:05.000100Z

🙏 fantastic. Thanks again.