Why Ruby inject method cannot sum up string lengths without initial value?

前端 未结 3 964
说谎
说谎 2020-12-18 06:39

Why the following code issues an error ?

[\'hello\',\'stack\',\'overflow\'].inject{|memo,s|memo+s.length}

TypeError: can\'t convert Fixnum into String
              


        
相关标签:
3条回答
  • 2020-12-18 07:01

    You have the answer in apidock :

    If you do not explicitly specify an initial value for memo, then uses the first element of collection is used as the initial value of memo.

    That is, without an initial value, you're trying to do 'hello' + 'stack'.length

    0 讨论(0)
  • 2020-12-18 07:21

    Without the initial value, inject uses the first element in the collection as the initial value.

    see ruby-doc.

    0 讨论(0)
  • 2020-12-18 07:24

    As the error message already tells you, the problem is that you have a TypeError. Just because Ruby is dynamically and implicitly typed doesn't mean that you don't have to think about types.

    The type of Enumerable#inject without an explicit accumulator (this is usually called reduce) is something like

    reduce :: [a] → (a → a → a) → a
    

    or in a more Rubyish notation I just made up

    Enumerable[A]#inject {|A, A| A } → A
    

    You will notice that all the types are the same. The element type of the Enumerable, the two argument types of the block, the return type of the block and the return type of the overall method.

    In your case, the types for the block just don't add up. The block gets passed two Strings and it is supposed to return a String. But you call the + method on the first argument (which is a String) with an argument that is an Integer. But String#+ doesn't take an Integer it only takes a String or more precisely something which can be converted to a String, i.e. something that responds to #to_str. That's why you get a TypeError for String#+.

    The type of Enumerable#inject with an explicit accumulator (this is usually called fold) is something like

    fold :: [b] → a → (a → b → a) → a
    

    or

    Enumerable[B]#inject(A) {|A, B| A } → A
    

    Here you see that the accumulator can have a different type than the element type of the collection. Which is precisely what you need.

    These two rules generally get you through all Enumerable#inject-related problems:

    1. the type of the accumulator and the return type of the block must be the same
    2. when not passing an explicit accumulator, the type of the accumulator is the same as the element type

    Rule #1 will most often bite you when you do something like

    acc[key] = value
    

    in your block, because assignments evaluate to the assigned value, not the receiver of the assignment. You'll have to replace this with

    acc.tap { acc[key] = value }
    

    In your particular case, the two solutions have already been mentioned. Either use an explicit accumulator

    ary.reduce(0){|acc, e| acc + e.length }
    

    or convert to integers first

    ary.map(&:length).reduce(:+)
    
    0 讨论(0)
提交回复
热议问题