Database Reference
In-Depth Information
Creating ilter operators
Filter operators remove rows from the output in the map phase. They take one item,
and they return a Boolean. If true, the item should be included in the results; if false,
then the item should be held back. For example, this ilter returns true if the input is an
even number, and we use it in a query to return only the companions who accompanied
an even number of doctors:
(deffilterfn is-even? [x] (even? x))
(?<- (stdout)
[?companion ?dr-count]
(doctor ?companion _)
(c/count ?dr-count)
(is-even? ?dr-count))
Creating buffer operators
Buffers operators work in the reduce phase. They process a group of rows as a single input.
They take an entire list of input rows to process and return one or more items for the output.
For example, this buffer operator takes rows of strings and returns the total number of
characters in all the strings. We use it in the query to count the characters in the full names
of the companions for each doctor:
(defbufferfn count-chars [strings]
[(reduce + 0 (mapcat #(map count %) strings))])
(?<- (stdout)
[?dr ?companion-chars]
(doctor ?c ?dr)
(full-name ?c ?name)
(count-chars ?name :> ?companion-chars))
Creating aggregate operators
Aggregate functions work in the reduce phase to combine input rows into one value.
Compared to buffer operators, aggregate operators are in some ways more lexible—they
can be used with other aggregators—but are more restricted in other ways.
Each aggregator function has to be able to be called with no parameters, one parameter, or
two parameters. The call with no parameters returns the initial value, while the call with one
parameter takes the aggregator's state and returns the inal value. Moreover, the call with two
parameters takes the state and a new value and folds the two together into a new state.
For example, this returns the average length of the names of each doctor's companions:
(defaggregatefn mean-count
([] [0 0])
([[n total] string]
 
Search WWH ::




Custom Search