<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: chunk code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 23:11:12 GMT</pubDate>
    <description>DZone Snippets: chunk code</description>
    <item>
      <title>Split array into smaller arrays of equal size</title>
      <link>http://snippets.dzone.com/posts/show/3486</link>
      <description>Split an array of elements into a set of smaller arrays of equal size. Extra elements are preferentially assigned to earlier arrays. If there are no elements in a given returned array it will be [] (empty array)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# use as standalone function&lt;br /&gt;def chunk_array(array, pieces=2)&lt;br /&gt;  len = array.length;&lt;br /&gt;  mid = (len/pieces)&lt;br /&gt;  chunks = []&lt;br /&gt;  start = 0&lt;br /&gt;  1.upto(pieces) do |i|&lt;br /&gt;    last = start+mid&lt;br /&gt;    last = last-1 unless len%pieces &gt;= i&lt;br /&gt;    chunks &lt;&lt; array[start..last] || []&lt;br /&gt;    start = last+1&lt;br /&gt;  end&lt;br /&gt;  chunks&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# use as array.chunk&lt;br /&gt;class Array&lt;br /&gt;  def chunk(pieces=2)&lt;br /&gt;    len = self.length;&lt;br /&gt;    mid = (len/pieces)&lt;br /&gt;    chunks = []&lt;br /&gt;    start = 0&lt;br /&gt;    1.upto(pieces) do |i|&lt;br /&gt;      last = start+mid&lt;br /&gt;      last = last-1 unless len%pieces &gt;= i&lt;br /&gt;      chunks &lt;&lt; self[start..last] || []&lt;br /&gt;      start = last+1&lt;br /&gt;    end&lt;br /&gt;    chunks&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Examples of use:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt; chunk_array [1,2,3,4,5,6], 2&lt;br /&gt;=&gt; [[1, 2, 3], [4, 5, 6]]&lt;br /&gt;&lt;br /&gt;&gt;&gt; chunk_array [1,2,3,4,5,6], 3&lt;br /&gt;=&gt; [[1, 2], [3, 4], [5, 6]]&lt;br /&gt;&lt;br /&gt;&gt;&gt; chunk_array [1,2,3,4,5,6], 4&lt;br /&gt;=&gt; [[1, 2], [3, 4], [5], [6]]&lt;br /&gt;&lt;br /&gt;&gt;&gt; chunk_array [1,2,3,4,5,6,7,8,9,10], 4&lt;br /&gt;=&gt; [[1, 2, 3], [4, 5, 6], [7, 8], [9, 10]]&lt;br /&gt;&lt;br /&gt;&gt;&gt; chunk_array [1,2,3], 4&lt;br /&gt;=&gt; [[1], [2], [3], []]&lt;br /&gt;&lt;br /&gt;&gt;&gt; chunk_array [], 2&lt;br /&gt;=&gt; [[], []]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;if you prefer the second form (more ruby-ish, but not always appropriate)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt; [1,2,3,4,5,6,7,8,9,10].chunk&lt;br /&gt;=&gt; [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]&lt;br /&gt;&lt;br /&gt;&gt;&gt; [1,2,3,4,5,6,7,8,9,10].chunk 3&lt;br /&gt;=&gt; [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This is handy when used with a splat because you can do things like:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;left, right = *chunk_array(all,2)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 09 Feb 2007 22:52:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3486</guid>
      <author>mattsa (Matt Sanders)</author>
    </item>
    <item>
      <title>Database connection chunk</title>
      <link>http://snippets.dzone.com/posts/show/3093</link>
      <description>&lt;code&gt;&lt;br /&gt;mysql_select_db($database_conname, $conname);&lt;br /&gt;&lt;br /&gt;$query_name = "SELECT * FROM tablename ORDER BY column DESC LIMIT 0, 10";&lt;br /&gt;$name = mysql_query($query_name, $conname) or die(mysql_error());&lt;br /&gt;$row_name = mysql_fetch_assoc($conname);&lt;br /&gt;$totalrows_name = mysql_num_rows($conname);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 05 Dec 2006 21:53:19 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3093</guid>
      <author>plasticated ()</author>
    </item>
    <item>
      <title>Split String into roughly equal-sized chunks.</title>
      <link>http://snippets.dzone.com/posts/show/2631</link>
      <description>Split a string into an array of roughly equal sized chunks based on a string or regular expression delimiter.&lt;br /&gt;Delimiter is preserved in output.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class String&lt;br /&gt;  def chunk_string(average_segment_size = 40, sclice_on = /\s+/)&lt;br /&gt;    out = []&lt;br /&gt;    slices_estimate = self.size.divmod(average_segment_size)&lt;br /&gt;    slice_count = (slices_estimate[1] &gt; 0 ? slices_estimate[0] + 1 : slices_estimate[0])&lt;br /&gt;    slice_guess = self.size / slice_count&lt;br /&gt;    previous_slice_location = 0&lt;br /&gt;    (1..slice_count - 1).each do&lt;br /&gt;      |i|&lt;br /&gt;      slice_location = self.nearest_split(slice_guess * i, sclice_on)&lt;br /&gt;      out &lt;&lt; self.slice(previous_slice_location..slice_location)&lt;br /&gt;      previous_slice_location = slice_location + 1&lt;br /&gt;    end&lt;br /&gt;    out &lt;&lt; self.slice(previous_slice_location..self.size)&lt;br /&gt;    out&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def nearest_split(slice_start, slice_on)&lt;br /&gt;    left_scan_location  = (self.slice(0..slice_start).rindex(slice_on)).to_i&lt;br /&gt;    right_scan_location = (self.slice((slice_start+1)..self.size).index(slice_on)).to_i + slice_start&lt;br /&gt;    ((slice_start - left_scan_location) &lt; (right_scan_location - slice_start) ? left_scan_location : right_scan_location)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 21 Sep 2006 00:29:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2631</guid>
      <author>duncanbeevers (Duncan Beevers)</author>
    </item>
    <item>
      <title>chunk function</title>
      <link>http://snippets.dzone.com/posts/show/1126</link>
      <description>&lt;code&gt;&lt;br /&gt;    split: chunk: segment: func [  ; subdivide ?&lt;br /&gt;        {See: CLOS pg. 937. Not that mine works the same, but that was&lt;br /&gt;        the inspiration.}&lt;br /&gt;        series [series!]&lt;br /&gt;        size   [integer!] "The size of the chunks (last chunk may be shorter)"&lt;br /&gt;        /into  "split into a set number (size) of chunks (last chunk may be longer than others)."&lt;br /&gt;        /local ct cur-piece result&lt;br /&gt;    ][&lt;br /&gt;        ct: either into [size] [round/down divide length? series size]&lt;br /&gt;        if into [size: to-integer divide length? series size]&lt;br /&gt;        result: copy []&lt;br /&gt;        if zero? size [return result]&lt;br /&gt;        parse series [&lt;br /&gt;            ct [&lt;br /&gt;                copy cur-piece size skip (append/only result cur-piece) mark:&lt;br /&gt;            ]&lt;br /&gt;        ]&lt;br /&gt;        if any [into  not zero? remainder length? series size] [&lt;br /&gt;            cur-piece: copy mark&lt;br /&gt;            either into&lt;br /&gt;                [append last result cur-piece]&lt;br /&gt;                [append/only result cur-piece]&lt;br /&gt;        ]&lt;br /&gt;        result&lt;br /&gt;    ]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Jan 2006 06:20:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1126</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
  </channel>
</rss>
