Boost performance of List#car, #cdr, #cadr, #cdar, etc etc

In a little micro-benchmark I tried, this gave a 9x performance boost to `#caddr`. `#car`, `#cdr`, `#cadr`, `#cdar` (and so on) should all show similar speed-ups.

I'm opening a PR so others can comment on code style before the changes are merged in.
The resulting method will be faster.


On Wed, Jul 16, 2014 at 10:13 PM, Kurtis Rainbolt-Greene <
notifications@github.com> wrote:

> In lib/hamster/list.rb:
>
> > -        case char
> > -        when "a" then memo.head
> > -        when "d" then memo.tail
> > -        end
> > +      if name.to_s.match(CADR)
> > +        # Perform compositions of car and cdr operations. Their names consist of a 'c',
> > +        # followed by at least one 'a' or 'd', and finally an 'r'. The series of 'a's and
> > +        # 'd's in the method name identify the series of car and cdr operations performed.
> > +        # The order in which the 'a's and 'd's appear is the inverse of the order in which
> > +        # the corresponding operations are performed.
> > +        code = "def #{name}; self."
> > +        code << Regexp.last_match[1].reverse.chars.map do |char|
> > +          {'a' => 'head', 'd' => 'tail'}[char]
> > +        end.join('.')
> > +        code << '; end'
> > +        List.class_eval(code)
>
> Why code evaluation instead of using define_method?
>
> —
> Reply to this email directly or view it on GitHub
> <https://github.com/hamstergem/hamster/pull/111/files#r15024274>.
>