<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.appian.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>reduce() and merge()</title><link>https://community.appian.com/discussions/f/general/29784/reduce-and-merge</link><description>Hello, 
 I&amp;#39;m preparing to take a certification exam, and I&amp;#39;m struggling to find a use case that would help me better understand functions reduce() and merge(). Can you help a brother out?</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: reduce() and merge()</title><link>https://community.appian.com/thread/118205?ContentTypeID=1</link><pubDate>Mon, 28 Aug 2023 12:40:04 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:c570a3f0-e3fa-4334-b8c4-e963330f469d</guid><dc:creator>Witold Wozniak</dc:creator><description>&lt;p&gt;Thank you! I try to read your blog regularly.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: reduce() and merge()</title><link>https://community.appian.com/thread/118173?ContentTypeID=1</link><pubDate>Mon, 28 Aug 2023 06:26:53 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:9ee9483c-d2c1-445d-8296-b05e06b4d997</guid><dc:creator>Stefan Helzle</dc:creator><description>&lt;p&gt;Check out my blog post for a use case for reduce().&lt;/p&gt;
&lt;p&gt;&lt;a href="https://appian.rocks/2022/08/29/complex-algorithms-in-appian/"&gt;appian.rocks/.../&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: reduce() and merge()</title><link>https://community.appian.com/thread/118170?ContentTypeID=1</link><pubDate>Sun, 27 Aug 2023 20:27:00 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:f5fc32de-03eb-4c09-b7b7-da680c2754ff</guid><dc:creator>Witold Wozniak</dc:creator><description>&lt;p&gt;Helps a lot! Thank you!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: reduce() and merge()</title><link>https://community.appian.com/thread/118166?ContentTypeID=1</link><pubDate>Sun, 27 Aug 2023 12:32:16 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:e873aec5-06f5-494d-aff2-19d0b8cab2a7</guid><dc:creator>Mathieu Drouin</dc:creator><description>&lt;p&gt;&lt;img alt=" " src="/resized-image/__size/640x480/__key/communityserver-discussions-components-files/11/pastedimage1693138743815v3.png" /&gt;&lt;/p&gt;
&lt;p&gt;So basically merge seems to be meant specifically to be used with the &amp;quot;legacy&amp;quot; looping functions (i.e. before a!forEach existed) that took a function/predicate and applied it to a list.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://docs.appian.com/suite/help/23.3/fnc_looping_apply.html"&gt;https://docs.appian.com/suite/help/23.3/fnc_looping_apply.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;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&amp;nbsp;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.&lt;/p&gt;
&lt;p&gt;Case in point, here is the version of the string substitution&amp;nbsp;expression with reduce()&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;a!localVariables(
  local!text: &amp;quot;My name is ###NAME### and I live in ###CITY###&amp;quot;,
  local!keys: {
    &amp;quot;###NAME###&amp;quot;,
    &amp;quot;###CITY###&amp;quot;
  },
  local!values: {
    &amp;quot;Mat&amp;quot;,
    &amp;quot;Montreal&amp;quot;
  },
  reduce(
    substitute(_, _, _),
    local!text,
    merge(local!keys, local!values)
  )
)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="/resized-image/__size/640x480/__key/communityserver-discussions-components-files/11/pastedimage1693139192098v4.png" /&gt;&lt;/p&gt;
&lt;p&gt;vs if you&amp;nbsp;tried to do&amp;nbsp;the same thing with a!forEach()&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;a!localVariables(
  local!text: &amp;quot;My name is ###NAME### and I live in ###CITY###&amp;quot;,
  local!keys: { &amp;quot;###NAME###&amp;quot;, &amp;quot;###CITY###&amp;quot; },
  local!values: { &amp;quot;Mat&amp;quot;, &amp;quot;Montreal&amp;quot; },
  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)
    )
  )
)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="/resized-image/__size/640x480/__key/communityserver-discussions-components-files/11/pastedimage1693139374742v6.png" /&gt;&lt;/p&gt;
&lt;p&gt;Hope this helps!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: reduce() and merge()</title><link>https://community.appian.com/thread/118165?ContentTypeID=1</link><pubDate>Sun, 27 Aug 2023 09:47:08 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:8a51f2da-2b3a-4ed7-9bac-4d2048db775c</guid><dc:creator>Witold Wozniak</dc:creator><description>&lt;p&gt;Thank you, but I might have explained my problem wrong.&lt;/p&gt;
&lt;p&gt;I could not come up with a use case, where merge() would be superior over, for example, append() or insert()? I know there&amp;#39;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: reduce() and merge()</title><link>https://community.appian.com/thread/118164?ContentTypeID=1</link><pubDate>Sat, 26 Aug 2023 22:02:19 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:3ff61d03-7ede-4873-b5cf-2093cc0c8ac3</guid><dc:creator>Mathieu Drouin</dc:creator><description>&lt;p&gt;For the merge, the documentation seems pretty self explanatory. It merges lists.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://docs.appian.com/suite/help/23.2/fnc_looping_merge.html"&gt;docs.appian.com/.../fnc_looping_merge.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As for the reduce function, this will probably explain it better than I can: &lt;a href="https://blog.khanacademy.org/lets-reduce-a-gentle-introduction-to-javascripts-reduce-method/"&gt;blog.khanacademy.org/.../&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>