-
-
Save pasberth/1175529 to your computer and use it in GitHub Desktop.
Python でこういうことをしたいっていう願望
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Python でもこんな感じのものを定義して | |
| # ["<a>" + e + "</a>" for e in r(['root1', ['branch0', 'branch1'], 'root2', ['branches'] ])] | |
| # って書きたい | |
| class Array | |
| def r &blk | |
| [].tap { |res| | |
| each { |e| | |
| if e.is_a? Array | |
| res << e.r(&blk) | |
| else | |
| res << blk.call(e) | |
| end | |
| } | |
| } | |
| end | |
| end | |
| if __FILE__ == $PROGRAM_NAME | |
| require "test/unit" | |
| class TestR < Test::Unit::TestCase | |
| def test_r | |
| assert_equal ['root1', ['branch0', 'branch1'], 'root2', ['branches'] ].r { |e| | |
| '<a>' + e + '</a>' | |
| }, ["<a>root1</a>", ["<a>branch0</a>", "<a>branch1</a>"], "<a>root2</a>", ["<a>branches</a>"]] | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment