我想要的映射函数的 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):
???