Collections

< Expressions Variables >

Sets and Lists

terp supports list and sets as literals. The default list type is java.util.ArrayList, the default set type is java.util.HashSet. Lists and sets can be converted into each other via the built-in conversion operators ^set and ^list.

Collection literals consist of a list of elements enclosed in curly braces { }. The default type that is created is an ArrayList. To create a set, simply use the ^set conversion operator.

All collection types support the size and length properties (which are usable interchangeably).

Collections can be enumerated and processed by formatters, selectors, and transformers. Please see the relevant documentation sections for more details.

When collections are converted to a string (during template expansion or result printing), the collection elements are converted to strings and then concatenated without any separators between them. You can employ transformers and formatters to change that behavior.

Collections support the plus and minus operators as well as their respective assignment variants. Whenever possible, the result collection will be of the same type as the fist operand. This is especially important when comparing the behavior of adding lists to sets versus the behavior of adding sets to lists.

The collection conversion operators ^set and ^list have special semantics when it comes to null or no-arguments construction. Rather than returning null, they return an empty instance of their target type.

The following examples illustrate collection usage in terp.

C:\terp\bin>terp -e "{2,0,1,1,2,2}"
201122
C:\terp\bin>terp -e "{2,0,1,1,2,2}.size"
6
C:\terp\bin>terp -e "^set({2,0,1,1,2,2})"
012
C:\terp\bin>terp -e "{0,1} + 2"
012
C:\terp\bin>terp -e "({0,1} + {1,2}).toString()"
[0, 1, 1, 2]
C:\terp\bin>terp -e "(^set({0,1}) + {1,2}).toString()"
[0, 1, 2]
C:\terp\bin>terp -e "^list().toString()"
[]
C:\terp\bin>terp -e "^list(null).toString()"
[]

 


Copyright 2006-2016 by Codemesh, Inc., ALL RIGHTS RESERVED

:
terp expressions
codemesh.com home expressions templates ant about us contact us download   

Commandline