Ad
  • Custom User Avatar

    @ColbyDauph is correct about the purpose of the Kata. It is intended to make new Rubyists aware of existing features, which means that this solution is "really solving it". However, I'm not a big fan of Kata being used this way and would much rather have problems which challenged the user to create his or her own solution.

  • Custom User Avatar

    As far as I can tell there is no way to prevent a user from submitting a solution that uses #inject. Maybe that means this problem is not suitable for a kata.

  • Custom User Avatar

    Some current solutions return 0 for [0].accumulate(nil, :+),

    which differs from [0].inject(nil, :+) which raises an error NoMethodError: undefined method '+' for nil:NilClass

    I'd suggest adding a test case similar to

    Test.expect_error { [0].accumulate(nil, :+) }
    
  • Custom User Avatar

    Please do explain what s is and where it comes from.

  • Custom User Avatar

    OK, that is acceptable. Thanks for explaining.

    Just so you know where my point of view comes from, here are some practical cases where the ability to use both attr_lazy :even and attr_lazy :even? to memoize results would be useful.

    # The memoized result of `even` is a lazy enumerator
    attr_lazy :even do
      @numbers.lazy.map { |n| fib(n) }.select(&:even?)  # where fib(n) returns the Fibonacci number F(n)
    end
    
    attr_lazy :even? do
      # Enumeratar::Lazy#any? is a potentially expensive operation since
      # it forces iteration until a value is yielded. If there is a need
      # to invoke this multiple times it is safest to memoize the result.
      even.any?
    end
    
    attr_lazy :foos do
      # Build some data relation, e.g. an ActiveRecord::Relation instance.
    end
    
    attr_lazy :foos? do
      foos.any?  # Executes a query against the data store each time it is invoked. Memoize if invoked more than once.
    end
    
  • Custom User Avatar

    According to the test cases, you expect that when @even is set then invokations of #even? respect this as part of the memoization. This seems inconsistent to me because in Ruby even and even? are two distinct identifiers. Take for example a class that has both attr_lazy :even and attr_lazy :even?. In this case the test case seem to apply to two different lazy attributes.