ruby
Ruby has each_cons which can be used as
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 # [#<Pair @left="a", @right="b">, #<Pair @left="b", @right="c">, #<Pair @left="c", @right="d">]
Swift
The same code in swift, but without each_cons function
struct Pair { let left: String let right: String } let votes = ["a", "b", "c", "d"] var pairs = [Pair]() for i in 1..<votes.count { let left = votes[i-1] let right = votes[i] pairs.append(Pair(left: left, right: right)) } print(pairs) // [Pair(left: "a", right: "b"), Pair(left: "b", right: "c"), Pair(left: "c", right: "d")]
How can this quick code be shorter or simpler?
source share