{"id":1421,"date":"2021-02-16T14:01:56","date_gmt":"2021-02-16T01:01:56","guid":{"rendered":"https:\/\/www.ronella.xyz\/?p=1421"},"modified":"2021-02-16T14:01:57","modified_gmt":"2021-02-16T01:01:57","slug":"gosu-collection-enhancements-functions","status":"publish","type":"post","link":"https:\/\/www.ronella.xyz\/?p=1421","title":{"rendered":"Gosu Collection Enhancements Functions"},"content":{"rendered":"<h3>flatMap<\/h3>\n<p>Maps each element of the Collection to a Collection of values and then flattens them into a single List.<\/p>\n<h4>Syntax<\/h4>\n<pre><code>flatMap&lt;R&gt;(mapper(item : Collection&lt;?&gt;) : Collection&lt;R&gt;) : List&lt;R&gt;<\/code><\/pre>\n<h4>Example<\/h4>\n<pre><code class=\"language-gosu\">var items = {{1},{2,2},{3,3,3},{4,4,4,4}}\nprint(items.flatMap(\\ ___item -&gt; ___item))<\/code><\/pre>\n<h5>Output<\/h5>\n<pre><code>[1, 2, 2, 3, 3, 3, 4, 4, 4, 4]<\/code><\/pre>\n<hr \/>\n<h3>fold<\/h3>\n<p>Accumulates the values of an Collection<T> into a single T.<\/p>\n<h4>Syntax<\/h4>\n<pre><code>fold(aggregator(aggregate : T, item : T) : T) : T<\/code><\/pre>\n<h4>Example<\/h4>\n<pre><code>var items = {{1},{2,2},{3,3,3},{4,4,4,4}}\nvar flatItems = items.flatMap(\\ ___item -&gt; ___item)\nprint(flatItems.fold(\\ ___aggr, ___item -&gt; ___aggr + ___item))<\/code><\/pre>\n<h5>Output<\/h5>\n<pre><code>30<\/code><\/pre>\n<hr \/>\n<h3>intersect<\/h3>\n<p>Returns a Set<T> that is the intersection of the two Collection objects.<\/p>\n<h4>Syntax<\/h4>\n<pre><code>intersect(that: Collection&lt;T&gt;) : Set&lt;T&gt;<\/code><\/pre>\n<h4>Example<\/h4>\n<pre><code>var items1 = {1,2,3,4,5,6}\nvar items2 = {4,5,6,7,8,9}\nprint(items1.intersect(items2))<\/code><\/pre>\n<h5>Output<\/h5>\n<pre><code>[4, 5, 6]<\/code><\/pre>\n<hr \/>\n<h3>map<\/h3>\n<p>Returns a List of each element of the Collection<T> mapped to a new value.<\/p>\n<h4>Syntax<\/h4>\n<pre><code>map&lt;Q&gt;(mapper(item : &lt;INPUT&gt;) : &lt;Q&gt;) : List&lt;Q&gt;<\/code><\/pre>\n<h4>Example<\/h4>\n<pre><code>var numbers = {1,2,3}\nvar words = {&quot;one&quot;,&quot;two&quot;,&quot;three&quot;}\nprint(numbers.map(\\ ___number -&gt; words[___number-1]))<\/code><\/pre>\n<h5>Output<\/h5>\n<pre><code>[one, two, three]<\/code><\/pre>\n<hr \/>\n<h3>partition<\/h3>\n<p>Partitions this Collection into a Map of keys to a list of elements in this Collection.<\/p>\n<h4>Syntax<\/h4>\n<pre><code>partition&lt;Q&gt;(partitioner(item : R) : Q) : Map&lt;Q, List&lt;R&gt;&gt;<\/code><\/pre>\n<h4>Example<\/h4>\n<pre><code>var numbers = {1,2,3,4,5,6,8}\nprint(numbers.partition&lt;String&gt;(\\ ___number -&gt; ___number % 2 == 0 ? &quot;even&quot; : &quot;odd&quot;))<\/code><\/pre>\n<h5>Output<\/h5>\n<pre><code>{even=[2, 4, 6, 8], odd=[1, 3, 5]}<\/code><\/pre>\n<hr \/>\n<h3>reduce<\/h3>\n<p>Accumulates the values of a Collection<T> into a single V given an initial seed value<\/p>\n<h4>Syntax<\/h4>\n<pre><code>reduce&lt;T&gt;(init : T, aggregrator(aggregate: T, item : ?) : T) : T<\/code><\/pre>\n<h4>Example<\/h4>\n<pre><code>var numbers = {1,2,3,4,5,6,8}\nvar sumOfNumbers = numbers.reduce(0, \\ ___aggr, ___number -&gt; ___aggr + ___number)\nprint(sumOfNumbers)<\/code><\/pre>\n<h5>Output<\/h5>\n<pre><code>29<\/code><\/pre>\n<hr \/>\n<h3>union<\/h3>\n<p>Returns a new Set<T> that is the union of the two Collections<\/p>\n<h4>Syntax<\/h4>\n<pre><code>union(that : Collection&lt;T&gt;) : Set&lt;T&gt;<\/code><\/pre>\n<h4>Example<\/h4>\n<pre><code>var items1 = {1,2,3,4,5,6}\nvar items2 = {4,5,6,7,8,9}\nprint(items1.union(items2))<\/code><\/pre>\n<h5>Output<\/h5>\n<pre><code>[1, 2, 3, 4, 5, 6, 7, 8, 9]<\/code><\/pre>\n<hr \/>\n<h3>disjunction<\/h3>\n<p>Returns a new Set<T> that is the set disjunction of this collection and the other collection<\/p>\n<h4>Syntax<\/h4>\n<pre><code>disjunction(that : Collection&lt;T&gt;) : Set&lt;T&gt;<\/code><\/pre>\n<h4>Example<\/h4>\n<pre><code>var items1 = {1,2,3,4,5,6}\nvar items2 = {4,5,6,7,8,9}\nprint(items1.disjunction(items2))<\/code><\/pre>\n<h5>Output<\/h5>\n<pre><code>[1, 2, 3, 7, 8, 9]<\/code><\/pre>\n<hr \/>\n<h3>join<\/h3>\n<p>Joins all elements together as a string with a delimiter<\/p>\n<h4>Syntax<\/h4>\n<pre><code>join(delim : String) : String<\/code><\/pre>\n<h4>Example<\/h4>\n<pre><code>var words = {&quot;Hello&quot;, &quot;World&quot;}\nprint(words.join(&quot; &quot;))<\/code><\/pre>\n<h5>Output<\/h5>\n<pre><code>Hello World<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>flatMap Maps each element of the Collection to a Collection of values and then flattens them into a single List. Syntax flatMap&lt;R&gt;(mapper(item : Collection&lt;?&gt;) : Collection&lt;R&gt;) : List&lt;R&gt; Example var items = {{1},{2,2},{3,3,3},{4,4,4,4}} print(items.flatMap(\\ ___item -&gt; ___item)) Output [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] fold Accumulates the values of an Collection [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,60],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1421"}],"collection":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1421"}],"version-history":[{"count":1,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1421\/revisions"}],"predecessor-version":[{"id":1422,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1421\/revisions\/1422"}],"wp:attachment":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}