Ways to Search Within Lists

Efficiently managing data is crucial in programming, and when it comes to text lists, employing list or array variables provides a convenient solution. However, dynamic data access can pose challenges.

In the realm of text lists, various methods exist for accessing their elements. Yet, the overuse of forEach() and if() statements within algorithms can detrimentally impact application performance. While the forEach function proves valuable, exploring alternative approaches to traverse arrays can enhance efficiency.

Alternatives to forEach()

>> wherecontains()

a!localVariables(
  local!fruits: {
    "Apple",
    "Banana",
    "Orange",
    "Strawberry",
    "Grape",
    "Watermelon",
    "Pineapple",
    "Pomegranate"
  },
  {
    /*Not THis is an Anonther Way Of Getting Values From an Array/list/Map*/
    wherecontains(local!fruits[{ 4, 7 }], local!fruits)/*We Will Get Result 
Note Istead of local!fruits[{4,7}] you cn also Specifiy Element Names and If Present You will Get those indexes */
  }
)

>> contains()
Checks if the provided value exists in the array, returning false or true.
a!localVariables(
  local!fruits: {
    "Apple",
    "Banana",
    "Orange",
    "Strawberry",
    "Grape",
    "Watermelon",
    "Pineapple",
    "Pomegranate"
  },
  {
    /*Not THis is an Anonther Way Of Getting Values From an Array/list/Map*/
    /*wherecontains(local!fruits[{ 4, 7 }], local!fruits)*/
    contains(local!fruits, "Apple")
    /*check in an array iif value is present or not     */
    
    /*We Will Get Result 
Note Istead of local!fruits[{4,7}] you cn also Specifiy Element Names and If Present You will Get those indexes */
  }
)

>> index or property

a!localVariables(
local!fruits: {
"Apple",
"Banana",
"Orange",
"Strawberry",
"Grape",
"Watermelon",
"Pineapple",
"Pomegranate"
},
{
/Not THis is an Anonther Way Of Getting Values From an Array/list/Map/
/wherecontains(local!fruits[{ 4, 7 }], local!fruits)/
/contains(local!fruits, "Apple")/
/*check in an array iif value is present or not */
index(local!fruits,{3, 7,9},"Value which we normally say ArrayIndexOutOfBoundsException"),
/* A Best Example will be finding value of A[7] when size of an Array is 5 hence need
to see if the length is less than to work Properly / property(local!fruits,{2, 6 ,10},"Aka Value which we can give if Not Found like -1") / Just a quick prompt Find Difference between to
tostring()
touniformstring()
/ /We Will Get Result
Note instead of local!fruits[{4,7}] you can also Specify Element Names and If Present You will Get those indexes */
}
)

>> filter() - find()

>>  find() Function:

Purpose: Searches for a partial text within another text.
Output: Returns the position of the first character found.
Special Case: If the partial text is not found, it returns zero.

>>filter() Function:

Purpose: Evaluates a function for each element in a list.
Output: Returns a list of elements for which the evaluation result was true.
How it works: It applies a specified function to each item in a list and includes only those items where the function evaluates to true.
a!localVariables(
  local!fruits: {
    "Apple",
    "Banana",
    "Orange",
    "Strawberry",
    "Grape",
    "Watermelon",
    "Pineapple",
    "Pomegranate"
  },
  {
    /*Not THis is an Anonther Way Of Getting Values From an Array/list/Map*/
    /*wherecontains(local!fruits[{ 4, 7 }], local!fruits)*/
    /*contains(local!fruits, "Apple")*/
    /*check in an array iif value is present or not     */
    /*index(local!fruits,{3, 7,9},"Value which we normaly say ArrayIndexOutOfBoundsException"),*/
    /* A Best Example will be finding value of A[7] when size of an Array is 5 hence need 
    to see if length is less than to work Properly  */
      /*property(local!fruits,{2, 6 ,10},"Aka Value which we can give if Not Found like -1")*/
      /* Just a quick prompt Find Diffrence between to     
      tostring()
      touniformstring()
      */
    
    filter(find("na", _), local!fruits)
    /*We Will Get Result 
Note Istead of local!fruits[{4,7}] you cn also Specifiy Element Names and If Present You will Get those indexes */
  }
)

>> reject() Function with find()

As we Already now about Find Lets revise and see
find() Function:

Purpose: Searches for a partial text within another text.
Output: Returns the position of the first character found.
Special Case: If the partial text is not found, it returns zero.

>> reject() Function:

