Methods
Methods are special types of Functions that apply to a List or Sequence of data. Usually, methods are used to aggregate data – such as counting or summing List values.
Methods are called like this, where subject is the subject of the method, such as the List or Sequence that is being aggregated:
subject.methodName(parameter1, parameter2)
If the method is passed an Expression as a parameter, you can use either of the following syntaxes:
subject.methodName({expression}) subject.methodName{expression}
The second is often preferred, for clarity and brevity.
Methods can be chained together to form a pipeline of evaluations -- for example:
subject.methodName1{expression1}.methodName2{expression2}
Methods can be used anywhere in Carrot.
Most commonly, methods will operate on a List – for example, to calculate the total number of shares from the Grants field, only counting grants that have a strike price of less than 10 cents, one could write:
grants.filter{price < .1}.sum{shares}
Method | Description |
---|---|
.all() | Returns true if all of the items match an expression |
.any() | Returns true if at least one of the items matches an expression |
`.count() | Returns the number of items matching an expression |
`.filter() | Returns a list of items that match an expression |
`.find() | When called against a Table, queries for entities in that table matching the expression |
`.groupBy() | Generally only used in Dashboards. Groups the items by key, returning a SequenceGroupedBy, that can be aggregated. |
`.join() | Returns a string that joins the items together. |
`.limit() | Returns a list of items, with the number of the items limited to a certain number. |
`.map() | Returns a list of items that have been transformed using the mapping expression passed in. |
`.max() | Returns the maximum value of the items in the list, excluding nulls. |
`.mean() | Returns the arithmetic mean (average) of the items in the list, excluding nulls. |
`.min() | Returns the minimum value of the items in the list, excluding null. |
`.none() | Returns true if none of the items matches an expression. |
`.sort() | Returns a list of items, sorted by an expression. |
`.sortDesc() | Returns a list of items, sorted in descending order. |
.sum() | Returns the sum of the items (optionally transformed with an expression). |
Returns a list filtered down to its unique items |
Aggregators are special expressions used to aggregate values over time, only used in ChartHop Dashboards. However, aggregators are now deprecated.
We recommend that you replace their use with the corresponding ChartHop methods above. The Dashboards UI will be migrated to use Methods instead.
An aggregator in Carrot appears as a word followed by curly brackets, which contain an expression.
aggregator{expression}
An aggregator performs a calculation on the parameter expression and returns a Number value based on the aggregator's logic.
All aggregators are case-insensitive.
Aggregators are only available in Dashboards.
To replace the use of an Aggregator currently used in a Dashboard with a Method call instead, use the following syntax:
db.job.find(jobFilter).aggregator{expression}
For example, this aggregator call:
count{headcount if department:engineering}
Becomes:
db.job.find{department:engineering}.count{headcount}
Aggregator | Description |
---|---|
count{} | Counts the number of times a given expression evaluates to a truthy value. All non-truthy values, such as null, an empty string, 0, or false are discarded. |
max{} | Returns the largest value of a given expression. All non-numeric values are discarded. |
mean{} | Returns the average of all values of a given expression. All non-numeric values are discarded. |
min{} | Returns the smallest value of a given expression. All non-numeric values are discarded. |
sum{} | Returns the sum of all values of a given expression. All non-numeric values are discarded. |