Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/rethinkdb/query.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,11 @@
[sq x-or-func]
(term :CONTAINS [sq x-or-func]))

(defn fold
"Apply a function to a sequence in order, maintaining state via an accumulator.
The fold command returns either a single value or a new sequence."
[sq base func & [optargs]] (term :FOLD [sq base func] optargs))

;;; Document manipulation

(defn pluck
Expand Down
1 change: 1 addition & 0 deletions src/rethinkdb/types.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"LIMIT" 71,
"OFFSETS_OF" 87,
"CONTAINS" 93,
"FOLD" 187,
"GET_FIELD" 31,
"KEYS" 94,
"VALUES" 186,
Expand Down
10 changes: 9 additions & 1 deletion test/rethinkdb/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,15 @@
(r/max [4 6]) 6
(r/distinct [1 6 1 8]) [1 6 8]
(r/contains [1 6 1 8] 1) true
(r/contains [1 6 1 8] 2) false)))
(r/contains [1 6 1 8] 2) false
(r/fold [1 6 1 8] 0 (r/fn [acc number] (r/add acc number))) 16
(r/fold [1 6 1 8] 0 (r/fn [acc number] (r/add acc 1)) ; increment accumulator
{:emit (r/fn [acc number new-acc]
(r/branch (-> new-acc (r/mod 2) (r/eq 0))
[number]
[])) ; select elements on even positions
:final-emit (r/fn [acc] [100])}) [6 8 100] ; append 100 to the end
)))

(deftest changefeeds
(with-open [conn (r/connect)]
Expand Down