Package convex.core.lang
Class RT
java.lang.Object
convex.core.lang.RT
Static utility class for Runtime functions. Mostly low-level support for Core
language capabilities, which will be wrapped as functions in the initial
execution environment.
"Low-level programming is good for the programmer's soul." — John Carmack
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic APrimitive
Gets the absolute value of a numeric value.allEqual
(T[] values) Returns true if all elements in an array are equal.static <R extends ACell>
ADataStructure<R>assoc
(ADataStructure<R> coll, ACell key, ACell value) Associates a key position with a given value in an associative data structurestatic boolean
Converts any CVM value to a boolean value.static AccountKey
Coerce to an AccountKey.static Address
castAddress
(ACell a) Casts the argument to a valid Address.static ABlob
Converts an object to a canonical blob representation.static CVMByte
Explicitly converts a numerical value to a CVM Byte.static CVMDouble
castDouble
(ACell a) Converts a numerical value to a CVM Double.Coerces an argument to a function interface.static Keyword
castKeyword
(ACell a) Coerces an named value to a Keyword.static CVMLong
Converts a numerical value to a CVM Long.Converts any collection to a setcastVector
(ACell o) Converts any countable data structure to a vector.static CVMDouble
Gets the ceiling a number after casting to a double.static Class<?>
commonNumericType
(ACell[] args) Get the target common numeric type for a given set of arguments.static long
compare
(long a, long b) Compares two long values numerically, according to Java primitive comparisons.static Long
Compares two objects representing numbers numerically.static ASequence<?>
Concatenates two sequences.Prepends an element to a sequential data structure to create a new list.Prepends two elements to a sequential data structure.Prepends three elements to a sequential data structure.static Long
Gets the count of elements in a countable data structure.static Long
Gets the count of elements in a collection or Java array.static <T extends ACell>
TConverts a Java value to a CVM type.static CVMLong
Decrements a Long valuestatic CVMDouble
static double
doubleFromUnsignedLong
(long a) Converts a long value, treated as unsigned, to a double.static AccountKey
Implicit cast to an AccountKey.static Address
Ensures the argument is a valid Address.static ADataStructure<?>
Ensures the argument is an associative data structure instance.static ABlob
ensureBlob
(ACell a) Implicitly casts the argument to a Blobstatic CVMChar
ensureChar
(ACell a) Implicitly casts argument to a CVM Characterstatic <E extends ACell>
ACountable<E>Casts to an ACountable instancestatic <E extends ACell>
ADataStructure<E>Casts to an ADataStructure instancestatic CVMDouble
Ensures the argument is a CVM Long value.Ensure the argument is a valid CVM function.static Hash
ensureHash
(ACell o) Coerces to Hash type.Casts the argument to a hashmap.static CVMLong
ensureLong
(ACell a) Ensures the argument is a CVM Long value.Converts the argument to a non-null Map.Converts an object to a map entry.static INumeric
Converts a CVM value to the standard numeric representation.Ensures argument is a sequence data structure.Ensures the value is a Set.static AString
Ensures the argument is a CVM Stringstatic Symbol
Ensures the argument is a Symbol.static CVMBool
static CVMDouble
Computes the result of a exp operation.static int
findNonNumeric
(ACell[] args) Finds the first non-numeric value in an array.static CVMDouble
Gets the floor a number after casting to a double.static CVMBool
static ACell
get
(ADataStructure<?> coll, ACell key) Gets an element from a data structure using the given key.static ACell
get
(ADataStructure<?> coll, ACell key, ACell notFound) Gets an element from a data structure using the given key.static AType
Get the runtime Type of any CVM valuestatic CVMBool
static CVMLong
Increments a Long valuestatic boolean
Tests if a value is one of the canonical boolean values 'true' or 'false'static boolean
Checks if a Cell is a valid CVM valuestatic boolean
static boolean
Tests if a Value is a valid numerical value type.static boolean
isValidAmount
(long amount) static <T> T
Converts a CVM value to equivalent JSON value as expressed in equivalent JVM types.static <T> T
Converts a CVM value to equivalent JVM valueReturns a Vector of keys of a Map, or null if the object is not a Map WARNING: Potentially O(n) in size of Mapstatic CVMBool
static CVMBool
static ACell
Gets the minimum of a set of numeric valuesstatic ACell
Gets the minimum of two numeric valuesstatic ACell
Gets the minimum of a set of numeric valuesstatic ACell
Gets the minimum of two numeric valuesstatic APrimitive
static APrimitive
minusDouble
(ACell[] args) static CVMLong
Compute mode.static AString
Gets the name from a CVM value.static <T extends ACell>
TGets the nth element from a sequential collection.static <T extends ACell>
TVariant of nth that also handles Java Arrays.static Class<?>
numericType
(ACell a) Gets the numeric class of an objectstatic APrimitive
static CVMDouble
plusDouble
(ACell[] args) static CVMDouble
Computes the result of a pow operation.static AString
Prints a value with a reasonable print limit.static AString
Prints a cell to a BlobBuilder, up to a specified limit of bytesstatic boolean
print
(BlobBuilder bb, ACell a, long limit) Prints a cell to a BlobBuilder, up to a specified limit of bytesConverts any collection of cells into a Sequence data structure.static ACell
Gets the signum of a numeric valuestatic long
splitmix64Calc
(long seed) Compute a SplitMix64 value for a given seed.static long
splitmix64Update
(long seed) Compute a SplitMix64 update.static CVMDouble
Gets the exact positive square root of a number after casting to a double.static AString
Converts a value to a CVM String representation.static AString
Converts arguments to an AString representation.static APrimitive
static APrimitive
timesDouble
(ACell[] args) static Address
Casts an arbitrary value to an Addressstatic CVMChar
toCharacter
(ACell a) Casts a value to a Character via a Long to get a Unicode 32-bit code pointstatic String
Converts a value to a Java String representationstatic void
Validates an object.static void
Validate a Cell.Returns the vector of values of a map, or null if the object is not a mapvec
(ACollection<T> coll) Converts any collection to a vector.Converts any data structure to a vector
-
Constructor Details
-
RT
public RT()
-
-
Method Details
-
allEqual
Returns true if all elements in an array are equal. Nulls are equal to null only.- Type Parameters:
T
- Type of values- Parameters:
values
- Array of values- Returns:
- True if all values are equal
-
eq
-
ge
-
gt
-
le
-
lt
-
min
Gets the minimum of a set of numeric values- Parameters:
values
- Arguments for which to compute minimum value- Returns:
- minimum value, or null if any argument is non-numeric
-
min
Gets the minimum of two numeric values- Parameters:
a
- First valueb
- Second value- Returns:
- minimum value, or null if any argument is non-numeric
-
max
Gets the minimum of a set of numeric values- Parameters:
values
- Arguments for which to compute minimum value- Returns:
- minimum value, or null if any argument is non-numeric
-
max
Gets the minimum of two numeric values- Parameters:
a
- First valueb
- Second value- Returns:
- minimum value, or null if any argument is non-numeric
-
commonNumericType
Get the target common numeric type for a given set of arguments. - Integers upcast to Long - Anything else upcasts to Double- Parameters:
args
- Argument array- Returns:
- The target numeric type, or null if there is a non-numeric argument
-
findNonNumeric
Finds the first non-numeric value in an array. Used for error reporting.- Parameters:
args
- Argument array- Returns:
- First non-numeric value, or null if not found.
-
numericType
Gets the numeric class of an object- Parameters:
a
- Numerical value- Returns:
- Long.class or Double.class if cast possible, or null if not numeric.
-
plus
-
plusDouble
-
minus
-
minusDouble
-
times
-
timesDouble
-
divide
-
pow
Computes the result of a pow operation. Returns null if a cast fails.- Parameters:
args
- Argument array, should be length 2- Returns:
- Result of exponentiation
-
exp
Computes the result of a exp operation. Returns null if a cast fails.- Parameters:
arg
- Numeric value- Returns:
- Numeric result, or null
-
floor
Gets the floor a number after casting to a double. Equivalent to java.lang.StrictMath.floor(...)- Parameters:
a
- Numerical Value- Returns:
- The floor of the number, or null if cast fails
-
ceil
Gets the ceiling a number after casting to a double. Equivalent to java.lang.StrictMath.ceil(...)- Parameters:
a
- Numerical Value- Returns:
- The ceiling of the number, or null if cast fails
-
sqrt
Gets the exact positive square root of a number after casting to a double. Returns NaN for negative numbers.- Parameters:
a
- Numerical Value- Returns:
- The square root of the number, or null if cast fails
-
splitmix64Update
public static long splitmix64Update(long seed) Compute a SplitMix64 update. Component for splittable PRNG. See: https://xorshift.di.unimi.it/splitmix64.c- Parameters:
seed
- Initial SplitMix64 seed- Returns:
- Updated SplitMix64 seed
-
splitmix64Calc
public static long splitmix64Calc(long seed) Compute a SplitMix64 value for a given seed. Component for splittabe PRNG See: https://xorshift.di.unimi.it/splitmix64.c- Parameters:
seed
- Initial SplitMix64 seed- Returns:
- Updated SplitMix64 seed
-
doubleFromUnsignedLong
public static double doubleFromUnsignedLong(long a) Converts a long value, treated as unsigned, to a double. Useful for randomness- Parameters:
a
- Long value, treated as unsigned- Returns:
- Double value of long
-
abs
Gets the absolute value of a numeric value. Supports double and long.- Parameters:
a
- Numeric CVM value- Returns:
- Absolute value, or null if not a numeric value
-
signum
Gets the signum of a numeric value- Parameters:
a
- Numeric value- Returns:
- value of -1, 0 or 1, NaN is argument is NaN, or null if the argument is not numeric
-
compare
Compares two objects representing numbers numerically.- Parameters:
a
- First numeric valueb
- Second numeric valuenanValue
- Value to return in case of a NaN result- Returns:
- -1 if a is smaller, 1 if a is larger, 0 if a equals b, null if either value non-numeric, NaN if either value is NaN
-
compare
public static long compare(long a, long b) Compares two long values numerically, according to Java primitive comparisons.- Parameters:
a
- First numberb
- Second number- Returns:
- -1 if a is less than b, 1 if greater, 0 is they are equal
-
ensureNumber
Converts a CVM value to the standard numeric representation. Result will be one of:- Long for Byte, Long
- Double for Double
- null for any non-numeric value
- Parameters:
a
- Value to convert to numeric representation- Returns:
- The number value, or null if cannot be converted
-
isNumber
Tests if a Value is a valid numerical value type. Note: Returns false for null, but true for NaN- Parameters:
val
- Value to test- Returns:
- True if a number, false otherwise
-
inc
Increments a Long value- Parameters:
x
- Value to increment- Returns:
- Long Value, or null if conversion fails
-
dec
Decrements a Long value- Parameters:
x
- Value to decrement- Returns:
- Long Value, or null if conversion fails
-
castDouble
Converts a numerical value to a CVM Double.- Parameters:
a
- Value to cast- Returns:
- Double value, or null if not convertible
-
ensureDouble
Ensures the argument is a CVM Long value.- Parameters:
a
- Value to cast- Returns:
- CVMDouble value, or null if not convertible
-
castLong
Converts a numerical value to a CVM Long. Doubles and floats will be converted if possible.- Parameters:
a
- Value to cast- Returns:
- Long value, or null if not convertible
-
ensureLong
Ensures the argument is a CVM Long value.- Parameters:
a
- Value to cast- Returns:
- CVMLong value, or null if not convertible
-
castByte
Explicitly converts a numerical value to a CVM Byte. Doubles and floats will be converted if possible.- Parameters:
a
- Value to cast- Returns:
- Long value, or null if not convertible
-
toCharacter
Casts a value to a Character via a Long to get a Unicode 32-bit code point- Parameters:
a
- Value to cast- Returns:
- CVMChar value, or null if cast fails
-
vec
Converts any data structure to a vector- Parameters:
o
- Object to attempt to convert to a Vector- Returns:
- AVector instance, or null if not convertible
-
castVector
Converts any countable data structure to a vector. Might be O(n)- Parameters:
o
- Value to convert- Returns:
- AVector instance, or null if conversion fails
-
castSet
Converts any collection to a set- Parameters:
o
- Value to cast- Returns:
- Set instance, or null if cast fails
-
vec
Converts any collection to a vector. Always succeeds, but may have O(n) cost Null values are converted to empty vector (considered as empty sequence)- Parameters:
coll
- Collection to convert to a Vector- Returns:
- Vector instance
-
sequence
Converts any collection of cells into a Sequence data structure. Potentially O(n) in size of collection. Nulls are converted to an empty vector. Returns null if conversion is not possible.- Type Parameters:
T
- Type of cell in collection- Parameters:
o
- An object that contains a collection of cells- Returns:
- An ASequence instance, or null if the argument cannot be converted to a sequence
-
ensureSequence
Ensures argument is a sequence data structure. Nulls are converted to an empty vector. Returns null if conversion is not possible.- Type Parameters:
T
- Type of sequence elements- Parameters:
o
- Value to cast to sequence- Returns:
- An ASequence instance, or null if the argument cannot be converted to a sequence
-
nth
Gets the nth element from a sequential collection. Throws an exception if access is out of bounds - caller responsibility to check bounds first- Type Parameters:
T
- Type of element in collection- Parameters:
o
- Countable Valuei
- Index of element to get- Returns:
- Element from collection at the specified position
-
nth
Variant of nth that also handles Java Arrays. Used for destructuring.- Type Parameters:
T
- Return type- Parameters:
o
- Object to check for indexed elementi
- Index to check- Returns:
- Element at specified index
-
count
Gets the count of elements in a collection or Java array. Null is considered an empty collection.- Parameters:
o
- An Object representing a collection of items to be counted- Returns:
- The count of elements in the collection, or null if not countable
-
count
Gets the count of elements in a countable data structure. Null is considered an empty collection.- Parameters:
a
- Any Cell potentially representing a collection of items to be counted- Returns:
- The count of elements in the collection, or null if not countable
-
str
Converts arguments to an AString representation. Handles:- CVM Strings (unchanged)
- Blobs (converted to hex)
- Numbers (converted to canonical numeric representation)
- Other Objects (printed in canonical format)
- Parameters:
args
- Values to convert to String- Returns:
- AString value, or null if allowable String length exceeded
-
print
Prints a cell to a BlobBuilder, up to a specified limit of bytes- Parameters:
bb
- BlobBuilder instancea
- Cell to print (may be nil)limit
- Limit of printing- Returns:
- True if within limit, false if exceeded
-
print
Prints a cell to a BlobBuilder, up to a specified limit of bytes- Parameters:
a
- Cell to print (may be nil)limit
- Limit of printing in bytes- Returns:
- Printed String, or null if limit exceed
-
print
Prints a value with a reasonable print limit. WARNING: May return null, not for use in CVM code- Returns:
- Printed String, or null if limit exceed
-
str
Converts a value to a CVM String representation. Required to work for all valid Cells.- Parameters:
a
- Value to convert to a CVM String- Returns:
- CVM String representation of object
-
toString
Converts a value to a Java String representation- Parameters:
a
- Any CVM value- Returns:
- Java String representation. May be "nil".
-
name
Gets the name from a CVM value. Supports Strings, Keywords and Symbols.- Parameters:
a
- Value to cast to a name- Returns:
- Name of the argument, or null if not Named
-
cons
Prepends an element to a sequential data structure to create a new list. May be O(n). The new element will always be in position 0- Type Parameters:
T
- Type of elements- Parameters:
x
- Element to prependxs
- Any sequential object, or null (will be treated as empty sequence)- Returns:
- A new list with the cons'ed element at the start
-
cons
Prepends two elements to a sequential data structure. The new elements will always be in position 0 and 1- Type Parameters:
T
- Type of elements- Parameters:
x
- Element to prepend at position 0y
- Element to prepend at position 1xs
- Any sequential object, or null (will be treated as empty sequence)- Returns:
- A new list with the cons'ed elements at the start
-
cons
Prepends three elements to a sequential data structure. The new elements will always be in position 0, 1 and 2- Type Parameters:
T
- Type of elements- Parameters:
x
- Element to prepend at position 0y
- Element to prepend at position 1z
- Element to prepend at position 2xs
- Any sequential object- Returns:
- A new list with the cons'ed elements at the start
-
castFunction
Coerces an argument to a function interface. Certain values e.g. Keywords can be used / applied in function position.- Type Parameters:
T
- Function return type- Parameters:
a
- Value to cast to a function- Returns:
- AFn instance, or null if the argument cannot be coerced to a function.
-
ensureFunction
Ensure the argument is a valid CVM function. Returns null otherwise.- Type Parameters:
T
- Function return type- Parameters:
a
- Value to cast to a function- Returns:
- IFn instance, or null if the argument cannot be coerced to a function.
-
castAddress
Casts the argument to a valid Address. Handles:- Strings, which are interpreted as 16-character hex strings
- Addresses, which are returned unchanged
- Blobs, which are converted to addresses if and only if they are of the correct length (8 bytes)
- Numeric Longs, which are converted to the equivalent Address
- Parameters:
a
- Value to cast to an Address- Returns:
- Address value or null if not castable to a valid address
-
toAddress
Casts an arbitrary value to an Address- Parameters:
a
- Value to cast. Strings or CVM values accepted- Returns:
- Address instance, or null if not convertible
-
ensureAddress
Ensures the argument is a valid Address.- Parameters:
a
- Value to cast- Returns:
- Address value or null if not a valid address
-
ensureAccountKey
Implicit cast to an AccountKey. Accepts blobs of correct length- Parameters:
a
- Value to cast- Returns:
- AccountKey instance, or null if coercion fails
-
castAccountKey
Coerce to an AccountKey. Accepts strings and blobs of correct length- Parameters:
a
- Value to cast- Returns:
- AccountKey instance, or null if coercion fails
-
castBlob
Converts an object to a canonical blob representation. Handles blobs, addresses, hashes and hex strings- Parameters:
a
- Value to convert to a Blob- Returns:
- Blob value, or null if not convertable to a blob
-
ensureMap
Converts the argument to a non-null Map. Nulls are implicitly converted to the empty map.- Type Parameters:
K
- Type of map keysV
- Type of map values- Parameters:
a
- Value to cast- Returns:
- Map instance, or null if argument cannot be converted to a map
-
get
Gets an element from a data structure using the given key.- Parameters:
coll
- Collection to querykey
- Key to look up in collection- Returns:
- Value from collection with the specified key, or null if not found.
-
get
Gets an element from a data structure using the given key. Returns the notFound parameter if the data structure does not have the specified key- Parameters:
coll
- Collection to querykey
- Key to look up in collectionnotFound
- Value to return if the lookup failed- Returns:
- Value from collection with the specified key, or notFound argument if not found.
-
bool
Converts any CVM value to a boolean value. An value is considered falsey if null or equal to CVMBool.FALSE, truthy otherwise- Parameters:
a
- Object to convert to boolean value- Returns:
- true if object is truthy, false otherwise
-
ensureMapEntry
Converts an object to a map entry. Handles MapEntries and length 2 Vectors.- Type Parameters:
K
- Type of map keyV
- Type of map value- Parameters:
x
- Value to cast- Returns:
- MapEntry instance, or null if conversion fails
-
ensureHash
Coerces to Hash type. Converts blobs of correct length.- Parameters:
o
- Value to cast- Returns:
- Hash instance, or null if conversion not possible
-
castKeyword
Coerces an named value to a Keyword.- Parameters:
a
- Value to cast- Returns:
- Valid Keyword if correctly constructed, or null if a failure occurs
-
ensureSymbol
Ensures the argument is a Symbol.- Parameters:
a
- Value to cast- Returns:
- Symbol if correctly constructed, or null if a failure occurs
-
ensureDataStructure
Casts to an ADataStructure instance- Type Parameters:
E
- Type of data structure element- Parameters:
a
- Value to cast- Returns:
- ADataStructure instance, or null if not a data structure
-
ensureCountable
Casts to an ACountable instance- Type Parameters:
E
- Type of countable element- Parameters:
a
- Value to cast- Returns:
- ADataStructure instance, or null if not a data structure
-
isBoolean
Tests if a value is one of the canonical boolean values 'true' or 'false'- Parameters:
value
- Value to test- Returns:
- True if the value is a canonical boolean value.
-
concat
Concatenates two sequences. Ignores nulls.- Parameters:
a
- First sequence. Will be used to determine the type of the result if not null.b
- Second sequence. Will be the result if the first parameter is null.- Returns:
- Concatenated Sequence
-
validate
Validates an object. Might be a Cell or Ref- Parameters:
o
- Object to validate- Throws:
InvalidDataException
- For any validation failure
-
validateCell
Validate a Cell.- Parameters:
o
- Object to validate- Throws:
InvalidDataException
- For any validation failure
-
assoc
public static <R extends ACell> ADataStructure<R> assoc(ADataStructure<R> coll, ACell key, ACell value) Associates a key position with a given value in an associative data structure- Parameters:
coll
- Any associative data structurekey
- Key to update or addvalue
- Value to associate with key- Returns:
- Updated data structure, or null if implicit cast of key or value to required type fails
-
keys
Returns a Vector of keys of a Map, or null if the object is not a Map WARNING: Potentially O(n) in size of Map- Parameters:
a
- Value to extract keys from (i.e. a Map)- Returns:
- Vector of keys in the Map
-
values
Returns the vector of values of a map, or null if the object is not a map- Parameters:
a
- Value to extract values from (i.e. a Map)- Returns:
- Vector of values from a map, or null if the object is not a map
-
ensureAssociative
Ensures the argument is an associative data structure instance. A null argument is considered an empty map.- Parameters:
o
- Value to cast- Returns:
- IAssociative instance, or null if conversion is not possible
-
ensureSet
Ensures the value is a Set. null is converted to the empty Set.- Type Parameters:
T
- Type of Set element- Parameters:
a
- Value to cast- Returns:
- A Set instance, or null if the argument is not a Set
-
ensureHashMap
Casts the argument to a hashmap. null is converted to the empty HashMap.- Type Parameters:
K
- Type of keysV
- Type of values- Parameters:
a
- Any object- Returns:
- AHashMap instance, or null if not a hash map
-
ensureBlob
Implicitly casts the argument to a Blob- Parameters:
a
- Value to cast to Blob- Returns:
- Blob instance, or null if cast fails
-
ensureString
Ensures the argument is a CVM String- Parameters:
a
- Value to cast to a String- Returns:
- AString instance, or null if argument is not a String
-
isValidAmount
public static boolean isValidAmount(long amount) -
cvm
Converts a Java value to a CVM type.- Parameters:
o
- Any Java Object- Returns:
- Valid CVM type
-
jvm
Converts a CVM value to equivalent JVM value- Parameters:
o
- Value to convert to JVM type- Returns:
- Java value, or unchanged input
-
json
Converts a CVM value to equivalent JSON value as expressed in equivalent JVM types. Note some special one-way conversions that are required because JSON is not sufficiently expressive for all CVM types: - Address becomes a Number (Long type) - Lists and Vectors both become an Array (Java List type) - Characters become a String - Blobs become a hex string representation '0x....'- Parameters:
o
- Value to convert to JVM type- Returns:
- Java value which represents JSON object
-
mod
Compute mode.- Parameters:
a
- First numeric argument (numerator)b
- First numeric argument (divisor)- Returns:
- Numeric value or null if cast fails
-
getType
Get the runtime Type of any CVM value- Parameters:
a
- Any CVM value- Returns:
- Type of CVM value
-
isNaN
-
isCVM
Checks if a Cell is a valid CVM value- Parameters:
a
- Cell to check- Returns:
- True if CVM VAlue, false otherwise
-
ensureChar
Implicitly casts argument to a CVM Character- Parameters:
a
- Value to cast- Returns:
- CVMChar instance, or null if not implicitly castable
-