Recursion in Appian

Certified Associate Developer

Hi everyone,

Hope everyone is doing well and safe.

As I am new to Appian, Could anyone of you help me for this question,

is it possible to apply recursion concept in appian ?what is recursion in appian? how it will work?

Thank you all in advance!

Regards,

Kiran Sajjan

  Discussion posts and replies are publicly visible

Parents
  • Hi Kiran,

    I echo Harshit's sentiments and would aim to avoid recursion in Appian and if needed would incorporate a "depth" or "loop" check to ensure an infinite (or very large) recursion isn't created. I would also encourage you to check out the reduce() function (https://docs.appian.com/suite/help/21.4/fnc_looping_reduce.html) is a much neater way to have a rule or function call itself. I will admit though, the specifics of using reduce can be a little confusing.

    Often there is other ways to achieve your goal without recursion and it's usually, but not always, by using iteration and the a!forEach() function (https://docs.appian.com/suite/help/21.4/fnc_looping_a_foreach.html). So again, avoid using recursion unless it cannot be done any other way.

    My primary use of recursion is in a function that calls "Deep Index" (no idea if Stefan originally made and named this function in Appian but I found it from a post of his) where you recursively call indexes down a hierarchy. See the below code and let me know if you have any questions. I've included a depth check in the code to give you an idea of the kind of fail safe that should be considered when making anything that could result in an infinite loop.

    a!localVariables(
      local!input: {
        one: {
          a: {
            i: 1,
            ii: 2,
            iii: 3
          },
          b: {
            i: 4,
            ii: 5,
            iii: 6
          },
          c: {
            i: 7,
            ii: 8,
            iii: 9
          }
        },
        two: {
          a: {
            i: 10,
            ii: 11,
            iii: 12
          },
          b: {
            i: 13,
            ii: 14,
            iii: 15
          },
          c: {
            i: 16,
            ii: 17,
            iii: 18
          }
        },
        three: {
          a: {
            i: 19,
            ii: 20,
            iii: 21
          },
          b: {
            i: 22,
            ii: 23,
            iii: 24
          },
          c: {
            i: 25,
            ii: 26,
            iii: 27
          }
        }
      },
      local!path: {
        "two",
        "b",
        "i"
      },
      local!default: "Not a valid path",
      local!depthLimit: 100, /*This is the limit on how many times index can be called*/
      local!pathLength: if(
        a!isNullOrEmpty(local!path),
        0,
        length(local!path)
      ),
      if(
        local!pathLength > local!depthLimit,
        concat(
          "Recursion depth would exceed limit of ",
          local!depthLimit,
          ", call aborted"
        ),
        reduce(
          index(_,_,local!default),
          local!input,
          local!path
        )
      )
    )

Reply
  • Hi Kiran,

    I echo Harshit's sentiments and would aim to avoid recursion in Appian and if needed would incorporate a "depth" or "loop" check to ensure an infinite (or very large) recursion isn't created. I would also encourage you to check out the reduce() function (https://docs.appian.com/suite/help/21.4/fnc_looping_reduce.html) is a much neater way to have a rule or function call itself. I will admit though, the specifics of using reduce can be a little confusing.

    Often there is other ways to achieve your goal without recursion and it's usually, but not always, by using iteration and the a!forEach() function (https://docs.appian.com/suite/help/21.4/fnc_looping_a_foreach.html). So again, avoid using recursion unless it cannot be done any other way.

    My primary use of recursion is in a function that calls "Deep Index" (no idea if Stefan originally made and named this function in Appian but I found it from a post of his) where you recursively call indexes down a hierarchy. See the below code and let me know if you have any questions. I've included a depth check in the code to give you an idea of the kind of fail safe that should be considered when making anything that could result in an infinite loop.

    a!localVariables(
      local!input: {
        one: {
          a: {
            i: 1,
            ii: 2,
            iii: 3
          },
          b: {
            i: 4,
            ii: 5,
            iii: 6
          },
          c: {
            i: 7,
            ii: 8,
            iii: 9
          }
        },
        two: {
          a: {
            i: 10,
            ii: 11,
            iii: 12
          },
          b: {
            i: 13,
            ii: 14,
            iii: 15
          },
          c: {
            i: 16,
            ii: 17,
            iii: 18
          }
        },
        three: {
          a: {
            i: 19,
            ii: 20,
            iii: 21
          },
          b: {
            i: 22,
            ii: 23,
            iii: 24
          },
          c: {
            i: 25,
            ii: 26,
            iii: 27
          }
        }
      },
      local!path: {
        "two",
        "b",
        "i"
      },
      local!default: "Not a valid path",
      local!depthLimit: 100, /*This is the limit on how many times index can be called*/
      local!pathLength: if(
        a!isNullOrEmpty(local!path),
        0,
        length(local!path)
      ),
      if(
        local!pathLength > local!depthLimit,
        concat(
          "Recursion depth would exceed limit of ",
          local!depthLimit,
          ", call aborted"
        ),
        reduce(
          index(_,_,local!default),
          local!input,
          local!path
        )
      )
    )

Children
No Data