Does DataScript have a set difference operator?
i.e. every element of result set A that is not also in result set B?
In this rule:
[(subproject-or-self ?e1 ?e2)
[?e1]
(subproject ?e1 ?e2)]]
What does [?e1]
do? I’ve yet to encounter that syntax in my limited experimentations with Datascript.@d4hines The intention was that subproject-or-self
evaluates to either ?e1 or subprojects thereof.
Though I just realise it should be [?e2] instead.
@urzds What does that [ ]
syntax do?
Does it just fill in attribute and value with blanks?
@d4hines I believe so. I just took the recursive example from the datascript readme and adapted it to my needs as appeared logical to me. Lucky me: It worked. 😉
I think rules exist to create relations automatically? So [?e1] is a relation. The one with AVT=blank. That's how I understand it, without being an expert.
That’s definitely how I would read that, but what does [?e1 _ _]
buy you? That there’s an entity with that id? isn’t that implicit in the query?
Well, I can tell you that (subproject ?e ?existing-project)
finds you all subprojects ?e, but not including ?existing-project.
So I added another rule that would also match ?existing-project in addition to the subprojects the other rule found. I kept subproject
and subproject-or-self
separate in order to prevent funny stuff from happening (infinite recursions?), without having a specific reason.
I guess I’m not convinced that this query:
[(subproject-or-self ?e1 ?e2)
[?e2]
(subproject ?e1 ?e2)]]
could ever return a different set than this query:
[(subproject-or-self ?e1 ?e2)
(subproject ?e1 ?e2)]]
Or indeed, that the addition of any form with the pattern [?e]
could ever change any result set.And I’m interested because if I’m wrong, I need to reevaluate how I think queries…evaluate.
Maybe I really should write some tests that check these assumptions...
How else would you answer my question from yesterday? How to make ?project the union of ?top-level-project and some list of [?subproject]s?
I would have tried
[(or [?project :unique-id ?top-level-project-id] (subproject ?project ?top-level-project-id)]
But I’m new at this - that’s just a hunch.
You’ve gotta just try this at the REPL.
I think I got error messages regarding unbound variables when I did it with or
. So I tried or-join
, but that resulted in what I posted above.
Gotcha. Reading, the docs on or-join
, that makes sense now. This has been a helpful excursion - thanks for sharing it with us!
It’s probably just so much spam for the ol’ timers, but I know I learned something 😛