Purpose: Evaluates a function for each element within a list.
Output: Returns the list elements whose evaluation result was false.
How it works: It applies a specified function to each item in a list and includes only those items where the function evaluates to false.
a!localVariables(
  local!fruits: {
    "Apple",
    "Banana",
    "Orange",
    "Strawberry",
    "Grape",
    "Watermelon",
    "Pineapple",
    "Pomegranate"
  },
  {
    /*Not THis is an Anonther Way Of Getting Values From an Array/list/Map*/
    /*wherecontains(local!fruits[{ 4, 7 }], local!fruits)*/
    /*contains(local!fruits, "Apple")*/
    /*check in an array iif value is present or not     */
    /*index(local!fruits,{3, 7,9},"Value which we normaly say ArrayIndexOutOfBoundsException"),*/
    /* A Best Example will be finding value of A[7] when size of an Array is 5 hence need 
    to see if length is less than to work Properly  */
      /*property(local!fruits,{2, 6 ,10},"Aka Value which we can give if Not Found like -1")*/
      /* Just a quick prompt Find Diffrence between to     
      tostring()
      touniformstring()
      */
    /*filter(find("na", _), local!fruits)*/
    reject(find("A", _), local!fruits)
    /*We Will Get Result 
Note Istead of local!fruits[{4,7}] you cn also Specifiy Element Names and If Present You will Get those indexes */
  }
)
Note :- Reject will Fail If you Directly pass Null Arrays hence null Check need To Be Taken Care 
Example : -
a!localVariables(
  local!fruits: {
    "Apple",
    "Banana",
    "Orange",
    "Strawberry",
    "Grape",
    "Watermelon",
    "Pineapple",
    "Pomegranate"
  },
  local!null,
  {
    /*Not THis is an Anonther Way Of Getting Values From an Array/list/Map*/
    /*wherecontains(local!fruits[{ 4, 7 }], local!fruits)*/
    /*contains(local!fruits, "Apple")*/
    /*check in an array iif value is present or not     */
    /*index(local!fruits,{3, 7,9},"Value which we normaly say ArrayIndexOutOfBoundsException"),*/
    /* A Best Example will be finding value of A[7] when size of an Array is 5 hence need 
    to see if length is less than to work Properly  */
      /*property(local!fruits,{2, 6 ,10},"Aka Value which we can give if Not Found like -1")*/
      /* Just a quick prompt Find Diffrence between to     
      tostring()
      touniformstring()
      */
    /*filter(find("na", _), local!fruits)*/
    /*reject(find("A", _), local!fruits)*/
    reject(fn!isnull,local!null)
    /*We Will Get Result 
Note Istead of local!fruits[{4,7}] you cn also Specifiy Element Names and If Present You will Get those indexes */
  }
)
Hence  we will Use Null Check Before using reject function.
a!localVariables(
  local!fruits: {
    "Apple",
    "Banana",
    "Orange",
    "Strawberry",
    "Grape",
    "Watermelon",
    "Pineapple",
    "Pomegranate"
  },
  local!null,
  {
    /*Not THis is an Anonther Way Of Getting Values From an Array/list/Map*/
    /*wherecontains(local!fruits[{ 4, 7 }], local!fruits)*/
    /*contains(local!fruits, "Apple")*/
    /*check in an array iif value is present or not     */
    /*index(local!fruits,{3, 7,9},"Value which we normaly say ArrayIndexOutOfBoundsException"),*/
    /* A Best Example will be finding value of A[7] when size of an Array is 5 hence need 
    to see if length is less than to work Properly  */
      /*property(local!fruits,{2, 6 ,10},"Aka Value which we can give if Not Found like -1")*/
      /* Just a quick prompt Find Diffrence between to     
      tostring()
      touniformstring()
      */
    /*filter(find("na", _), local!fruits)*/
    /*reject(find("A", _), local!fruits)*/
    if(a!isNullOrEmpty(local!null),-1,reject(fn!isnull,local!null))
    /*We Will Get Result 
Note Istead of local!fruits[{4,7}] you cn also Specifiy Element Names and If Present You will Get those indexes */
  }
)

>> displayvalue()

It receives 4 parameters:
Value to search, array containing the value, array with replacement values ​​and default value.
displayvalue() looks for the value in the first array and returns the index, with that index it searches the second array and returns its value, if the index does not exist, it returns the default value.


a!localVariables(
  /*Note Informative part you can user ddd or eee for Full weekday name  */
  /*For three initials of Week Day Use eee  */
  local!weekDayFull: a!forEach(enumerate(7), text(fv!item, "DDDD")),
  local!weekDay2Dig: a!forEach(local!weekDayFull, left(fv!item, 2)),
  {
    displayvalue(
      text(today(), "DDDD"),
      local!weekDayFull,
      local!weekDay2Dig,
      "Invalid Week"
    )
  }
)

Leave a comment

Create a website or blog at WordPress.com

Up ↑

Discover more from Appian Tips

Subscribe now to keep reading and get access to the full archive.

Continue reading