<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: slice code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 02:10:34 GMT</pubDate>
    <description>DZone Snippets: slice code</description>
    <item>
      <title>Rubylike slice for strings and lists</title>
      <link>http://snippets.dzone.com/posts/show/5055</link>
      <description>(slice "hello" 2 4) =&gt; "ll"&lt;br /&gt;Comparable to: "hello"[2...4]&lt;br /&gt;&lt;br /&gt;(slice "hello" 2 -1) =&gt; "ll"&lt;br /&gt;Comparable to: "hello"[2...-1]&lt;br /&gt;&lt;br /&gt;(slice "hello" '(2 2)) =&gt; "ll"&lt;br /&gt;Comparable to: "hello"[2,2]&lt;br /&gt;&lt;br /&gt;(slice "hello" 1) =&gt; "ello"&lt;br /&gt;Comparable to: "hello"[2..-1]&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;(def slice (str x (o y))&lt;br /&gt;  (if (atom x)&lt;br /&gt;    (if &lt;br /&gt;      ; (slice "hello" 1) =&gt; "ello"&lt;br /&gt;      (no y) &lt;br /&gt;        (subseq str x)&lt;br /&gt;      ; (slice "hello" 2 -1) =&gt; "ll"&lt;br /&gt;      (&lt; y 0) &lt;br /&gt;        (subseq str x (+ (len str) y))&lt;br /&gt;      ; (slice "hello" 2 4) =&gt; "ll"&lt;br /&gt;      (subseq str x y))&lt;br /&gt;    ; (slice "hello" '(2 2)) =&gt; "ll"&lt;br /&gt;    (subseq str (car x) (+ (car x) (cadr x)))))&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 30 Jan 2008 13:26:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5055</guid>
      <author>mdemare (Michiel de Mare)</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>slice function</title>
      <link>http://snippets.dzone.com/posts/show/1132</link>
      <description>&lt;code&gt;&lt;br /&gt;slice: func [series start len] [copy/part at series start len]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Jan 2006 06:29:24 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1132</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
  </channel>
</rss>
