Skip to content

Instantly share code, notes, and snippets.

@pauljohanneskraft
Last active July 4, 2016 00:20
Show Gist options
  • Save pauljohanneskraft/116be58cc625aa330720663a8e25bf1d to your computer and use it in GitHub Desktop.
Save pauljohanneskraft/116be58cc625aa330720663a8e25bf1d to your computer and use it in GitHub Desktop.
Make all in-place-operations out-of-place by using this function

Out of place

This function makes it possible to use an out-of-place operation on some object where only in-place variants exist. simply use it as following

func outOfPlaceVariant(_ element: T) -> T {
  outOfPlace(with: element) { $0.inPlaceVariant() }
}
func outOfPlace<T>(with element: T, _ f: (inout T) throws -> ()) rethrows -> T {
try f(&element)
return element
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment