# Reference

## Syntax, operators and functions of the Quirrel language

### Highlights

- Set-oriented data manipulation
- Powerful built-in operations and functions
- User-definable functions to abstract out common functionality

# Getting Started

The tutorial is the best way to learn more about Quirrel. It walks you through all the concepts in Quirrel. This page provides a concise reference of language features, operators and functions. For more detail, you may want to check out the language specification.

# Data In Quirrel

Quirrel has native support for JSON. You can create strings, numbers, booleans, arrays, and objects just like you do in JavaScript. Additionally, Quirrel is set-oriented, which means all data are represented as sets of JSON values (surrounded by "[]"). For example:

{name: "John", age: 29, gender: "male"} [{name: "John", age: 29, gender: "male"}] true [true] "hello world" ["hello world"]

Note that single values are represented as single-element sets.

# Function Reference Table Of Contents

# Operator Reference

new arg

Produces a copy of the given set with a fresh set of identities (for use in self-joins)

arg1 + arg2

Numeric addition

arg1 - arg2

Numeric subtraction

arg1 * arg2

Numeric multiplication

arg1 / arg2

Numeric division

arg1 ^ arg2

Numeric exponentiation

arg1 < arg2

Numeric less-than comparison

arg1 <= arg2

Numeric less-than-equal comparison

arg1 > arg2

Numeric greater-than comparison

arg1 >= arg2

Numeric greater-than-equal comparison

arg1 = arg2

Equality comparison

arg1 != arg2

Negated equality comparison

arg1 & arg2

Boolean conjunction

arg1 | arg2

Boolean disjunction

! arg

Boolean complementation

neg arg

Numeric negation

arg1 ~ arg2 arg3

Set relation (for use in cross-set joins)

arg1 union arg2

Set union

arg1 intersect arg2

Set intersection

arg1 difference arg2

Set difference. Returns the events contained in arg1 which are not contained in arg2.

# Standard Function Reference

count (s : Any): Int

Takes a single set and returns its size.

distinct (s : Any): Any

Takes a single set and returns a set of the distinct (unique) values.

exists (s : Boolean): boolean

Takes a set of booleans and returns a single boolean: returns true if the given set of booleans contains at least one true value, returns false otherwise.

flatten(arr: Array) : set

Takes a set of arrays and flattens them into a set where each value of the original array becomes an independent record in the resulting set.

forall (s : Boolean): boolean

Takes a set of booleans and returns a single boolean: returns true if the given set of booleans contains all true values, returns false otherwise.

geometricMean (n : Numeric): Numeric

Takes a single set and returns its geometric mean.

load (s : String): Any

Takes a single string path and returns the set of events in the data store which correspond to the path in question.

max (n : Numeric): Numeric

Takes a single set of numeric events and returns the maximum value among all the events.

maxTime(times:

*DateTime*):*DateTime*Takes a set of

*DateTime*values and returns the maximum.mean (n : Numeric): Numeric

Takes a single set of numeric events and returns the arithmetic mean of all the values.

min (n : Numeric): Numeric

Takes a single set of numeric events and returns the minimum of all the values.

minTime(times:

*DateTime*):*DateTime*Takes a set of

*DateTime*values and returns the minimum.stdDev (n : Numeric): Numeric

Takes a single set of numeric events and returns the standard deviation from the arithmetic mean of all the values.

sum (n : Numeric): Numeric

Takes a single set of numeric events and returns the sum of all the values.

sumSq (n : Numeric): Numeric

Takes a single set and returns the sum of the square of each value.

variance (n : Numeric): Numeric

Takes a single set and returns its population variance.

# Statistics Function Reference

std::stats::corr (n1 : Numeric, n2 : Numeric): Numeric

Takes two sets and returns the correlation coefficient of the matched pairs of numbers.

std::stats::cov (n1 : Numeric, n2 : Numeric): Numeric

Takes two sets and returns the population covariance of the matched pairs of numbers.

std::stats::kMedians(data: Any, k: Numeric): Object (n1 : Numeric, n2 : Numeric): Numeric

Takes two sets. The first is a set of data on which a k-medians clustering algorithm will be run. The second is a numeric value k, which represents the number of cluster centroids that will be computed. The result is an object of the form: {"model1": {"cluster1": Any, "cluster2": Any, ...}, "model2": {"cluster1": Any, ...}, ...}. There will be one model for each schema present in the data. The values in the cluster fields represent the centroid of the cluster, inserted into the original data schema.

std::stats::assignClusters( data: Any, models: Object): Object

Takes two sets. The first is a set of data from which the result will be assigned. The second is a set of models that must be of the form: {"model1": {"cluster1": Any, "cluster2": Any, ...}, "model2": {"cluster1": Any, ...}, ...}. A single object of the form {model1: {clusterId : "cluster1", clusterCenter: Any }, {model1: {clusterId : "cluster2", clusterCenter: Any} , etc. will be returned for each model whose schema is contained in the row's schema. For example, if the schema for model2 isn't included in the row's schema, while the schemas for model1 and model3 are, the result for a given row will be something like: {"model1": {clusterId" : "cluster7", clusterCenter: { x : 3012, y : 13 }, "model3": {clusterId" : "cluster12", clusterCenter: { x : 1267, z : 294 }.

std:stats::linearRegression(n1: Numeric, data: Any): Object

Takes two sets: the first is the dependent variable and the second contains all the independent variables. Each schema in the data generates a field ("Model1", "Model2", etc.) in the result object. Each of these fields contains an array of size two. The first element of the array contains the coefficients of the model, inserted into the original schema. The second element of the array is the constant term. For example, if the original data contains two schema: {"foo": Numeric, "bar": Numeric} and {"foo": Numeric}, the resulting object would have the form: {"Model1": [{"foo": Numeric, "bar", Numeric}, Numeric], "Model2": [{"foo": Numeric}, Numeric]}.

std::stats::logisticRegression(n1: Numeric, data: Any): Object

Takes two sets: the first is the dependent variable and the second contains all the independent variables. The dependent variable must have numeric value 0 or value 1. Each schema in the data generates a field ("Model1", "Model2", etc.) in the resulting object. Each of these fields contains an array of size two. The first element of the array contains the coefficients of the model, inserted into the original schema. The second element of the array contains the constant term. For example, if the original data contains two schema: {"foo": Numeric, "bar": Numeric} and {"foo": Numeric}, the resulting object would have the form: {"Model1": [{"foo": Numeric, "bar", Numeric}, Numeric], "Model2": [{"foo": Numeric}, Numeric]}.

std::stats::predictLinear(data: Any, models: Object): Object

Takes two sets. The first is a set of data from which the result will be predicted. The second is a set of models that must be of the form: {"Model1": [Any, Numeric], "Model2": [Any, Numeric], . . .}. Consider a row of data. A single numeric value will be returned for each model whose schema is contained in the row's schema. For example, if the schema for Model2 isn't included in the row's schema, while the schemas for Model1 and Model3 are, the result for a given row will be of the form: {"Model1": Numeric, "Model3": Numeric}.

std::stats::predictLogistic(data: Any, models: Object): Object

Identitical to predictLinear, except that the resulting numerics will be values between 0 and 1.

std::stats::rank(s : Any): Numeric

Takes a single set, and returns a set of numbers whose ordering corresponds to the lexical ordering of the original set, starting from 0. If the values of multiple events in the original set compare as equal (ignoring event identities) they will have the same rank value. There may be discontinuities in the sequence of numbers returned.

std::stats::denseRank(s: Any): Numeric

Takes a single set, and returns a set of numbers whose ordering corresponds to the lexical ordering of the original set, starting from 0. If the values of multiple events in the original set compare as equal (ignoring event identities) they will have the same rank value. There will be no discontinuities in the sequence of numbers returned.

std::stats::indexedRank(s: Any): Numeric

Takes a single set, and returns a set of numbers whose ordering corresponds to the lexical ordering of the original set, but where values that compare as equal (ignoring event identities) are each assigned distinct rank values. The order of rank values within a group of equal event values is arbitrary, but you are guaranteed that rank values respect the lexical ordering and that there will be no discontinuities in the sequence of numbers returned.

# Math Function Reference

std::math::abs (n: Numeric): Double

Takes a single set of numeric events and returns a set of each absolute value.

std::math::acos (n: Numeric): Double

Takes a single set of numeric events and returns a set of each arccosine.

std::math::asin (n: Numeric): Double

Takes a single set of numeric events and returns a set of each arcsine.

std::math::atan (n: Numeric): Double

Takes a single set of numeric events and returns a set of each arctangent.

std::math::atan2 (n1: Numeric, n2: Numeric): Double

Takes two sets of numeric events and returns a set of the angle theta from the conversion of each pair of rectangular coordinates to polar coordinates.

std::math::cbrt (n: Numeric): Double

Takes a single set of numeric events and returns a set of each cube root.

std::math::ceil (n: Numeric): Double

Takes a single set of numeric events and returns a set of each ceiling.

std::math::copySign (n1: Numeric, n2: Numeric): Double

Takes two sets of numeric events and returns the first value with the sign of the second value.

std::math::cos (n: Numeric): Double

Takes a single set of numeric events and returns a set of each cosine.

std::math::cosh (n: Numeric): Double

Takes a single set of numeric events and returns a set of each hyperbolic cosine.

std::math::exp (n: Numeric): Double

Takes a single set of numeric events and returns a set of Euler's number (e) raised to each value.

std::math::floor (n: Numeric): Double

Takes a single set of numeric events and returns a set of each floor.

std::math::hypot (n1: Numeric, n2: Numeric): Double

Takes two sets of numeric events and returns a set of the square root of the sum of the square of each pair.

std::math::IEEEremainder (n1: Numeric, n2: Numeric): Double

Takes two sets of numeric events and returns a set of the remainder operation computed on the two values, as prescribed by the IEEE 754 standard.

std::math::log (n: Numeric): Double

Takes a single set of numeric events and returns a set of each natural log.

std::math::log10 (n: Numeric): Double

Takes a single set of numeric events and returns a set of each log in base 10.

std::math::minOf (n1: Numeric, n2: Numeric): Double

Takes two sets of numeric events and returns a set of the minimum of each pair.

std::math::maxOf (n1: Numeric, n2: Numeric): Double

Takes two sets of numeric events and returns a set of the maximum of each pair.

std::math::parseNum (s: String): Numeric

Converts the given string to a numeric value if possible (otherwise the result is undefined).

std::math::pow (n1: Numeric, n2: Numeric): Double

Takes two sets of numeric events and returns a set of the first value raised to the power of the second value.

std::math::roundTo(n1: Numeric, n2: Numeric): Double

Takes two sets of numeric events and rounds the first to the number of decimals specified in the second. A negative number in the 2nd field indicates decimals to the right. So, roundTo(1234, neg 2) would return 1200.

std::math::round (n: Numeric): Double

Takes a single set of numeric events and returns a set of each value rounded.

std::math::sin (n: Numeric): Double

Takes a single set of numeric events and returns a set of each sine.

std::math::signum (n: Numeric): Double

Takes a single set of numeric events and returns a set of each signum. 1.0 if the argument is greater than zero. -1.0 if the argument is less than zero.

std::math::sinh (n: Numeric): Double

Takes a single set of numeric events and returns a set of each hyperbolic sine.

std::math::sqrt (n: Numeric): Double

Takes a single set of numeric events and returns a set of each square root.

std::math::tan (n: Numeric): Double

Takes a single set of numeric events and returns a set of each tangent.

std::math::tanh (n: Numeric): Double

Takes a single set of numeric events and returns a set of each hyperbolic tangent.

std::math::toDegrees (n: Numeric): Double

Takes a single set of numeric events (in Radians) and returns a set of each value in degrees.

std::math::toRadians (n: Numeric): Double

Takes a single set of numeric events (in degrees) and returns a set of each value in Radians.

std::math::ulp (n: Numeric): Double

Takes a single set of numeric events and returns a set of the size of each unit in the last place.

# String Function Reference

std::string::codePointAt (s: String, n: Numeric): Int

Takes a single set of string events and a set of numeric events. Returns the character (Unicode code point) at the specified index.

std::string::codePointBefore (s: String, n: Numeric): Int

Takes a single set of string events and a set of numeric events. Returns the character (Unicode code point) before the specified index.

std::string::concat (s1: String, s2: String): String

Takes two sets of string events and returns the concatenation of the two strings.

std::string::dropLeft:(s: String, n: Numeric): String

Takes a set of string events and returns a new substring of the first string, removing the first n letters from s. If there are less than n letters in s, then the empty string is returned.

std::string::dropRight(s: String, n: Numeric): String

Takes a set of string events and returns a new substring of the first string, removing the last n letters from s. If there are less than n letters in s, then the empty string is returned.

std::string::endsWith (s1: String, s2: String): Boolean

Takes two sets of string events and returns a set of true or false. True if and only if the first string ends with the suffix specified by the second string.

std::string::equals (s1: String, s2: String): Boolean

Takes two sets of string events and returns a set of true or false. True if and only if the two strings are equivalent.

std::string::equalsIgnoreCase (s1: String, s2: String): Boolean

Takes two sets of string events and returns a set of true or false. True if and only if the two strings are equivalent, ignoring case considerations.

std::string::indexOf (s1: String, s2: String): Int

Takes two sets of string events and returns the index in the first string of the first occurrence of the substring specified by the second string.

std::string::isEmpty (s: String): Boolean

Takes a single set of string events and returns a set of true or false. True if and only if the length of the string is zero.

std::string::lastIndexOf (s1: String, s2: String): Int

Takes two sets of string events and returns the index in the first string of the last occurrence of the substring specified by the second string.

std::string::length (s: String): Int

Takes a single set of string events and returns a set of each string length.

std::string::matches (s1: String, s2: String): Boolean

Takes two sets of string events and returns true or false. True if and only if the first string matches the regular expression specified by the second string.

std::string::numToString(ns: Numeric): String

Converts a set of numeric values to a set of strings.

std::string::regexMatch(s: String, regex: String): Array[String]

Takes two sets of string events and treats the latter as a set of regular expressions, returning a set of arrays of the capture groups. Empty captures are returned as the empty set.

std::string::split(sset: String, separator: String): Array

std::string::splitRegex(sset: String, regex: String): ArrayThese two functions split individual strings into arrays. In the former, the separator is interpreted as a literal string; in the latter, a regular expression is used to find the points to split at.

std::string::startsWith (s1: String, s2: String): Boolean

Takes two sets of string events and returns true or false. True if and only if the first string starts with the prefix specified by the second string.

std::string::substring (s: String, n: Numeric): String

Takes two sets of string events and returns a new substring of the first string, beginning at the specified index.

std::string::takeLeft(s: String, n: Numeric): String

Takes a set of string events and returns a new substring of the first string that is the first n letters from s. If n is larger than the number of letters, than s is returned.

std::string::takeRight(s: String, n: Numeric): String

Takes a set of string events and returns a new substring of the last n letters of the string. If there are less than n letters in s, then s is returned.

std::string::toLowerCase (s: String): String

Takes a single set of string events and returns a set of each string with each character in lower case.

std::string::toUpperCase (s: String): String

Takes a single set of string events and returns a set of each string with each character in upper case.

std::string::trim(ss: String): String

Removes leading and trailing whitespace from the input strings.

# DateTime Function Reference

std::time::changeTimeZone (time : ISO8601, tz: String): ISO8601

Takes a set of ISO8601 strings and a set of time zone strings (of the form "+04:00") and returns a set of ISO8601 strings.

std::time::date (time : ISO8601): String

Takes a single set of ISO8601 strings and returns the date in format "yyyy-MM-dd".

std::time::dateHour(time : ISO8601): String

Takes a single set of ISO8601 strings and returns a set of the date and hour in format "yyyy-MM-dd'T'HH".

std::time::dateHourMinute(time : ISO8601): String

Takes a single set of ISO8601 strings and returns a set of the date, hour, and minute in format "yyyy-MM-dd'T'HH:mm".

std::time::dateHourMinuteSecond(time : ISO8601): String

Takes a single set of ISO8601 strings and returns a set of the date, hour, minute, and second in format "yyyy-MM-dd'T'HH:mm:ss".

std::time::dateHourMinuteSecondMillis(time : ISO8601): String

Takes a single set of ISO8601 strings and returns a set of the date, hour, minute, second, and millisecond in format "yyyy-MM-dd'T'HH:mm:ss.SSS".

std::time::daysBetween (time1 : ISO8601, time2 : ISO8601): Int

Takes two sets of ISO8601 strings and returns a set of the days between two times.

std::time::dayOfMonth (time : ISO8601): Int

Takes a single set of ISO8601 strings and returns a set of the day of the month.

std::time::dayOfWeek (time : ISO8601): Int

Takes a single set of ISO8601 strings and returns a set of the day of the week as an integer. Monday is day 1.

std::time::dayOfYear (time : ISO8601): Int

Takes a single set of ISO8601 strings and returns a set of the day of the year.

std::time::daysPlus(time : ISO8601, i : Int): String

Takes a single set of ISO8601 strings and an integer, and returns a set of ISO8601 Strings plus the specified number of days.

std::time::getMillis (time : ISO8601): Long

Takes a set of ISO8601 strings and returns a set of millisecond longs

std::time::hourMinute(time : ISO8601): String

Takes a single set of ISO8601 strings and returns a set of the hour and minute in format "HH:mm".

std::time::hourMinuteSecond(time : ISO8601): String

Takes a single set of ISO8601 strings and returns a set of the hour, minute, and second in format "HH:mm:ss".

std::time::hourOfDay (time : ISO8601): Int

Takes a single set of ISO8601 strings and returns a set of the hour of the day.

std::time::hoursBetween (time1 : ISO8601, time2 : ISO8601): Long

Takes two sets of ISO8601 strings and returns a set of the hours between two times.

std::time::hoursPlus(time : ISO8601, i : Int): String

Takes a single set of ISO8601 strings and an integer, and returns a set of ISO8601 Strings plus the specified number of hours.

std::time::millisOfSecond (time : ISO8601): Int

Takes a single set of ISO8601 strings and returns a set of the mills of the second.

std::time::maxTimeOf(d1:

*DateTime*, d2:*DateTime*):*DateTime*Compares two

*DateTime*values and returns the later.std::time::millisPlus(time : ISO8601, i : Int): String

Takes a single set of ISO8601 strings and an integer, and returns a set of ISO8601 Strings plus the specified number of millis.

std::time::millisToISO (time : Long, tz : String): ISO8601

Takes a set of long millis-since-epoch and a set of time zone strings (of the form "+04:00") and returns a set of ISO8601 strings.

std::time::minTimeOf(d1:

*DateTime*, d2:*DateTime*):*DateTime*Compares two

*DateTime*values and returns the earlier.std::time::minuteOfHour (time : ISO8601): Int

Takes a single set of ISO8601 strings and returns a set of the minute of the hour.

std::time::minutesBetween (time1 : ISO8601, time2 : ISO8601): Long

Takes two sets of ISO8601 strings and returns a set of the minutes between two times.

std::time::minutesPlus(time : ISO8601, i : Int): String

Takes a single set of ISO8601 strings and an integer, and returns a set of ISO8601 Strings plus the specified number of minutes.

std::time::monthDay(time : ISO8601): String

Takes a single set of ISO8601 strings and returns a set of the month and day in format "MM-dd".

std::time::monthOfYear (time : ISO8601): Int

Takes a single set of ISO8601 strings and returns a set of the month of the year as an integer. January is month 1.

std::time::monthsBetween (time1 : ISO8601, time2 : ISO8601): Int

Takes two sets of ISO8601 strings and returns a set of the months between two times.

std::time::monthsPlus(time : ISO8601, i : Int): String

Takes a single set of ISO8601 strings and an integer, and returns a set of ISO8601 Strings plus the specified number of months.

std::time::quarter (time : ISO8601): Int

Takes a single set of ISO8601 strings and returns a set of the quarter of year as an integer. January/February/March is quarter 1.

std::time::range ({start :

*DateTime*, end :*DateTime*, step :*Period*}): ArrayTakes an object with start, end and step fields. The start and end fields require a

*DateTime*. The step requires a*Period*. A*Period*can be produced using the std::time::parse*Period*() function. The range function returns an array of*DateTime*, inclusive of start and up to (possibly including) end, based on the interval provided in the step field.std::time::parseDateTime(s: String, s: String): ISO8601

Takes a set of time strings and a set of time string formats. Returns a set of ISO8601 strings. If no time zone is specified, the default is UTC.

std::time::parseDateTimeFuzzy(s: String): ISO8601

Takes a set of time strings and makes a best effort to parse the string into a

*DateTime*. If possible, returns a set of ISO8601 strings.std::time::parse

*Period*(s : String):*Period*Takes a string and returns a date

*Period*. For information on how to construct a*Period*string, see wikipediastd::time::season (time : ISO8601): String

Takes a set of ISO8601 strings and returns a set of strings. According to the solstices and equinoxes, the seasons are "fall", "winter", "spring", "summer".

std::time::secondOfMinute (time : ISO8601): Int

Takes a single set of ISO8601 strings and returns a set of the second of the minute.

std::time::secondsBetween(time1 : ISO8601, time2 : ISO8601): Long

Takes two sets of ISO8601 strings and returns a set of the seconds between two times.

std::time::secondsPlus(time : ISO8601, i : Int): String

Takes a single set of ISO8601 strings and an integer, and returns a set of ISO8601 Strings plus the specified number of seconds.

std::time::timeWithZone(time : ISO8601): String

Takes a single set of ISO8601 strings and returns a set of the time and time zone in format "HH:mm:ss.SSSZZ".

std::time::timeWithoutZone(time : ISO8601): String

Takes a single set of ISO8601 strings and returns a set of the time in format "HH:mm:ss.SSS".

std::time::timeZone (time : ISO8601): String

Takes a single set of ISO8601 strings and returns a set of the time zone as a string of the form "+04:00".

std::time::weeksBetween (time1 : ISO8601, time2 : ISO8601): Int

Takes two sets of ISO8601 strings and returns a set of the weeks between two times.

std::time::weekOfYear (time : ISO8601): Int

Takes a single set of ISO8601 strings and returns a set of the week of the year.

std::time::weeksPlus(time : ISO8601, i : Int): String

Takes a single set of ISO8601 strings and an integer, and returns a set of ISO8601 Strings plus the specified number of weeks.

std::time::year (time : ISO8601): Int

Takes a single set of ISO8601 strings and returns a set of the year.

std::time::yearsBetween (time1 : ISO8601, time2 : ISO8601): Int

Takes two sets of ISO8601 strings and returns a set of the years between two times.

std::time::yearDayOfYear(time : ISO8601): String

Takes a single set of ISO8601 strings and returns a set of the year and day of year in format "yyyy-DDD".

std::time::yearMonth(time : ISO8601): String

Takes a single set of ISO8601 strings and returns a set of the year and month in format "yyyy-MM".

std::time::yearsPlus(time : ISO8601, i : Int): String

Takes a single set of ISO8601 strings and an integer, and returns a set of ISO8601 Strings plus the specified number of years.

# Type Function Reference

std::type::isArray(s : Any): boolean

Takes a single set and returns true for each event that is an Array and false for all other defined events.

std::type::isBoolean(s : Any) : boolean

Takes a single set and returns true for each event that is a Boolean and false for all other defined events.

std::type::isNull(s : Any): boolean

Takes a single set and returns true for each event that is a null and false for all other defined events.

std::type::isNumber(s : Any): boolean

Takes a single set and returns true for each event that is a Number and false for all other defined events.

std::type::isObject(s : Any): boolean

Takes a single set and returns true for each event that is an Object and false for all other defined events.

std::type::isString(s : Any): boolean

Takes a single set and returns true for each event that is an String d false for all other defined events.