Arcade: Enhance Back() to be the equivalent of First()

06-28-2022 01:48 AM
Status: Open
MVP Frequent Contributor

At a first glance, Back() seems to be the equivalent to First(). One gets the first element of an array, the other gets the last element. But there are two important differences:


First() has two signatures, it works on arrays and feature sets

Back() only has one signature, it doesn't work on feature sets

  • Back(inputArray) -> Any


The behavior when called on an empty array is different:



var arr = []

var f_arr = First(arr)  // f_arr is null
var b_arr = Back(arr)  // this will throw an ExecutionError




This has implications for guarding against empty arrays (or featuresets):



var arr = []
// some code that fills arr (or doesn't)

// when we want to goard against an empty array using First(), we can simply
// call the function and check the result for null:
var f_arr = First(arr)
if(f_arr == null) {
    // arr is empty, code for that case here
} else {
    // arr isn't empty

// when we want to do the same using Back(), we have to call Count(), which
// is more computationally intensive, especially for large array
if(Count(arr) == 0) {
    // arr is empty, code for that case here
} else {
    // arr isn't empty



For most arrays, this is probably negligible, but it could very well become a problem if Back() could work on feature sets. Also, it's just irritating that these two quite similar functions handle this case so differently.


So, my suggestion:

  • Enable Back() to work on feature sets
  • Change how Back() handles empty input to how First() does it
  • optionally, rename Back() to Last(), because that's what it does: it gets the last element, in the same way that First() gets the first element and isn't named Front()...
Tags (1)