# Function

``public final class Function``

Function factory.

• ``` avg(_:) ```

Create an AVG(expr) function expression that returns the average of all the number values in the group of the values expressed by the given expression.

#### Declaration

Swift

``public static func avg(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The expression.

#### Return Value

The AVG(expr) function.

• ``` count(_:) ```

Create a COUNT(expr) function expression that returns the count of all values in the group of the values expressed by the given expression.

#### Declaration

Swift

``public static func count(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The expression.

#### Return Value

The COUNT(expr) function.

• ``` min(_:) ```

Create a MIN(expr) function expression that returns the minimum value in the group of the values expressed by the given expression.

#### Declaration

Swift

``public static func min(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The expression.

#### Return Value

The MIN(expr) function.

• ``` max(_:) ```

Create a MAX(expr) function expression that returns the maximum value in the group of the values expressed by the given expression.

#### Declaration

Swift

``public static func max(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The expression.

#### Return Value

The MAX(expr) function.

• ``` sum(_:) ```

Create a SUM(expr) function expression that return the sum of all number values in the group of the values expressed by the given expression.

#### Declaration

Swift

``public static func sum(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The expression.

#### Return Value

The SUM(expr) function.

• ``` abs(_:) ```

Creates an ABS(expr) function that returns the absolute value of the given numeric expression.

#### Declaration

Swift

``public static func abs(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression.

#### Return Value

The ABS(expr) function.

• ``` acos(_:) ```

Creates an ACOS(expr) function that returns the inverse cosine of the given numeric expression.

#### Declaration

Swift

``public static func acos(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression.

#### Return Value

The ACOS(expr) function.

• ``` asin(_:) ```

Creates an ASIN(expr) function that returns the inverse sin of the given numeric expression.

#### Declaration

Swift

``public static func asin(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression.

#### Return Value

The ASIN(expr) function.

• ``` atan(_:) ```

Creates an ATAN(expr) function that returns the inverse tangent of the numeric expression.

#### Declaration

Swift

``public static func atan(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression.

#### Return Value

The ATAN(expr) function.

• ``` atan2(x:y:) ```

Creates an ATAN2(X, Y) function that returns the arctangent of y/x.

#### Declaration

Swift

``public static func atan2(x: ExpressionProtocol, y: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` x ``` The expression to evaluate as the X coordinate. ``` y ``` The expression to evaluate as the Y coordinate.

#### Return Value

The ATAN2(X, Y) function.

• ``` ceil(_:) ```

Creates a CEIL(expr) function that returns the ceiling value of the given numeric expression

#### Declaration

Swift

``public static func ceil(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression.

#### Return Value

The CEIL(expr) function.

• ``` cos(_:) ```

Creates a COS(expr) function that returns the cosine of the given numeric expression.

#### Declaration

Swift

``public static func cos(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression.

#### Return Value

The COS(expr) function.

• ``` degrees(_:) ```

Creates a DEGREES(expr) function that returns the degrees value of the given radiants value expression.

#### Declaration

Swift

``public static func degrees(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression to evaluate as a radiants value.

#### Return Value

The DEGREES(expr) function.

• ``` e() ```

Creates a E() function that return the value of the mathemetical constant ‘e’.

#### Declaration

Swift

``public static func e() -> ExpressionProtocol``

#### Return Value

The E() constant function.

• ``` exp(_:) ```

Creates a EXP(expr) function that returns the value of ‘e’ power by the given numeric expression.

#### Declaration

Swift

``public static func exp(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression.

#### Return Value

The EXP(expr) function.

• ``` floor(_:) ```

Creates a FLOOR(expr) function that returns the floor value of the given numeric expression.

#### Declaration

Swift

``public static func floor(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression.

#### Return Value

The FLOOR(expr) function.

• ``` ln(_:) ```

Creates a LN(expr) function that returns the natural log of the given numeric expression.

#### Declaration

Swift

``public static func ln(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression.

#### Return Value

The LN(expr) function.

• ``` log(_:) ```

Creates a LOG(expr) function that returns the base 10 log of the given numeric expression.

#### Declaration

Swift

``public static func log(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression.

#### Return Value

The LOG(expr) function.

• ``` pi() ```

Creates a PI() function that returns the mathemetical constant Pi.

#### Declaration

Swift

``public static func pi() -> ExpressionProtocol``

#### Return Value

The PI() constant function.

• ``` power(base:exponent:) ```

Creates a POWER(base, exponent) function that returns the value of the given base expression power the given exponent expression.

#### Declaration

Swift

``public static func power(base: ExpressionProtocol, exponent: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` base ``` The base expression. ``` exponent ``` The exponent expression.

#### Return Value

The POWER(base, exponent) function.

• ``` radians(_:) ```

Creates a RADIANS(expr) function that returns the radians value of the given degrees value expression.

#### Declaration

Swift

``public static func radians(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression to evaluate as a degrees value.

#### Return Value

• ``` round(_:) ```

Creates a ROUND(expr) function that returns the rounded value of the given numeric expression.

#### Declaration

Swift

``public static func round(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression.

#### Return Value

The ROUND(expr) function.

• ``` round(_:digits:) ```

Creates a ROUND(expr, digits) function that returns the rounded value to the given number of digits of the given numeric expression.

#### Declaration

Swift

``public static func round(_ expression: ExpressionProtocol, digits: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression. ``` digits ``` The number of digits.

#### Return Value

The ROUND(expr, digits) function.

• ``` sign(_:) ```

Creates a SIGN(expr) function that returns the sign (1: positive, -1: negative, 0: zero) of the given numeric expression.

#### Declaration

Swift

``public static func sign(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression.

#### Return Value

The SIGN(expr) function.

• ``` sin(_:) ```

Creates a SIN(expr) function that returns the sin of the given numeric expression.

#### Declaration

Swift

``public static func sin(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression.

#### Return Value

The SIN(expr) function.

• ``` sqrt(_:) ```

Creates a SQRT(expr) function that returns the square root of the given numeric expression.

#### Declaration

Swift

``public static func sqrt(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression.

#### Return Value

The SQRT(expr) function.

• ``` tan(_:) ```

Creates a TAN(expr) function that returns the tangent of the given numeric expression.

#### Declaration

Swift

``public static func tan(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression.

#### Return Value

The TAN(expr) function.

• ``` trunc(_:) ```

Creates a TRUNC(expr) function that truncates all of the digits after the decimal place of the given numeric expression.

#### Declaration

Swift

``public static func trunc(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression.

#### Return Value

The trunc function.

• ``` trunc(_:digits:) ```

Creates a TRUNC(expr, digits) function that truncates the number of the digits after the decimal place of the given numeric expression.

#### Declaration

Swift

``public static func trunc(_ expression: ExpressionProtocol, digits: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric expression. ``` digits ``` The number of digits to truncate.

#### Return Value

The TRUNC(expr, digits) function.

• ``` contains(_:substring:) ```

Creates a CONTAINS(expr, substr) function that evaluates whether the given string expression conatins the given substring expression or not.

#### Declaration

Swift

``public static func contains(_ expression: ExpressionProtocol, substring: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The string expression. ``` substring ``` The substring expression.

#### Return Value

The CONTAINS(expr, substr) function.

• ``` length(_:) ```

Creates a LENGTH(expr) function that returns the length of the given string expression.

#### Declaration

Swift

``public static func length(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The string expression.

#### Return Value

The LENGTH(expr) function.

• ``` lower(_:) ```

Creates a LOWER(expr) function that returns the lowercase string of the given string expression.

#### Declaration

Swift

``public static func lower(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The string expression.

#### Return Value

The LOWER(expr) function.

• ``` ltrim(_:) ```

Creates a LTRIM(expr) function that removes the whitespace from the beginning of the given string expression.

#### Declaration

Swift

``public static func ltrim(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The string expression.

#### Return Value

The LTRIM(expr) function.

• ``` rtrim(_:) ```

Creates a RTRIM(expr) function that removes the whitespace from the end of the given string expression.

#### Declaration

Swift

``public static func rtrim(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The string expression.

#### Return Value

The RTRIM(expr) function.

• ``` trim(_:) ```

Creates a TRIM(expr) function that removes the whitespace from the beginning and ‘ the end of the given string expression.

#### Declaration

Swift

``public static func trim(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The string expression.

#### Return Value

The TRIM(expr) function.

• ``` upper(_:) ```

Creates a UPPER(expr) function that returns the uppercase string of the given string expression.

#### Declaration

Swift

``public static func upper(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The string expression.

#### Return Value

The UPPER(expr) function.

• ``` stringToMillis(_:) ```

Creates a STR_TO_MILLIS(expr) function that returns the number of milliseconds since the unix epoch of the given ISO 8601 date string expression.

Note

Valid date strings must start with a date in the form YYYY-MM-DD (time only strings are not supported).

Times can be of the form HH:MM, HH:MM:SS, or HH:MM:SS.FFF. Leading zero is not optional (i.e. 02 is ok, 2 is not). Hours are in 24-hour format. FFF represents milliseconds, and trailing zeros are optional (i.e. 5 == 500).

Time zones can be in one of three forms: (+/-)HH:MM (+/-)HHMM Z (which represents UTC)

No time zone present will default to the device local time zone.

#### Declaration

Swift

``public static func stringToMillis(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The validly formatted ISO 8601 date time string expression.

#### Return Value

The STR_TO_MILLIS(expr) function.

• ``` stringToUTC(_:) ```

Creates a STR_TO_UTC(expr) function that returns the ISO 8601 UTC datetime string of the given ISO 8601 date string expression.

Note

Valid date strings must start with a date in the form YYYY-MM-DD (time only strings are not supported).

Times can be of the form HH:MM, HH:MM:SS, or HH:MM:SS.FFF. Leading zero is not optional (i.e. 02 is ok, 2 is not). Hours are in 24-hour format. FFF represents milliseconds, and trailing zeros are optional (i.e. 5 == 500).

Time zones can be in one of three forms: (+/-)HH:MM (+/-)HHMM Z (which represents UTC)

No time zone present will default to the device local time zone.

#### Declaration

Swift

``public static func stringToUTC(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The validly formatted ISO 8601 date time string expression.

#### Return Value

The STR_TO_UTC(expr) function.

• ``` millisToString(_:) ```

Creates a MILLIS_TO_STR(expr) function that returns a ISO 8601 date time string in device local timezone of the given number of milliseconds since the unix epoch expression.

Note

If the input expression is not numeric, then the result will be null.

#### Declaration

Swift

``public static func millisToString(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric value representing milliseconds since the unix epoch.

#### Return Value

The MILLIS_TO_STR(expr) function.

• ``` millisToUTC(_:) ```

Creates a MILLIS_TO_UTC(expr) function that returns the UTC ISO 8601 date time string of the given number of milliseconds since the unix epoch expression.

Note

If the input expression is not numeric, then the result will be null.

#### Declaration

Swift

``public static func millisToUTC(_ expression: ExpressionProtocol) -> ExpressionProtocol``

#### Parameters

 ``` expression ``` The numeric value representing milliseconds since the unix epoch.

#### Return Value

The MILLIS_TO_UTC(expr) function.

• ``` prediction(model:input:) ```

ENTERPRISE EDITION ONLY : UNCOMMITTED

Creates prediction function with the given model name and input. When running a query with the prediction function, the corresponding predictive model registered to CouchbaseLite Database class will be called with the given input to predict the result.

The prediction result returned by the predictive model will be in a form dictionary object. To create an expression that refers to a property in the prediction result, the property(_ keypath: String) method of the created PredictionFunction object can be used.

#### Declaration

Swift

``````public static func prediction(model: String,
input: ExpressionProtocol) -> PredictionFunction``````

#### Parameters

 ``` model ``` The predictive model name registered to the CouchbaseLite Database. ``` input ``` The expression evaluated to a dictionary.

#### Return Value

A PredictionFunction object.

• ``` euclideanDistance(between:and:) ```

ENTERPRISE EDITION ONLY

Creates a function that returns the euclidean distance between the two input vectors. The result is a non-negative floating-point number. The expression1 and expression2 must be arrays of numbers, and must be the same length.

#### Declaration

Swift

``````public static func euclideanDistance(between expression1: ExpressionProtocol,
and expression2: ExpressionProtocol) -> ExpressionProtocol``````

#### Parameters

 ``` expression1 ``` The expression evaluated to an arrays of numbers. ``` expression2 ``` The expression evaluated to an arrays of numbers.

#### Return Value

The euclidean distance between two given input vectors.

• ``` squaredEuclideanDistance(between:and:) ```

ENTERPRISE EDITION ONLY

Creates a function that returns the squared euclidean distance between the two input vectors. The result is a non-negative floating-point number. The expression1 and expression2 must be arrays of numbers, and must be the same length.

#### Declaration

Swift

``````public static func squaredEuclideanDistance(between expression1: ExpressionProtocol,
and expression2: ExpressionProtocol) -> ExpressionProtocol``````

#### Parameters

 ``` expression1 ``` The expression evaluated to an arrays of numbers. ``` expression2 ``` The expression evaluated to an arrays of numbers.

#### Return Value

The squared euclidean distance between two given input vectors.

• ``` cosineDistance(between:and:) ```

ENTERPRISE EDITION ONLY

Creates a function that returns the cosine distance which one minus the cosine similarity between the two input vectors. The result is a floating-point number ranges from −1.0 to 1.0. The expression1 and expression2 must be arrays of numbers, and must be the same length.

#### Declaration

Swift

``````public static func cosineDistance(between expression1: ExpressionProtocol,
and expression2: ExpressionProtocol) -> ExpressionProtocol``````

#### Parameters

 ``` expression1 ``` The expression evaluated to an arrays of numbers. ``` expression2 ``` The expression evaluated to an arrays of numbers.

#### Return Value

The cosine distance between two given input vectors.