簡體   English   中英

clojure.core / for與Cypher ExecutionResult

[英]clojure.core/for with Cypher ExecutionResult

(defn cypher
  [query]
  (let [result (-> *cypher* (.execute query))]
    (for [row result
          column (.entrySet row)]
      {(keyword (.getKey column))
       (Neo4jVertex. (.getValue column) *g*)})))

repl=> (cypher "start n=node:people('*:*') return n")
{:n #<Neo4jVertex v[1]>}

該查詢返回兩個結果,但是我只能使用clojure.core/for查看一個結果。 我應該怎么做?

Neo4j文檔具有以下示例(這是我要模擬的示例):

for ( Map<String, Object> row : result )
{
    for ( Entry<String, Object> column : row.entrySet() )
    {
        rows += column.getKey() + ": " + column.getValue() + "; ";
    }
    rows += "\n";
}

我認為您需要使用clojure.core/doseqdocs )。

user=> (doseq [row [1 2 3]]
  #_=>        [result [4 5 6]]
  #_=>   (println (str {:row row :result result}))))
{:row 1, :result 4}
{:row 1, :result 5}
{:row 1, :result 6}
{:row 2, :result 4}
{:row 2, :result 5}
{:row 2, :result 6}
{:row 3, :result 4}
{:row 3, :result 5}
{:row 3, :result 6}

因此,根據您的示例,可能會執行以下操作:

; ...
(doseq [row result]
       [column (.entrySet row)]
  (println (str {(keyword (.getKey column)) (Neo4jVertex. (.getValue column) *g*)}))))
; ...

注意, doseq返回nil 您必須在doseq表單的主體中調用諸如println之類的doseq

看起來clojure.core/for確實具有列表理解功能,因此類似下面的內容實際上返回一個列表:

user=> (for [row [1 2 3]
  #_=>       result [4 5 6]]
  #_=>   {:row row :result result})
({:row 1, :result 4} {:row 1, :result 5} {:row 1, :result 6} {:row 2, :result 4} {:row 2, :result 5} {:row 2, :result 6} {:row 3, :result 4} {:row 3, :result 5} {:row 3, :result 6})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM