我在學習Ruby時解決了代碼的一些問題。我解決了這個one更C++ - 雜交的方式(我還是新來的Ruby)在Ruby中,塊和地圖的組合是什麼?
然後我就檢查基於upvotes最好的解決辦法,那就是:
def unique_in_order(iterable)
(iterable.is_a?(String) ? iterable.chars : iterable).chunk { |x| x }.map(&:first)
end
我不知道(&:first)
是什麼。我知道什麼地圖做,看起來當我跑:
[1, 2, 3, 4, 4, 5].chunk {|x| x}.map(&:first)
重複的元素被刪除。
如果問題只是關於&:第一位,請參閱ht tp://stackoverflow.com/questions/1217088/what-does-mapname-mean-in-ruby –