Forum: Ruby-dev [ruby-trunk - Feature #5550][Open] Hash#depth, Hash#flat_length for recursive hashes

Posted by Tsuyoshi Sawada (Guest)
on 2011-11-02 19:11
(Received via mailing list)
Issue #5550 has been reported by Tsuyoshi Sawada.

----------------------------------------
Feature #5550: Hash#depth, Hash#flat_length for recursive hashes
http://redmine.ruby-lang.org/issues/5550

Author: Tsuyoshi Sawada
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


I often have a hash whose value is recursively a hash, which may look 
like the following:

    {"Japan" =>
        {"Hokkaido" => "Sapporo", ...},
        {"Honhuu" =>
            {"Aomori" => "Hirosaki", ...},
            {"Akita" => ...},
            ...
        },
        {"Shikoku" => ...},
        ...
    }

In these cases, it will be convenient if there is a way to know the 
(maximum) depth of he original hash, and the numbers of all the 
"terminal nodes". I would like to propose two methods Hash#depth and 
Hash#flat_length, whose Ruby implementation can be as follows:

    class Hash
  def depth
    1 + (values.map{|v| Hash === v ? v.depth : 1}.max)
  end
  def flat_length
    values.inject(0){|sum, v| sum + (Hash === v ? v.flat_length : 1)}
  end
    end
Posted by Yukihiro Matsumoto (Guest)
on 2011-11-03 11:57
(Received via mailing list)
Issue #5550 has been updated by Yukihiro Matsumoto.

Status changed from Open to Feedback

Hashの本質はkey-valueのマッピングなので、valueが再帰的にHashであることを想定した(再帰的なHashでなければ役に立たない)メソッドを追加することには抵抗があります。
わずか6行のmonkey patchingを避けるためにすべてのRubyに追加すべきメソッドですか?
----------------------------------------
Feature #5550: Hash#depth, Hash#flat_length for recursive hashes
http://redmine.ruby-lang.org/issues/5550

Author: Tsuyoshi Sawada
Status: Feedback
Priority: Normal
Assignee:
Category:
Target version:


I often have a hash whose value is recursively a hash, which may look 
like the following:

    {"Japan" =>
        {"Hokkaido" => "Sapporo", ...},
        {"Honhuu" =>
            {"Aomori" => "Hirosaki", ...},
            {"Akita" => ...},
            ...
        },
        {"Shikoku" => ...},
        ...
    }

In these cases, it will be convenient if there is a way to know the 
(maximum) depth of he original hash, and the numbers of all the 
"terminal nodes". I would like to propose two methods Hash#depth and 
Hash#flat_length, whose Ruby implementation can be as follows:

    class Hash
  def depth
    1 + (values.map{|v| Hash === v ? v.depth : 1}.max)
  end
  def flat_length
    values.inject(0){|sum, v| sum + (Hash === v ? v.flat_length : 1)}
  end
    end
Posted by Alexey Muranov (alexey_m)
on 2011-11-05 16:46
(Received via mailing list)
Issue #5550 has been updated by Alexey Muranov.


Excuse me, can you be more precise with your example please? Ruby does 
not accept it (after removing the dots "...")?  Are you talking about 
nested hashes?  How about creating a class Tree that would inherit from 
Hash and define additional methods there?
----------------------------------------
Feature #5550: Hash#depth, Hash#flat_length for recursive hashes
http://redmine.ruby-lang.org/issues/5550

Author: Tsuyoshi Sawada
Status: Feedback
Priority: Normal
Assignee:
Category:
Target version:


I often have a hash whose value is recursively a hash, which may look 
like the following:

    {"Japan" =>
        {"Hokkaido" => "Sapporo", ...},
        {"Honhuu" =>
            {"Aomori" => "Hirosaki", ...},
            {"Akita" => ...},
            ...
        },
        {"Shikoku" => ...},
        ...
    }

In these cases, it will be convenient if there is a way to know the 
(maximum) depth of he original hash, and the numbers of all the 
"terminal nodes". I would like to propose two methods Hash#depth and 
Hash#flat_length, whose Ruby implementation can be as follows:

    class Hash
  def depth
    1 + (values.map{|v| Hash === v ? v.depth : 1}.max)
  end
  def flat_length
    values.inject(0){|sum, v| sum + (Hash === v ? v.flat_length : 1)}
  end
    end
Posted by Thomas Sawyer (7rans)
on 2011-11-06 09:35
(Received via mailing list)
Issue #5550 has been updated by Thomas Sawyer.


I take it you meant `nested hash`. I think your methods will infinite 
loop on recursive hash --and that needs to be considered.

I understand #depth, Array might use such a method too. But 
#flat_length, I don't quite get what is being counted.

----------------------------------------
Feature #5550: Hash#depth, Hash#flat_length for recursive hashes
http://redmine.ruby-lang.org/issues/5550

Author: Tsuyoshi Sawada
Status: Feedback
Priority: Normal
Assignee:
Category:
Target version:


I often have a hash whose value is recursively a hash, which may look 
like the following:

    {"Japan" =>
        {"Hokkaido" => "Sapporo", ...},
        {"Honhuu" =>
            {"Aomori" => "Hirosaki", ...},
            {"Akita" => ...},
            ...
        },
        {"Shikoku" => ...},
        ...
    }

In these cases, it will be convenient if there is a way to know the 
(maximum) depth of he original hash, and the numbers of all the 
"terminal nodes". I would like to propose two methods Hash#depth and 
Hash#flat_length, whose Ruby implementation can be as follows:

    class Hash
  def depth
    1 + (values.map{|v| Hash === v ? v.depth : 1}.max)
  end
  def flat_length
    values.inject(0){|sum, v| sum + (Hash === v ? v.flat_length : 1)}
  end
    end
Posted by mame (Yusuke Endoh) (Guest)
on 2012-11-20 13:01
(Received via mailing list)
Issue #5550 has been updated by mame (Yusuke Endoh).

Status changed from Feedback to Rejected

No feedback, looks hopeless to me.  Closing.

--
Yusuke Endoh <mame@tsg.ne.jp>
----------------------------------------
Feature #5550: Hash#depth, Hash#flat_length for recursive hashes
https://bugs.ruby-lang.org/issues/5550#change-33202

Author: sawa (Tsuyoshi Sawada)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:


I often have a hash whose value is recursively a hash, which may look 
like the following:

    {"Japan" =>
        {"Hokkaido" => "Sapporo", ...},
        {"Honhuu" =>
            {"Aomori" => "Hirosaki", ...},
            {"Akita" => ...},
            ...
        },
        {"Shikoku" => ...},
        ...
    }

In these cases, it will be convenient if there is a way to know the 
(maximum) depth of he original hash, and the numbers of all the 
"terminal nodes". I would like to propose two methods Hash#depth and 
Hash#flat_length, whose Ruby implementation can be as follows:

    class Hash
  def depth
    1 + (values.map{|v| Hash === v ? v.depth : 1}.max)
  end
  def flat_length
    values.inject(0){|sum, v| sum + (Hash === v ? v.flat_length : 1)}
  end
    end
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.