jweinst1
3/4/2016 - 9:42 PM

associative appending operator in swift,

associative appending operator in swift,

infix operator <~ { associativity left }

func <~<elem> (var list:[elem], elem:elem) -> [elem] {
    list.append(elem)
    return list
}

[1, 2, 3] <~ 4
//[1, 2, 3, 4]
[1, 2, 3] <~ [5]
//[1, 2, 3, [5]]

//due to associativity being left, the operands are all push to the list on the left
[1] <~ [2] <~ [3]
//[1, [2], [3]]

[1] <~ 2 <~ 3 <~ 5
//[1, 2, 3, 4]