Permalink
Commits on Mar 29, 2012
  1. Fix specs to work with Ruby 1.9.3+

    The ability to refer to constants in an included module was actually a bug
    in Ruby 1.9.2, so the specs were failing on later versions where this bug has
    been fixed.
    
    The solution is to define the example blocks inside the module itself, since
    that's the only place where the unqualified constants should be visible.
    
    rspec/rspec-core#506
    http://bugs.ruby-lang.org/issues/5657
    https://twitter.com/dchelimsky/status/185335729738616833
    committed Mar 29, 2012
Commits on Oct 28, 2011
  1. Implement FizzBuzz

    def fizzbuzz(m)
      (1..m).map { |n|
        if (n % 15).zero?
          'FizzBuzz'
        elsif (n % 3).zero?
          'Fizz'
        elsif (n % 5).zero?
          'Buzz'
        else
          n.to_s
        end
      }
    end
    committed Oct 22, 2011
  2. Implement TO_DIGITS for natural numbers

    def to_digits(n)
      (n <= 9 ? [] : to_digits(n / 10)).push(n % 10)
    end
    committed Oct 22, 2011
  3. Extract MAP from INCREMENT_ALL and DOUBLE_ALL

    def map(k, &block)
      fold(k, []) { |l, x| l.unshift(block.call(x)) }
    end
    committed Oct 14, 2011
  4. Implement DOUBLE_ALL for lists

    def double_all(k)
      fold(k, []) { |l, n| l.unshift(n * 2) }
    end
    committed Oct 19, 2011
  5. Implement INCREMENT_ALL for lists

    def increment_all(k)
      fold(k, []) { |l, n| l.unshift(n + 1) }
    end
    committed Oct 14, 2011
  6. Extract FOLD from CONCAT and REVERSE

    def fold(l, x, &block)
      if l.empty?
        x
      else
        block.call(fold(l[1..-1], x, &block), l.first)
      end
    end
    committed Oct 17, 2011
  7. Implement REVERSE for lists

    def reverse(l)
      if l.empty?
        []
      else
        reverse(l[1..-1]).push(l.first)
      end
    end
    committed Oct 19, 2011
  8. Implement PUSH for lists

    def push(l, x)
      l.concat([].unshift(x))
    end
    committed Oct 13, 2011
  9. Implement CONCAT for lists

    def concat(k, l)
      if k.empty?
        l
      else
        concat(k[1..-1], l).unshift(k.first)
      end
    end
    committed Oct 13, 2011
  10. Extract INJECT from SUM and PRODUCT

    def inject(l, x, &block)
      if l.empty?
        x
      else
        inject(l[1..-1], block.call(x, l.first), &block)
      end
    end
    committed Oct 13, 2011
  11. Implement PRODUCT for lists

    def product(l)
      if l.empty?
        1
      else
        l.first * product(l[1..-1])
      end
    end
    committed Oct 13, 2011
  12. Implement SUM for lists

    def sum(l)
      if l.empty?
        0
      else
        l.first + sum(l[1..-1])
      end
    end
    committed Oct 13, 2011
  13. Implement RANGE for lists

    def range(m, n)
      if m <= n
        range(m + 1, n).unshift(m)
      else
        []
      end
    end
    committed Oct 13, 2011
  14. Implement REST for lists

    committed Oct 13, 2011
  15. Implement FIRST for lists

    committed Oct 13, 2011
  16. Implement IS_EMPTY for lists

    committed Oct 13, 2011
  17. Implement pairs

    committed Oct 13, 2011
  18. Implement MOD for natural numbers

    def mod(m, n)
      if n <= m
        mod(m - n, n)
      else
        m
      end
    end
    committed Oct 14, 2011
  19. Implement DIV for natural numbers

    def div(m, n)
      if n <= m
        div(m - n, n) + 1
      else
        0
      end
    end
    committed Oct 22, 2011
  20. Define Y and Z combinators

    committed Oct 13, 2011
  21. Implement broken, cheating FACTORIAL for natural numbers

    def factorial(n)
      if n.zero?
        1
      else
        n * factorial(n - 1)
      end
    end
    committed Oct 13, 2011
  22. Implement IS_LESS_OR_EQUAL and IS_EQUAL for natural numbers

    def less_or_equal?(m, n)
      (m - n).zero? # 0 is the smallest number
    end
    
    def equal?(m, n)
      m <= n && n <= m
    end
    committed Oct 12, 2011
  23. Implement NOT, AND and OR for booleans

    def not(b)
      if b
        false
      else
        true
      end
    end
    
    def and(a, b)
      if a
        if b
          true
        else
          false
        end
      else
        false
      end
    end
    
    def or(a, b)
      if a
        true
      else
        if b
          true
        else
          false
        end
      end
    end
    committed Oct 17, 2011
  24. Implement booleans

    committed Oct 12, 2011
  25. Implement natural numbers

    committed Oct 12, 2011