How to use / in a Datomic query?
This query
[:find ?p ?af
:where [?p :person/age ?pa]
[?p :person/father ?f]
[?f :person/age ?fa]
[(/ ?pa ?fa) ?af]]
returns only rounded (to 0 decimal places) values for ?af
, so I can't add sth. like
[(< 0.25 ?af)]
[(< ?af 0.5)]
to the :where clause.
I have tried casts to various types inside the query like
[(double ?pa) ?pa2]
but this only affects the result type and never removes the rounding.
If you know something and are experienced, I have quite a few more (especially type related) problems with Datomic. However I have not found a work-around for this division-query problem (except dividing after querying) and I really hope that it is not owed to the early development-stage of Datomic. I'd guess because Datomic does not support the Ratio type, they are rounding, but a few decimal places would be really nice.
I am using Datomic with Clojure.
EDIT I found a work-around for this, however I would still like to know why it is necessary.
Work-around:
Use clojure.core//
[:find ?p ?af
:where [?p :person/age ?pa]
[?p :person/father ?f]
[?f :person/age ?fa]
[(clojure.core// ?pa ?fa) ?af]]
But I certainly want to know which / is called (when invoked without a fully qualified namespace) and where I can find it's documentation so that I can learn about other possible problems with other operators.
EDIT2: Invoking an exception by designing a query to divide by zero, I found out that the static method clojure.lang.Numbers/quotient
is invoked. How should I have known this? It seems like Datomic is not opensource, at least I could not find the source-code in the jar file. Using clojure.tools.trace
on datomic.datalog
, I found out that the invoke happens by resolving /
from datomic.extensions
. In datomic.extions
, /
is defined as clojure.core/quot
, which invokes clojure.lang.Numbers/quotient
.
All this is undocumented as far as I am concerned.
Indeed, it is an undocumented feature. Stuart Halloway has just confirmed this in the Datomic group:
Documentation for the operators available in query is missing, as you discovered. We are working on that, thanks for the report. The / operator currently performs integer division only, and we are looking into ways to make that richer without exposing consumers to a type (ratio) that most JVM languages do not have.
Regarding the correct workaround I will keep this answer updated.
链接地址: http://www.djcxy.com/p/65260.html下一篇: 如何在Datomic查询中使用/?