<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users535</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/535" rel="self"/>
  <title>jes5199</title>
  <updated>Sun Apr 06 00:19:24 -0700 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor4714</id>
    <published>2008-04-06T00:19:24-07:00</published>
    <title>[Ruby] On Euler 14</title>
    <content type="html">&lt;p&gt;slightly faster, caches more data&lt;/p&gt;

&lt;pre&gt;require 'benchmark' 

def operation(n)
  return n%2 == 0 ? n/2 : 3*n+1 
end

def recursive(hash, n)
  hash[n] or hash[n] = 1 + recursive(hash, operation(n) )
end

chains = {} 
max_chain_size = 0 
max_chain_start = 1 

Benchmark.bm do |x| 
  x.report do 
    chains[1] = 1
    2.upto(1000000) do |i| 
      chain_size = recursive(chains, i)
      
      if chain_size &amp;gt; max_chain_size 
        max_chain_size = chain_size 
        max_chain_start = i 
      end 
    end 
  end 
  puts &amp;quot;(#{max_chain_start}, #{max_chain_size})&amp;quot; 
end 
&lt;/pre&gt;</content>
    <author>
      <name>jes5199</name>
      <email>jes5199@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/276-euler-14/refactors/4714" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor4211</id>
    <published>2008-03-21T00:20:02-07:00</published>
    <title>[Ruby] On Recursive Hash#fetch</title>
    <content type="html">&lt;p&gt;well, since we're already reopening standard classes, I thought it might make sense to move the Array logic into class Array. Now we have simple recursion of methods, instead of having to make private functions.&lt;/p&gt;

&lt;pre&gt;class Hash
  def fetch_all(key)
    return [] if not self.has_key?(key)
    value = self.fetch(key)
    [ value ] + [ value ].fetch_all(key)
  end
end

class Array
  def fetch_all(key)
    self.find_all{|x| x.respond_to? :fetch_all} \
      .map{|x| x.fetch_all(key) } \
      .inject( [] ){|a,b| a + b }
  end
end
&lt;/pre&gt;</content>
    <author>
      <name>jes5199</name>
      <email>jes5199@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/256-recursive-hash-fetch/refactors/4211" rel="alternate"/>
  </entry>
</feed>

