Skip to content

Instantly share code, notes, and snippets.

@sjoerdvisscher
Created January 14, 2015 10:04
Show Gist options
  • Select an option

  • Save sjoerdvisscher/0a8a9c709096868e09a0 to your computer and use it in GitHub Desktop.

Select an option

Save sjoerdvisscher/0a8a9c709096868e09a0 to your computer and use it in GitHub Desktop.
zipper-like mutation in Swift
struct Rect {
var width = 1
var height = 1
mutating func scaleBy(value: Int) {
width *= value
height *= value
}
}
var rects: [Rect] = [Rect(width: 5,height: 6), Rect(width: 2, height: 3)]
let rect = rects[1]
rects[1].scaleBy(3)
rects // [Rect(5, 6), Rect(6, 9)]
rect // still Rect(2, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment