Tag: 等价物

Swift相当于Ruby的“each_cons”

ruby Ruby有each_cons可以像这样使用 class Pair def initialize(left, right) @left = left @right = right end end votes = [“a”, “b”, “c”, “d”] pairs = votes.each_cons(2).map { |vote| Pair.new(*vote) } p pairs # [#, #, #] 迅速 swift中的代码相同,但没有each_cons函数 struct Pair { let left: String let right: String } let votes = [“a”, “b”, “c”, “d”] var pairs […]