The functions associated with collections were introduced with 4D v16 R6 and have evolved with the possibility to use formulas for some of them, like map() or reduce(). 4D v20 brings other features that are just as practical, which we will present here.
First() and last()
The two functions, first() and last(), as their names indicate, allow access to the first and the last element of a collection. Unlike the use of indices and brackets (col[0], col[col.length-1]), there is no risk of error if the collection is empty. In this case, the value undefined will simply be returned by the functions.
$first:=MyCollection.first()
$last:=MyCollection.last()
AT()
The at() function is also an alternative to square brackets (col[x]) with a benefit; the value can be negative to get the items from the end.
Like the first() and last() functions, the undefined value will be returned (without error) in case of an empty collection or index overflow.
$first:=MyCollection.at(0)
$second:=MyCollection.at(1)
$third:=MyCollection.at(2) // might be undefined if collection contains only two items
$last:=MyCollection.at(-1)
Good news: The at() function is also available for entity selections!
The only difference is that the function returns Null instead of undefined when the entity selection is empty (or in case of index overflow).
$es:=ds.Info.all()
$title1:=$es.at(0).title
$title2:=$es.at(1).title
$title3:=$es.at(2).title
Flat()
The flat() function is more innovative and will be very useful in some cases. It allows the flattening of collections that can contain collections (that can also include collections, etc.).
By default, the number of levels of the original collection is reduced by one. So some collections may remain in the result. But the function accepts a parameter to compensate for this.
$flatCollection:=$nestedCollection.flat() // might still contain collections
$flatCollection:=$nestedCollection.flat(MAXLONG) // all included collections will be flatten
Result (Before / After)
includes()
The includes() function is handy because it allows knowing if a collection contains this or that element of almost any type… text, numeric, date… and even references of objects or collections.
$result:=colorsCol.includes("blue")
$result:=valuesCol.includes(10)
$result:=objectsCol.includes(myObjectRef)
reduceRight()
You already know the reduce() function, which allows you to iterate automatically on a collection to get a unique result of any type: A string, a numerical value…
This function allows the same style of calculations but scans the collection from right to left (i.e., starting from the end). Depending on the type of operation, the results can be quite different.
In summary, reduceRight() is the optimized equivalent of reverse().reduce()
flatmap()
The map() function allows (since version 4Dv16 R6) to create a collection based on the content of another collection. The flatMap() function allows us to perform, in a single operation, a map() followed by a flat() – a function that we mentioned before.
It is, therefore, the optimized equivalent of the map().flat()
Conclusion
These new functions, some of which are inspired by JavaScript, complete the large collection functions already present in 4D. They will be instrumental in your development.
Don’t hesitate to talk about it on the 4D forum!