Liking cljdoc? Tell your friends :D

clara.rules.accumulators

A set of common accumulators usable in Clara rules.

A set of common accumulators usable in Clara rules.
raw docstring

accumclj/s

(accum {:keys [initial-value reduce-fn combine-fn retract-fn convert-return-fn]
        :as accum-map})

Creates a new accumulator. Users are encouraged to use a pre-defined accumulator in this namespace if one fits their needs. (See min, max, all, distinct, and others in this namespace.) This function exists for cases where a custom accumulator is necessary.

The following properties are accepted.

  • An initial-value to be used with the reduced operations.
  • A reduce-fn that can be used with the Clojure Reducers library to reduce items.
  • An optional combine-fn that can be used with the Clojure Reducers library to combine reduced items.
  • An optional retract-fn that can remove a retracted fact from a previously reduced computation.
  • An optional convert-return-fn that converts the reduced data into something useful to the caller. Simply uses identity by default.
Creates a new accumulator. Users are encouraged to use a pre-defined
accumulator in this namespace if one fits their needs. (See min, max, all,
distinct, and others in this namespace.) This function
exists for cases where a custom accumulator is necessary.

The following properties are accepted.

* An initial-value to be used with the reduced operations.
* A reduce-fn that can be used with the Clojure Reducers library to reduce items.
* An optional combine-fn that can be used with the Clojure Reducers library to combine reduced items.
* An optional retract-fn that can remove a retracted fact from a previously reduced computation.
* An optional convert-return-fn that converts the reduced data into something useful to the caller.
  Simply uses identity by default.
 
sourceraw docstring

allclj/s

(all)
(all field)

Returns an accumulator that preserves all accumulated items. If given a field, returns all values in that field.

Returns an accumulator that preserves all accumulated items.
If given a field, returns all values in that field.
sourceraw docstring

averageclj/s

(average field)

Returns an accumulator that returns the average value of a given field.

Returns an accumulator that returns the average value of a given field.
sourceraw docstring

countclj/s

(count)

Returns an accumulator that simply counts the number of matching facts

Returns an accumulator that simply counts the number of matching facts
sourceraw docstring

distinctclj/s

(distinct)
(distinct field)

Returns an accumulator producing a distinct set of facts. If given a field, returns a distinct set of values for that field.

Returns an accumulator producing a distinct set of facts.
If given a field, returns a distinct set of values for that field.
sourceraw docstring

existsclj/s

(exists)

Returns an accumulator that accumulates to true if at least one fact exists and nil otherwise, the latter causing the accumulator condition to not match.

Returns an accumulator that accumulates to true if at least one fact
exists and nil otherwise, the latter causing the accumulator condition to not match.
sourceraw docstring

grouping-byclj/s

(grouping-by field)
(grouping-by field convert-return-fn)

Return a generic grouping accumulator. Behaves like clojure.core/group-by.

  • field - required - The field of a fact to group by.
  • convert-return-fn - optional - Converts the resulting grouped data. Defaults to clojure.core/identity.
Return a generic grouping accumulator. Behaves like clojure.core/group-by.

* `field` - required - The field of a fact to group by.
* `convert-return-fn` - optional - Converts the resulting grouped
data. Defaults to clojure.core/identity.
sourceraw docstring

maxclj/s

(max field & {:keys [returns-fact]})

Returns an accumulator that returns the maximum value of a given field.

The caller may provide the following options:

  • :returns-fact Returns the fact rather than the field value if set to true. Defaults to false.
Returns an accumulator that returns the maximum value of a given field.

The caller may provide the following options:

* :returns-fact Returns the fact rather than the field value if set to true. Defaults to false.
sourceraw docstring

minclj/s

(min field & {:keys [returns-fact]})

Returns an accumulator that returns the minimum value of a given field.

The caller may provide the following options:

  • :returns-fact Returns the fact rather than the field value if set to true. Defaults to false.
Returns an accumulator that returns the minimum value of a given field.

The caller may provide the following options:

* :returns-fact Returns the fact rather than the field value if set to true. Defaults to false.
sourceraw docstring

reduce-to-accumclj/s

(reduce-to-accum reduce-fn)
(reduce-to-accum reduce-fn initial-value)
(reduce-to-accum reduce-fn initial-value convert-return-fn)
(reduce-to-accum reduce-fn initial-value convert-return-fn combine-fn)

Creates an accumulator using a given reduce function with optional initial value and conversion to the final result.

For example, a a simple function that return a Temperature fact with the highest value:

(acc/reduce-to-accum (fn [previous value] (if previous (if (> (:temperature value) (:temperature previous)) value previous) value)))

Note that the above example produces the same result as (clara.rules.accumulators/max :temperature :returns-fact true), and users should prefer to use built-in accumulators when possible. This funciton exists to easily convert arbitrary reduce functions to an accumulator.

Callers may optionally pass in an initial value (which defaults to nil), a function to transform the value returned by the reduce (which defaults to identity), and a function to combine two reduced results (which uses the reduce-fn to add new items to the same reduced value by default).

Creates an accumulator using a given reduce function with optional initial value and
conversion to the final result.

For example, a a simple function that return a Temperature fact with the highest value:

  (acc/reduce-to-accum
     (fn [previous value]
        (if previous
           (if (> (:temperature value) (:temperature previous))
              value
              previous)
           value)))

Note that the above example produces the same result as
(clara.rules.accumulators/max :temperature :returns-fact true),
and users should prefer to use built-in accumulators when possible. This funciton exists to easily
convert arbitrary reduce functions to an accumulator.

Callers may optionally pass in an initial value (which defaults to nil),
a function to transform the value returned by the reduce (which defaults to identity),
and a function to combine two reduced results (which uses the reduce-fn to add new 
items to the same reduced value by default).
sourceraw docstring

sumclj/s

(sum field)

Returns an accumulator that returns the sum of values of a given field

Returns an accumulator that returns the sum of values of a given field
sourceraw docstring

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close