Is there a good video or guide on the :result-transform syntax/use? I haven’t found one yet
How could I combine the below two lines into a single :result-transform so that the tasks are sorted by Priority first (A → B), then by oldest journal entry to newest?
:result-transform (fn [result] (sort-by (fn [r] (get-in r [:block/page :block/journal-day])) result)) ; Sort by the journal date
@Siferiax and others have shared fantastic work related to queries for task management in Queries for task management. Consider looking through that discussion for many outstanding examples.
To combine sort-by you can use juxt. Look also into map.
A variation of the following might do what you need:
:result-transform (fn [result] (sort-by
(juxt
(fn [h] (get h :block/priority))
(fn [r] (get-in r [:block/page :block/journal-day]))
)
))
EDIT: for anyone looking at this in the future, note the correction by @Siferiaxbelow. It should be:
:result-transform (fn [result] (sort-by
(juxt
(fn [h] (get h :block/priority))
(fn [r] (get-in r [:block/page :block/journal-day]))
)
result
))
I have looked at those and used some, but I do not understand fully how :result-transform works, let along juxt.
@Siferiax - Would you be able to explain how to modify your examples to be used in this example? I did try the provided, but it errors out. Just don’t understand if there has to be any relation between the two or from above…
The example is missing the result line. Should be like this:
:result-transform (fn [result] (sort-by
(juxt
(fn [h] (get h :block/priority))
(fn [r] (get-in r [:block/page :block/journal-day]))
)
result
))
(fn) Is a anonymous function. It takes parameters as in put and uses those in the function.
So the result transform has a (fn [result]) where result is the input. Basically it is your query result. The term used here is arbitrary.
So in its most basic form we can have a result transform of :result-transform (fn [result] result)
Then we can start to add functions to it. In the case the sort-by function. This has a syntax of (sort-by keyfn coll)
Or in simpler terms what you want to sort by and what you wish to sort.
In the example the what you wish to sort was missing, that is, the result.
For juxt it is simply multiple of those keyfn as the sort by. It’s short for juxtaposition.
Can find more on those functions in the clojure docs. Just be mindful that not everything stated there is available in Logseq. It’s a good place to start learning about what examples on this forum do/how they work. (At least in terms of result-transform) https://clojuredocs.org/