Skip to main content

Standard Library

The following libraries are provided with the Ergo compiler.

Stdlib

The following functions are in the org.accordproject.ergo.stdlib namespace and available by default.

Functions on Integer

NameSignatureDescription
integerAbs(x:Integer) : IntegerAbsolute value
integerLog2(x:Integer) : IntegerBase 2 integer logarithm
integerSqrt(x:Integer) : IntegerInteger square root
integerToDouble(x:Integer) : DoubleCast to a Double
integerModulo(x:Integer, y:Integer) : IntegerInteger remainder
integerMin(x:Integer, y:Integer) : IntegerSmallest of x and y
integerMax(x:Integer, y:Integer) : IntegerLargest of x and y

Functions on Long

NameSignatureDescription
longAbs(x:Long) : LongAbsolute value
longLog2(x:Long) : LongBase 2 long logarithm
longSqrt(x:Long) : LongLong square root
longToDouble(x:Long) : DoubleCast to a Double
longModulo(x:Long, y:Long) : LongLong remainder
longMin(x:Long, y:Long) : LongSmallest of x and y
longMax(x:Long, y:Long) : LongLargest of x and y

Functions on Double

NameSignatureDescription
abs(x:Double) : DoubleAbsolute value
sqrt(x:Double) : DoubleSquare root
exp(x:Double) : DoubleExponential
log(x:Double) : DoubleNatural logarithm
log10(x:Double) : DoubleBase 10 logarithm
ceil(x:Double) : DoubleRound to closest integer above
floor(x:Double) : DoubleRound to closest integer below
truncate(x:Double) : IntegerCast to an Integer
doubleToInteger(x:Double) : IntegerSame as truncate
doubleToLong(x:Double) : LongCast to a Long
minPair(x:Double, y:Double) : DoubleSmallest of x and y
maxPair(x:Double, y:Double) : DoubleLargest of x and y

Functions on String

NameSignatureDescription
length(x:String) : IntegerPrints length of a string
encode(x:String) : StringEncode as URI component
decode(x:String) : StringDecode as URI component
doubleOpt(x:String) : Double?Cast to a Double
double(x:String) : DoubleCast to a Double or NaN
integerOpt(x:String) : Integer?Cast to an Integer
integer(x:String) : IntegerCast to a Integer or 0
longOpt(x:String) : Long?Cast to a Long
long(x:String) : LongCast to a Long or 0

Functions on Arrays

NameSignatureDescription
count(x:Any) : IntegerNumber of elements
flatten(x:Any[]) : Any[]Flattens a nested array
arrayAdd(x:Any[],y:Any[]) : Any[]Array concatenation
arraySubtract(x:Any[],y:Any[]) : Any[]Removes elements of y in x
inArray(x:Any,y:Any[]) : BooleanWhether x is in y
containsAll(x:Any[],y:Any[]) : BooleanWhether all elements of y are in x
distinct(x:Any[]) : Any[]Duplicates elimination
singleton(x:Any[]) : Any?Single value from singleton array

Note: For most of these functions, the type-checker infers more precise types than indicated here. For instance concat([1,2],[3,4]) will return [1,2,3,4] and have the type Integer[].

Log functions

NameSignatureDescription
logString(x:String) : UnitAdds string to the log

Aggregate functions

NameSignatureDescription
max(x:Double) : DoubleThe largest element in x
min(x:Double) : DoubleThe smallest element in x
sum(x:Double) : DoubleSum of the elements in x
average(x:Double) : DoubleArithmetic mean

Math functions

NameSignatureDescription
acos(x:Double) : DoubleThe inverse cosine of x
asin(x:Double) : DoubleThe inverse sine of x
atan(x:Double) : DoubleThe inverse tangent of x
atan2(x:Double, y:Double) : DoubleThe inverse tangent of x / y
cos(x:Double) : DoubleThe cosine of x
cosh(x:Double) : DoubleThe hyperbolic cosine of x
sin(x:Double) : DoubleThe sine of x
sinh(x:Double) : DoubleThe hyperbolic sine of x
tan(x:Double) : DoubleThe tangent of x
tanh(x:Double) : DoubleThe hyperbolic tangent of x

Other functions

NameSignatureDescription
failure(x:String) : ErgoErrorResponseErgo error from a string
toString(x:Any) : StringPrints any value to a string
toText(x:Any) : StringTemplate variant of toString (internal)

Time

The following functions are in the org.accordproject.time namespace and are available by importing that namespace. They rely on the time.cto types from the Accord Project models.

Functions on DateTime

NameSignatureDescription
now() : DateTimeReturns the time when execution started
dateTime(x:String) : DateTimeParse a date and time
getSecond(x:DateTime) : LongSecond component of a DateTime
getMinute(x:DateTime) : LongMinute component of a DateTime
getHour(x:DateTime) : LongHour component of a DateTime
getDay(x:DateTime) : LongDay of the month component of a DateTime
getWeek(x:DateTime) : LongWeek of the year component of a DateTime
getMonth(x:DateTime) : LongMonth component in a DateTime
getYear(x:DateTime) : LongYear component in a DateTime
isAfter(x:DateTime, y:DateTime) : BooleanWhether x if after y
isBefore(x:DateTime, y:DateTime) : BooleanWhether x is before y
isSame(x:DateTime, y:DateTime) : BooleanWhether x is the same DateTime as y
dateTimeMin(x:DateTime[]) : DateTimeThe earliest in an array of DateTime
dateTimeMax(x:DateTime[]) : DateTimeThe latest in an array of DateTime
format(x:DateTime,f:String) : StringPrints date x according to format f

Functions on Duration

NameSignatureDescription
durationAs(x:Duration, y:TemporalUnit) : DurationChange the unit for duration x to y
diffDurationAs(x:DateTime, y:DateTime, z:TemporalUnit) : DurationDuration between x and y in unit z
diffDuration(x:DateTime, y:DateTime) : DurationDuration between x and y in seconds
addDuration(x:DateTime, y:Duration) : DateTimeAdd duration y to x
subtractDuration(x:DateTime, y:Duration) : DateTimeSubtract duration y to x
divideDuration(x:Duration, y:Duration) : DoubleRatio between durations x and y

Functions on Period

NameSignatureDescription
diffPeriodAs(x:DateTime, y:DateTime, z:PeriodUnit) : PeriodTime period between x and y in unit z
addPeriod(x:DateTime, y:Period) : DateTimeAdd time period y to x
subtractPeriod(x:DateTime, y:Period) : DateTimeSubtract time period y to x
startOf(x:DateTime, y:PeriodUnit) : DateTimeStart of period y nearest to DateTime x
endOf(x:DateTime, y:PeriodUnit) : DateTimeEnd of period y nearest to DateTime x