The function where
suport operators "in"
and "not in"
, but opersator "not intersect"
is not supported.
In some cases, a negative operator "intersect"
is needed.
Is there a way to achieve this in 2023?
where
supports "intersect"
but there’s no equivalent "not intersect"
I’m aware of
for instance, if I want to exclude posts that have a particular tags:
{{ $pages := where .Site.RegularPages "Params.tags" "not intersect" (slice "tag1" "tag2") }}
A workaround with symdiff
.
{{ $pages := where .Site.RegularPages "Params.tags" "intersect" (slice "tag1" "tag2") }}
{{ $pages = $pages | symdiff .Site.RegularPages }}
Just adding my +1 for this feature
this seems awfully inefficient. the operator is very much needed, I had that need myself a few days ago.
What would be appreciated too, is an operator returning a truth value for “do these collections intersect or not”. this would mirror the in
/ not in
semantic perfectly.
Or better, extend in
to collections indifferently, so it becomes do these elements (possibly singular) figure in that set. But I doubt we’re gonna see something so revolutionary any time soon.
Why? An empty slice is falsy, so…
{{ $s1 := slice "a" "b" }}
{{ $s2 := slice "b" "c" }}
{{ if intersect $s1 $s2 }}
They intersect.
{{ else }}
They do not intersect.
{{ end }}
An empty slice is falsy. A slice with one or more elements is truthy.
ah ! I forgot ! It’s difficult to stop thinking in terms of type (to me a set is a set, not a truth value), because quite regularly the syntax comes back to bite me, complaining about said types. Sigh…
Anyway, thanks. It will come in handy.