-10

必要なマッピング関数の Ruby の例:

["qwe", ["asd", "zxc"]].map{ |i| [*i][0] } => ["qwe", "asd"]

def f array_or_string
  [*array_or_string].first
end

["qwe", ["asd", "zxc"]].map &method(:f)    => ["qwe", "asd"]

f ["qwe", "zxc"]                           => "qwe"
f "asd"                                    => "asd"

文字列は Python で反復可能であるため、この言語設計とどのように戦って同じ結果をエレガントに達成できませんか?

def f(array_or_string):
    ???
4

2 に答える 2