reduce() and merge()

Certified Associate Developer

Hello,

I'm preparing to take a certification exam, and I'm struggling to find a use case that would help me better understand functions reduce() and merge(). Can you help a brother out?

  Discussion posts and replies are publicly visible

Parents
  • +1
    Certified Lead Developer

    For the merge, the documentation seems pretty self explanatory. It merges lists.

    As for the reduce function, this will probably explain it better than I can:

  • 0
    Certified Associate Developer
    in reply to Mathieu Drouin

    Thank you, but I might have explained my problem wrong.

    I could not come up with a use case, where merge() would be superior over, for example, append() or insert()? I know there's a difference in how these work, but if our goal is just to join arrays, then we have other functions that can accomplish that as well. I wanted to know whether there are cases in which merge() is an objectively better choice, rather than a matter of preference, which seems to be the case when speaking about combining arrays.

    With reduce(), again, I understand the premise, but I struggle to find a practical use case that would help me test my understanding of the function.

  • +1
    Certified Lead Developer
    in reply to Witold Wozniak

    So basically merge seems to be meant specifically to be used with the "legacy" looping functions (i.e. before a!forEach existed) that took a function/predicate and applied it to a list.

    https://docs.appian.com/suite/help/23.3/fnc_looping_apply.html

    Now I say legacy, most of the functions in that list can be replaced with forEach with the exception of reduce() which does still has some uses. A real world example that may help you understand how it works would be a string substitution (i.e. you want to subsitute multiple keys in a string). Reduce allows you to loop through the list and apply the function but on each iteration it will take the output from the last iteration as an input to the current iteration.

    Case in point, here is the version of the string substitution expression with reduce()

    a!localVariables(
      local!text: "My name is ###NAME### and I live in ###CITY###",
      local!keys: {
        "###NAME###",
        "###CITY###"
      },
      local!values: {
        "Mat",
        "Montreal"
      },
      reduce(
        substitute(_, _, _),
        local!text,
        merge(local!keys, local!values)
      )
    )

    vs if you tried to do the same thing with a!forEach()

    a!localVariables(
      local!text: "My name is ###NAME### and I live in ###CITY###",
      local!keys: { "###NAME###", "###CITY###" },
      local!values: { "Mat", "Montreal" },
      a!forEach(
        items: local!keys,
        expression: a!localVariables(
          local!key: fv!item,
          local!value: index(local!values, fv!index, null),
          substitute(local!text, local!key, local!value)
        )
      )
    )

    Hope this helps!

  • 0
    Certified Associate Developer
    in reply to Mathieu Drouin

    Helps a lot! Thank you!

  • +1
    Certified Lead Developer
    in reply to Witold Wozniak

    Check out my blog post for a use case for reduce().

    appian.rocks/.../

Reply Children