specter

Felix Dorner 2024-07-04T12:23:52.420379Z

When I use (transform [...] f data) and the path yields 0 results, so my f is never called, how can I detect that and for example throw an exception?

Felix Dorner 2024-07-04T13:45:04.291449Z

(let [path ...] (select-one! p data) (transform p f data)) looks weird too 😕

mmer 2024-07-04T14:31:01.216939Z

I guess the transform function is designed to go through a set of data and transform the found entries. And then return the result. Not sure why you might expect it to run f if the path fails to match. Could you test afterwards whether the data had been changed? Perhaps comparing it with the previous data.

nathanmarz 2024-07-04T14:41:40.861309Z

you could do something like (let [vol (volatile! false)] (transform ... (fn [v] (vreset! vol true) (actual-transform-fn v)) data))

Felix Dorner 2024-07-04T15:59:39.534509Z

Okay yeah, works just fine.

Felix Dorner 2024-07-04T17:02:15.611649Z

Would you then also use the volatile trick to transform only the first match in case multiple elements are selected? I can't seem to solve the simplest stuff now 😞. E.g. can you give an example of run transform on the first even element of (range 10)

Felix Dorner 2024-07-04T17:02:50.568269Z

I mean I can do it with the volatile trick, but isnt there a way to do it with navigators?

Felix Dorner 2024-07-04T17:08:09.071449Z

Hmmm, filterer seems promising.

nathanmarz 2024-07-04T19:16:12.971189Z

there's a similar example on Specter's README for incrementing the last odd number in a sequence: (transform [(filterer odd?) LAST] inc data)

👍 1