<?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>Function reduce() and list of maps</title><link>https://community.appian.com/discussions/f/general/28888/function-reduce-and-list-of-maps</link><description>Hi, 
 i have the following code 
 
 a!localVariables( local!sensor: { a!map(data: { 125, 92, 22, 63, 73, 28, 19 }), a!map(data: { 35, 65, 92, 22, 63, 73, 28 }), a!map(data: { 2, 35, 65, 22, 63, 73, 18, 29 }), a!map(data: { 9, 35, 65, 63, 35, 28, 19 }</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: Function reduce() and list of maps</title><link>https://community.appian.com/thread/113893?ContentTypeID=1</link><pubDate>Wed, 07 Jun 2023 13:28:05 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:d0a96462-d518-41af-9bcb-9f3b6a485c56</guid><dc:creator>Marco Minoia</dc:creator><description>&lt;p&gt;Thanks to all!&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Yes sure, real case doesn&amp;#39;t need reduce to perform this operation... but it is an Appian Online Course Exercise (&lt;span&gt;&lt;span class="ui-provider gs b c d e f g h i j k l m n o p q r s t u v w x y z ab ac ae af ag ah ai aj ak" dir="ltr"&gt;VOF_AdvExpP2_L2_Ex1_Answer)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;It was jut to understand the behavior of reduce() in this particular case.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Function reduce() and list of maps</title><link>https://community.appian.com/thread/113889?ContentTypeID=1</link><pubDate>Wed, 07 Jun 2023 12:52:26 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:7acf3bce-4034-46d4-936d-6769b2277870</guid><dc:creator>Peter Lewis</dc:creator><description>&lt;p&gt;Yeah I can&amp;#39;t really think of a scenario where you would use reduce with the sum() function. Stewart is correct, but for sum you don&amp;#39;t even need to flatten it - just sum(local!sensor.data) will work too!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Function reduce() and list of maps</title><link>https://community.appian.com/thread/113853?ContentTypeID=1</link><pubDate>Wed, 07 Jun 2023 08:52:25 GMT</pubDate><guid isPermaLink="false">d3a83456-d57b-489c-a84c-4e8267bb592a:08e22b29-58c5-426a-b27c-27f950d31e77</guid><dc:creator>Stewart Burchell</dc:creator><description>&lt;p&gt;The reduce() function loops on the list of lists that you&amp;#39;ve provided and the fourth list is one item longer than the others (i.e. the value 29). The function can only work correctly on lists that are the same length. To make it work you&amp;#39;ll need to add an additional value 0 to all of the lists that are longer thus:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;a!localVariables(
  local!sensor: {
    a!map(data: { 125, 92, 22, 63, 73, 28, 19, 0 }),
    a!map(data: { 35, 65, 92, 22, 63, 73, 28, 0 }),
    a!map(data: { 2, 35, 65, 22, 63, 73, 18, 29 }),
    a!map(data: { 9, 35, 65, 63, 35, 28, 19, 0 })
  },
  reduce(fn!sum, 0, local!sensor.data)
)&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Alternatively, you can flatten the list and then simply use fn!sum() thus:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;a!localVariables(
  local!sensor: {
    a!map(data: { 125, 92, 22, 63, 73, 28, 19 }),
    a!map(data: { 35, 65, 92, 22, 63, 73, 28 }),
    a!map(data: { 2, 35, 65, 22, 63, 73, 18, 29 }),
    a!map(data: { 9, 35, 65, 63, 35, 28, 19 })
  },
  fn!sum(
    a!flatten(local!sensor.data)
  )
)&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>