<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: counting code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 11:57:54 GMT</pubDate>
    <description>DZone Snippets: counting code</description>
    <item>
      <title>Simple Haskell script for word counting</title>
      <link>http://snippets.dzone.com/posts/show/4263</link>
      <description>This is just a simple piece of code I put together to play with some Haskell when I realised I've not been writing nearly enough of the stuff. &lt;br /&gt;&lt;br /&gt;It reads text from stdin and prints the words it finds together with how many times each one occurred.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module Main&lt;br /&gt;where&lt;br /&gt;&lt;br /&gt;import List&lt;br /&gt;import Control.Arrow&lt;br /&gt;&lt;br /&gt;type Comparator a = (a -&gt; a -&gt; Ordering)&lt;br /&gt;&lt;br /&gt;ascending :: (Ord a) =&gt; (b -&gt; a) -&gt; Comparator b&lt;br /&gt;ascending f x y = compare (f x) (f y)&lt;br /&gt;&lt;br /&gt;descending :: (Ord a) =&gt; (b -&gt; a) -&gt; Comparator b&lt;br /&gt;descending = flip . ascending&lt;br /&gt;&lt;br /&gt;secondary :: Comparator a -&gt; Comparator a -&gt; Comparator a&lt;br /&gt;secondary f g x y = case f x y of {&lt;br /&gt;                    EQ -&gt; g x y;&lt;br /&gt;                    z  -&gt; z; }&lt;br /&gt;&lt;br /&gt;-- Returns a list of unique elements together with their frequency. Listed in decreasing order of frequency, followed by&lt;br /&gt;increasing order of the elements.&lt;br /&gt;count :: (Ord a) =&gt; [a] -&gt; [(a, Int)]&lt;br /&gt;count = map (head &amp;&amp;&amp; length) . sortBy (descending length `secondary` ascending head) . group . sort&lt;br /&gt;&lt;br /&gt;main :: IO ()&lt;br /&gt;main = interact $ unlines . map (\(x, y) -&gt; (take 20 $ x ++ repeat ' ')  ++ " : " ++ show y) . count . words&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 13:52:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4263</guid>
      <author>DRMacIver (David R. MacIver)</author>
    </item>
    <item>
      <title>Odd numbers up to 1..100 in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/2568</link>
      <description>// 10 different ways to display odd numbers 1 through 100 in Ruby&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;100.times do |i|&lt;br /&gt;  next if i % 2 == 0&lt;br /&gt;  puts i&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;50.times { |i| p i*2+1 }&lt;br /&gt;(1..100).step(2) { |i| puts i}&lt;br /&gt;1.upto(100) { |i| puts i unless i[0].zero? }&lt;br /&gt;puts Array.new(50) { |i| i * 2 + 1 }&lt;br /&gt;# a self referential recursive lambda :-)&lt;br /&gt;lambda { me = lambda { |x| p x; me.call(x+2) if x &lt; 99 } ; me.call(1) }.call&lt;br /&gt;&lt;br /&gt;# same thing, but passing the lambda around&lt;br /&gt;rec = lambda { |v,l| p v; l.call(v+2,l) if v &lt; 99 }&lt;br /&gt;rec.call(-1,rec)&lt;br /&gt;&lt;br /&gt;# same recursive algorithm, but in method form&lt;br /&gt;def odds(x=1)&lt;br /&gt;  p x&lt;br /&gt;  odds(x+2) if x &lt; 99&lt;br /&gt;end&lt;br /&gt;odds&lt;br /&gt;&lt;br /&gt;class Integer&lt;br /&gt;  def odd?&lt;br /&gt;    self[0].nonzero?&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;100.times { |i| puts i if i.odd? }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;require 'delegate'&lt;br /&gt;class OddNum &lt; DelegateClass(Fixnum)&lt;br /&gt;  def initialize(value)&lt;br /&gt;    value |= 1  # force it odd&lt;br /&gt;    super(value)&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def succ&lt;br /&gt;    # note that the delegated succ gets called when we call super&lt;br /&gt;    # and the constructor forces it (up) to the next odd number&lt;br /&gt;    OddNum.new(super)&lt;br /&gt;    &lt;br /&gt;    # or&lt;br /&gt;    # OddNum.new(self + 1)  # still using the constructor's force to odd&lt;br /&gt;    # or &lt;br /&gt;    # OddNum.new(self + 2)  # being odd all on our own&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;(OddNum.new(1)..100).each { |i| puts i }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 08 Sep 2006 18:20:03 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2568</guid>
      <author>pth (Patrick Hurley)</author>
    </item>
  </channel>
</rss>
