conventional method to find the gratest number using for loop
Discussion posts and replies are publicly visible
Why do you want to do it without the max() function when you can use it?
was practicing to do same as in c , using a local temp variable
reduce( rule!getMaxNumber(accumulator: _, item: _), 0, ri!array )
We can use reduce function
Create a rule which will compare the current item and accumulator and return the max number and use that rule as predicate in reduce fucntion
Or use the sort function:
a!localVariables( local!list:{5,4,7,9,2,5,0}, sort(local!list)[length(local!list)] )
This might be helpful.
a!localVariables( local!listOfNumber: { 67, 80, 100, 102, 150 }, a!forEach( items: local!listOfNumber, expression: a!localVariables( local!item: fv!item, local!updatedList: remove(local!listOfNumber, fv!index), if( count( wherecontains( true, a!forEach( items: local!updatedList, expression: local!item > fv!item ) ) ) = count(local!updatedList), fv!item, {} ) ) ) )
Here is an O(n) approach implemented like C, employing a temporary variable to store the current maximum (rule input ). In this implementation, a recursive approach has been utilized. If you have any questions or need further clarification, feel free to ask. if( ri!currentIndex > length(ri!arr), ri!currentMax, rule!maxNum( arr: ri!arr, currentMax: if( ri!currentMax > ri!arr[ri!currentIndex], ri!currentMax, ri!arr[ri!currentIndex] ), currentIndex: ri!currentIndex + 1 ) ) /*arr: array in which we needs to find max num*/ /*currentMax: first element of array*/ /*current index: first index of array (1 in case of appian)*/
if( ri!currentIndex > length(ri!arr), ri!currentMax, rule!maxNum( arr: ri!arr, currentMax: if( ri!currentMax > ri!arr[ri!currentIndex], ri!currentMax, ri!arr[ri!currentIndex] ), currentIndex: ri!currentIndex + 1 ) ) /*arr: array in which we needs to find max num*/ /*currentMax: first element of array*/ /*current index: first index of array (1 in case of appian)*/
result->
I'd point out that you're not actually declaring any local variable in this rule, and thus it's unnecessary to even call a!localVariables() other than potentially if you want to add one in the future for easier understanding.
I was going with a different approach with local variables and forgot to remove a!localVariables() when I moved to rule inputs.
Hi romeoraja ,
Below one might be helpful.
a!localVariables( local!list: { 67, 80, 100, 102, 150 }, local!map: a!forEach( items: local!list, expression: a!map(number: fv!item) ), index( index( index( todatasubset( arrayToPage: local!map, pagingConfiguration: a!pagingInfo( startIndex: 1, batchSize: length(local!list), sort: a!sortInfo(field: "number", ascending: false) ) ), "data", {} ), "number", {} ), 1, {} ) )
sort is actually an undocumented function. This neither comes up as a suggestion nor there is any documentation page for the same. I won't advise using it.