Working with data
Taxi StdLib
Taxi ships with a collections of functions for basic operations on data.
These operations are performed locally on data received from services - it does not instruct remote services to perform these changes.
Taxi does not provide implementations of these functions, that’s left to runtimes, such as Orbital.
Strings
A collection of functions for manipulating strings
applyFormat
taxi.stdlib.applyFormat
declare extension function applyFormat(input: Any, format: String):String
Applies the provided format to the input string, and returns the result.
Formats follow the format defined by the Java Formatter conventions
concat
taxi.stdlib.concat
declare function concat(Any...):String
containsString
taxi.stdlib.containsString
declare extension function containsString(source:String, valueToSearchFor:String):Boolean
Returns true if valueToSearchFor
within source
indexOf
taxi.stdlib.indexOf
declare extension function indexOf(source:String, valueToSearchFor:String):Int
Returns the index of valueToSearchFor
within source
left
taxi.stdlib.left
declare extension function left(source:String,count:Int):String
Returns the left most characters from the source string
length
taxi.stdlib.length
declare extension function length(String):Int
lowerCase
taxi.stdlib.lowerCase
declare extension function lowerCase(String):String
mid
taxi.stdlib.mid
declare extension function mid(source: String,startIndex: Int,endIndex: Int):String
Returns the middle of a string, starting at the startIndex
, and ending right before the endIndex
.
startIndex
- the start index (inclusive)endIndex
- the end index (exclusive)
padEnd
taxi.stdlib.padEnd
declare extension function padEnd(input: String, length: Int, padWith: String):String
Pads the string to the specified length at the end with the specified character or space. padWith should be a single character - if multiple characters are passed, only the first is used
padStart
taxi.stdlib.padStart
declare extension function padStart(input: String, length: Int, padWith: String):String
Pads the string to the specified length at the start with the specified character or space. padWith should be a single character - if multiple characters are passed, only the first is used
replace
taxi.stdlib.replace
declare extension function replace(source: String, searchValue:String, replacement: String):String
Replaces the contents of the provided String, returning a new String Accepts three args:
source: String
: The string to searchsearchValue: String
: The string to search forreplacement: String
: The string to use as a replacement
right
taxi.stdlib.right
declare extension function right(source:String,count:Int):String
trim
taxi.stdlib.trim
declare extension function trim(String):String
upperCase
taxi.stdlib.upperCase
declare extension function upperCase(String):String
Collections
A collection of functions for operating on collections
all
taxi.stdlib.all
declare extension function <T> all(collection: T[], predicate: (T) -> Boolean): Boolean
Returns true if all of the items in the collection satisfy the predicate
allOf
taxi.stdlib.allOf
declare function allOf(values:Boolean...): Boolean
Returns true
only if all provided boolean values are true
. Equivalent to logical AND.
This function ensures that every condition in a set must be satisfied.
// Check if a user can purchase a premium item
type CanPurchasePremium inherits Boolean by allOf(IsActive, HasSufficientFunds, IsVerified)
Examples
This example demonstrates how to use allOf()
to verify if a user meets all required criteria for premium access.
Try it out
This example demonstrates how to use allOf()
to verify if a user meets all required criteria for premium access.
any
taxi.stdlib.any
declare extension function <T> any(collection: T[], predicate: (T) -> Boolean): Boolean
Returns true if any of the items in the collection satisfy the predicate
anyOf
taxi.stdlib.anyOf
declare function anyOf(values:Boolean...): Boolean
Returns true
if at least one of the provided boolean values is true
. Equivalent to logical OR.
This function evaluates multiple conditions and returns true if any of them are true.
// Check if a user has any account verification
type IsVerified inherits Boolean by anyOf(HasEmailVerification, HasPhoneVerification, HasIdVerification)
Examples
This example demonstrates how to use anyOf()
to verify if a user meets any of several verification criteria.
Try it out
contains
taxi.stdlib.contains
declare extension function <T> contains(collection: T[], searchTarget:T): Boolean
Returns true
if the collection contains the specified search target element.
This function checks whether a given element exists within an array or collection.
// Check if a user's roles include admin privileges
find { user: User } as {
hasAdminAccess: user.roles.contains("ADMIN")
}
Examples
This example demonstrates how to use contains()
to check if a user has a specific role.
Try it out
exactlyOne
taxi.stdlib.exactlyOne
declare extension function <T> exactlyOne(collection:T[]):T
Returns the only item from the provided collection, or throws an error if there isn’t exactly one item in the collection.
This function is useful when you expect a collection to contain precisely one element and want to retrieve it.
// Get the single active session for a user
find { user: User } as {
currentSession: user.sessions.filter((session) -> session.isActive).exactlyOne()
}
Examples
This example demonstrates how to use exactlyOne()
to retrieve the single active session for a user.
Try it out
filter
taxi.stdlib.filter
declare extension function <T> filter(collection:T[], callback: (T) -> Boolean):T[]
Returns a new collection containing only the members of the original collection that match the provided predicate.
This function is used to select a subset of elements from a collection based on a condition.
find { store: Store } as {
allProducts: store.products // all products
inStockProducts: store.products.filter((product) -> product.stockLevel > 0)
affordableProducts: store.products.filter((product) -> product.price < 50.00)
}
Examples
This example demonstrates how to use filter()
to find friends who meet a certain age criteria.
Try it out
filterEach
taxi.stdlib.filterEach
declare extension function <T> filterEach(item: T, callback: (T) -> Boolean):T?
Evaluates the predicate against the provided item, returning the item if the predicate returns true, or null otherwise.
This function is intended for use in filtering streams, where null values are automatically excluded.
// Filter individual items in a pipeline
find { items: Item[] } as {
processedItems: items.map((item) -> filterEach(item, (i) -> i.status == "READY"))
}
Examples
This example demonstrates how to use filterEach()
to process items in a stream-like manner, keeping only those that meet certain criteria.
Try it out
first
taxi.stdlib.first
declare extension function <T> first(collection: T[]):T
Returns the first item within the collection. Throws an error if the collection is empty.
This function retrieves the first element in an ordered collection.
// Get the most recent transaction from a sorted list
find { account: Account } as {
latestTransaction: account.transactions.first()
}
Examples
This example demonstrates how to use first()
to retrieve the most recent transaction for an account.
Try it out
getAtIndex
taxi.stdlib.getAtIndex
declare extension function <T> getAtIndex(collection: T[], index: Int):T
Returns the item at the provided index in the collection. Throws an error if the index is out of bounds.
This function retrieves an element at a specific position in a collection, with zero-based indexing.
// Get the second item in a list
find { playlist: Playlist } as {
secondSong: playlist.songs.getAtIndex(1) // Note: index starts at 0
}
Examples
This example demonstrates how to use getAtIndex()
to retrieve a specific song from a playlist by its position.
Try it out
intersection
taxi.stdlib.intersection
declare extension function <T> intersection(collectionA: T[], collectionB: T[]):T[]
Returns a new collection containing only the elements that are present in both of the provided collections.
This function identifies common elements between two collections.
// Find common tags between two products
find { productA: Product, productB: Product } as {
commonTags: intersection(productA.tags, productB.tags)
}
Examples
This example demonstrates how to use intersection()
to find common elements between two collections.
Try it out
joinToString
taxi.stdlib.joinToString
declare extension function <T> joinToString(values:T[], separator: String = ",", prefix: String? = null, postfix: String? = null): String
Creates a string from all the elements separated using separator and using the given prefix and postfix if supplied. Null values are omitted
last
taxi.stdlib.last
declare extension function <T> last(collection: T[]):T
Returns the last item within the collection. Throws an error if the collection is empty.
This function retrieves the final element in an ordered collection.
// Get the oldest transaction from a date-sorted list
find { account: Account } as {
oldestTransaction: account.transactions.last()
}
Examples
This example demonstrates how to use last()
to retrieve the oldest transaction for an account.
Try it out
listOf
taxi.stdlib.listOf
declare function <T> listOf(values:T...):T[]
Creates and returns an array containing all the provided values.
This function is useful for creating collections inline without having to define them separately.
// Create a list of allowed status values
find { order: Order } as {
isValidStatus: listOf('PENDING', 'PROCESSING', 'SHIPPED', 'DELIVERED').contains(order.status)
}
Examples
This example demonstrates how to use listOf()
to create an inline collection and validate against it.
Try it out
none
taxi.stdlib.none
declare extension function <T> none(collection: T[], predicate: (T) -> Boolean): Boolean
Returns true if none of the items in the collection satisfy the predicate
noneOf
taxi.stdlib.noneOf
declare function noneOf(values:Boolean...): Boolean
Returns true
only if all provided boolean values are false
. Equivalent to logical NOR.
This function is useful for checking if none of multiple conditions apply.
// Check if a user has no permissions
type NoPermissions inherits Boolean by noneOf(IsAdmin, IsEditor, IsViewer)
Examples
This example demonstrates how to use noneOf()
to verify if a user lacks all specified roles.
Try it out
single
taxi.stdlib.single
declare extension function <T> single(collection:T[], callback: (T) -> Boolean):T
Returns the first item from a collection that matches the provided predicate. Throws an error if no matching item is found.
This function is useful when you need to find a specific element in a collection based on a condition.
// Find a product by its SKU code
find { inventory: Inventory } as {
appleMacbook: inventory.products.single((product) -> product.sku == "MB-PRO-2023")
}
Examples
This example demonstrates how to use single()
to find a specific product in an inventory.
Try it out
singleBy
taxi.stdlib.singleBy
declare extension function <T,A> singleBy(collection:T[], groupingFunction: (T) -> A, searchValue: A):T
Returns the item from a collection where the value returned by the grouping function matches the search value.
Similar to single()
, but results are first grouped by the selector function, and these results are cached
to improve future performance when querying the same collection multiple times.
// Find a product by its SKU code using a cached search
find { inventory: Inventory } as {
appleMacbook: inventory.products.singleBy((product) -> product.sku, "MB-PRO-2023")
}
Examples
This example demonstrates how to use singleBy()
to efficiently find items in a collection by a specific property.
Try it out
Dates
Mess about with time. Flux capacitor not included
addDays
taxi.stdlib.dates.addDays
declare function <T> addDays(T, Int):T
addMinutes
taxi.stdlib.dates.addMinutes
declare function <T> addMinutes(T, Int):T
addSeconds
taxi.stdlib.dates.addSeconds
declare function <T> addSeconds(T, Int):T
currentDate
taxi.stdlib.dates.currentDate
declare function currentDate():Date
currentDateTime
taxi.stdlib.dates.currentDateTime
declare function currentDateTime():DateTime
currentTime
taxi.stdlib.dates.currentTime
declare function currentTime():Time
now
taxi.stdlib.dates.now
declare function now():Instant
parseDate
taxi.stdlib.dates.parseDate
declare function <T> parseDate(String):T
Math
Numbers ‘n’ such. Maths for the brits.
max
taxi.stdlib.max
declare extension function <T,A> max(collection: T[], callback: (T) -> A):A
min
taxi.stdlib.min
declare extension function <T,A> min(collection: T[], callback: (T) -> A):A
round
taxi.stdlib.round
declare extension function round(value: Decimal, precision: Int = 0): Decimal
Rounds a decimal value to the specified precision.
sum
taxi.stdlib.sum
declare extension function <T,A> sum(collection: T[], callback: (T) -> A):A
Objects
Utilities for dealing with equality, etc
equals
taxi.stdlib.equals
declare extension function <A,B> equals(a:A, b:B): Boolean
Enums
Utilities for enums
enumForName
taxi.stdlib.enumForName
declare extension function <T> enumForName(enumType: lang.taxi.Type<T>, enumName: String): T
Returns the enum value for the provided name
hasEnumNamed
taxi.stdlib.hasEnumNamed
declare extension function <T> hasEnumNamed(enumType: lang.taxi.Type<T>, enumName: String): Boolean
Indicates if the enum contains an entry for the provided key.
Ignores default entries, so an enum with a default will return false to enumContains() if the key isn’t explicitly defined.
Aggregations
Functions for aggregating data within transformations.
sumOver
vyne.aggregations.sumOver
declare query function sumOver(Any...):Decimal
Functional
Functions that are functionally functions. Funky
fold
taxi.stdlib.fold
declare extension function <T,A> fold(collection: T[], initial: A, callback: (T,A) -> A):A
Iterates over a collection, combining elements to produce a single accumulated result.
Example:
// Given:
model Entry {
weight:Weight inherits Int
score:Score inherits Int
}
type WeightedAverage by (Entry[]) -> Entry[].fold(0, (Entry, Int) -> Int + (Weight*Score))
map
taxi.stdlib.map
declare extension function <T,A> map(collection: T[], callback: (T) -> A):A[]
Performs a mapping transformation function on every member of the provided array.
If the callback is in the form of a type expression, then each input value is converted into an instance of that type.
For example:
// Will convert Person[] to Person2[]
Person[].map((Person) -> Person2)
If the callback is in the form of any other type of expression, then that expression is evaluated against each member of the input array.
For example:
// Will return an array of Title, where the text has been converted to uppercase
Film[].map ( (Title) -> Title.upperCase() )
reduce
taxi.stdlib.reduce
declare extension function <T,A> reduce(collection: T[], callback: (T,A) -> A):A
Transformations
Functions for converting between types
convert
taxi.stdlib.convert
declare extension function <T> convert(source: Any, targetType: lang.taxi.Type<T>): T
Converts the provided source into the target type reference. Conversions are performed locally, using only the data provided in source - ie., no services or graph searches are performed.
This method is less powerful than using a standard projection (eg., A as B), because:
Only the exact facts passed in the source are considered
No graph searches or remote invocations are performed
As a result, it’s also more performant.
toRawType
taxi.stdlib.toRawType
declare extension function toRawType(source: Any):Any
Removes the semantic typing from the provided scalar value, returning a scalar value typed with the raw primitive type.
This can be useful if trying to compare values ignoring their type.
- If called with an Array, will remove semantic types of all members. The array should only contain scalar values.
- If called with a scalar value, will remove the semantic type of the value
Not supported for calling on an object (or array of objects), as Taxi’s type system is structural, and needs to know the structure of the type it’s operating on.
Errors
Functions for creating and handling errors
throw
taxi.stdlib.throw
declare function throw(error:Any):Nothing