<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: partition code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Tue, 22 Jul 2008 23:08:14 GMT</pubDate>
    <description>DZone Snippets: partition code</description>
    <item>
      <title>SPLIT-UNIQUE - split a block into unique and duplicate values</title>
      <link>http://snippets.dzone.com/posts/show/2948</link>
      <description>&lt;code&gt;&lt;br /&gt;    split-unique: func [block [any-block!] /local uniq dupe dest] [&lt;br /&gt;        uniq: copy []&lt;br /&gt;        dupe: copy []&lt;br /&gt;        foreach item block [&lt;br /&gt;            dest: either find/only uniq item [dupe] [uniq]&lt;br /&gt;            append/only dest item&lt;br /&gt;        ]&lt;br /&gt;        reduce [uniq dupe]&lt;br /&gt;    ]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 01 Nov 2006 21:35:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2948</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>GROUP - group like elements in a block</title>
      <link>http://snippets.dzone.com/posts/show/2947</link>
      <description>&lt;code&gt;&lt;br /&gt;    group: func [&lt;br /&gt;        {Returns a block of sub-blocks with items partitioned by value.}&lt;br /&gt;        block  [any-block!]&lt;br /&gt;        /local result&lt;br /&gt;    ][&lt;br /&gt;        result: copy []&lt;br /&gt;        ; First, build up a list of keys, with a place for values&lt;br /&gt;        ; to go with each key.&lt;br /&gt;        foreach item block [&lt;br /&gt;            if not find/only/skip result item 2 [&lt;br /&gt;                repend result [item copy []]&lt;br /&gt;            ]&lt;br /&gt;        ]&lt;br /&gt;        ; Add items to the block associated with each key.&lt;br /&gt;        foreach item block [append/only select result item item]&lt;br /&gt;        result&lt;br /&gt;    ]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 01 Nov 2006 21:30:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2947</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>Partitioning an array with Array#collect_every  and  Array#subdivide</title>
      <link>http://snippets.dzone.com/posts/show/2603</link>
      <description>Array#collect_every is taken from: &lt;br /&gt;&lt;br /&gt;http://redhanded.hobix.com/bits/matchingIntoMultipleAssignment.html&lt;br /&gt;&lt;br /&gt;Author: Ezra Zygmuntowicz (in the comments)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;# ---------------------------------------------------------------------------&lt;br /&gt;# collect_every(n [,fill=false[,offset=0]])                  =&gt; an array&lt;br /&gt;# collect_every(n [,fill=false[,offset=0]]) {|item| block}   =&gt; an_array&lt;br /&gt;# ---------------------------------------------------------------------------&lt;br /&gt;# If a block is given, it invokes the block passing in an array of n elements.&lt;br /&gt;# The last array passed may not contain n elements if size % 2 does not equal&lt;br /&gt;# zero. If no block is given, it returns an array containing the collections.&lt;br /&gt;#&lt;br /&gt;# If the optional argument fill is set to true, the empty spaces will be&lt;br /&gt;# filled with nils. The optional argument offset allows the collection to &lt;br /&gt;# start at that index in the array.&lt;br /&gt;#&lt;br /&gt;# a = (1..10).to_a&lt;br /&gt;# a.collect_every(5)               #=&gt; [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]&lt;br /&gt;# a.collect_every(5) {|x| p x}     #=&gt; [1, 2, 3, 4, 5]&lt;br /&gt;#                                      [6, 7, 8, 9, 10]&lt;br /&gt;# b = (1..7).to_a&lt;br /&gt;# b.collect_every(3)               #=&gt; [[1, 2, 3], [4, 5, 6], [7]]&lt;br /&gt;# b.collect_every(3,true)          #=&gt; [[1, 2, 3], [4, 5, 6], [7,nil,nil]]&lt;br /&gt;# b.collect_every(3,true,1)        #=&gt; [[2, 3, 4], [5, 6, 7]]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class Array&lt;br /&gt;&lt;br /&gt; def collect_every(n, fill=false, offset = 0)&lt;br /&gt;&lt;br /&gt;  if block_given?&lt;br /&gt;     while  offset &lt; size&lt;br /&gt;          ret = []&lt;br /&gt;&lt;br /&gt;          if fill&lt;br /&gt;             n.times do |x| &lt;br /&gt;                  if offset + x &gt; size - 1 then ret &lt;&lt; nil &lt;br /&gt;                  else  ret &lt;&lt; self[offset + x] end&lt;br /&gt;             end&lt;br /&gt;          else&lt;br /&gt;             n.times { |x| ret &lt;&lt; self[offset + x] unless offset + x &gt; size - 1}&lt;br /&gt;          end &lt;br /&gt;&lt;br /&gt;          offset += n&lt;br /&gt;          yield ret&lt;br /&gt;          ret = nil&lt;br /&gt;     end   &lt;br /&gt;&lt;br /&gt;  else&lt;br /&gt;&lt;br /&gt;   ret = []&lt;br /&gt;   while  offset &lt; size&lt;br /&gt;     ret &lt;&lt; []&lt;br /&gt;     if fill&lt;br /&gt;         n.times do |x|  &lt;br /&gt;            if offset + x &gt; size - 1 then ret.last &lt;&lt; nil&lt;br /&gt;            else ret.last &lt;&lt; self[offset + x]; end&lt;br /&gt;         end&lt;br /&gt;     else&lt;br /&gt;         n.times { |x| ret.last &lt;&lt; self[offset + x] unless offset + x &gt; size - 1 }&lt;br /&gt;     end&lt;br /&gt;&lt;br /&gt;     offset += n&lt;br /&gt;   end&lt;br /&gt;   return ret&lt;br /&gt;&lt;br /&gt;  end &lt;br /&gt;&lt;br /&gt; end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;a = (1..10).to_a&lt;br /&gt;puts a.collect_every(5).inspect                     #-&gt;  [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]&lt;br /&gt;a.collect_every(5) { |x| puts x.inspect }         #-&gt;  [1, 2, 3, 4, 5] and [6, 7, 8, 9, 10]&lt;br /&gt;puts a.collect_every(3, true).inspect             #-&gt;  [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, nil, nil]]&lt;br /&gt;puts a.collect_every(3, true,1).inspect          #-&gt;  [[2, 3, 4], [5, 6, 7], [8, 9, 10]]&lt;br /&gt;puts a.collect_every(3, true,4).inspect          #-&gt;  [[5, 6, 7], [8, 9, 10]]&lt;br /&gt;&lt;br /&gt;puts a.inspect                                              #-&gt;  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# ------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class Array&lt;br /&gt;&lt;br /&gt; def subdivide(n)&lt;br /&gt;   return self if n &lt;= 0 || n &gt;= self.size&lt;br /&gt;   result = []&lt;br /&gt;   max_subarray_size = n - 1&lt;br /&gt;&lt;br /&gt;     while self.size &gt; 0&lt;br /&gt;       result &lt;&lt; self.slice!(0..max_subarray_size)&lt;br /&gt;     end&lt;br /&gt;&lt;br /&gt;   result&lt;br /&gt; end&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;a = ('a'..'g').to_a&lt;br /&gt;b = a.subdivide(3)&lt;br /&gt;&lt;br /&gt;puts b.inspect       #-&gt;  [["a", "b", "c"], ["d", "e", "f"], ["g"]]&lt;br /&gt;puts a.inspect       #-&gt;  [] &lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 16 Sep 2006 14:11:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2603</guid>
      <author>ntk ()</author>
    </item>
  </channel>
</rss>
