今日已更新 133 条资讯 | 累计 37382 条内容
关于我们

Quoting Difficulties

Paula Gearon 2026年08月18日 05:13 2 次阅读 来源:Dev.to

In my last post I discussed DSLs for database querying in Clojure . These typically take the form of data structures. I also discussed how some query languages, like SPARQL and Datomic , use variables in their queries, and that these appear in Clojure as symbols . That post also demonstrated using quoting to embed symbols easily into a structure, and unquoting to use values inside those same structures. Some of it got messy. Symbol Reuse A colleague was recently trying to build SPARQL queries using Flint . This is a library that allows SPARQL queries that look very similar to Datomic queries. He was trying to programmatically build query fragments that could be appended to each other to form a complete query. Each fragment was generated by functions that returned a small structure that could be added into the query. In most cases, he could use quoting to return his structure. For instance, the following fragment might be used to find the name of a person who had changed an entity: [[ entity :data/modifiedBy ?person ] [ ?person :data/firstName ?name ]] This is not the final form though, since he wanted to both pass in a value for entity , while also quoting the symbols in his structure. Using the techniques from the last post, this is relatively straightforward: ( defn modifier-name [ entity ] [[ entity :data/modifiedBy '?person ] [ '?person :data/firstName '?name ]]) However, there are occasions where the entity might be modified more than once, and so multiple names should be returned. This will need a configurable name variable: ( defn modifier-name [ entity name-var ] [[ entity :data/modifiedBy '?person ] [ '?person :data/firstName name-var ]]) This would let a developer call something like: ( concat ( modifier-name '?entity '?name1 ) ( modifier-name '?entity '?name2 )) (note: There are overlaps between what ?name1 and ?name2 will bind to. This is just for illustration.) Autogensym Unfortunately, this has a bug. In both cases, the ?person variable is used, meanin

本文内容来源于互联网,版权归原作者所有
查看原文