Skip to content

Instantly share code, notes, and snippets.

@erica erica/nostalgia.swift
Last active Apr 20, 2016

Embed
What would you like to do?
/*
ericasadun.com
Sometimes letting go doesn't mean saying goodbye
*/
prefix operator ++ {}
// @discardableResult
prefix public func ++(inout x: Int) -> Int { x += 1; return x }
// @discardableResult
prefix public func ++(inout x: UInt) -> UInt { x += 1; return x }
postfix operator ++ {}
// @discardableResult
postfix public func ++(inout x: Int) -> Int { defer {x += 1}; return x }
// @discardableResult
postfix public func ++(inout x: UInt) -> UInt { defer {x += 1}; return x }
prefix operator -- {}
// @discardableResult
prefix public func --(inout x: Int) -> Int { x -= 1; return x }
// @discardableResult
prefix public func --(inout x: UInt) -> UInt { x -= 1; return x }
postfix operator -- {}
// @discardableResult
postfix public func --(inout x: Int) -> Int { defer {x -= 1}; return x }
// @discardableResult
postfix public func --(inout x: UInt) -> UInt { defer {x -= 1}; return x }
// Alternatively, deriving 1
// via Pilipenko Dima http://github.com/dimpiax
postfix operator +++ {}
postfix public func +++<T: IntegerArithmeticType>(inout x: T) -> T { defer { x += x/x }; return x }
prefix operator +++ {}
prefix public func +++<T: IntegerArithmeticType>(inout x: T) -> T { x += x/x; return x }
// CGFloat versions apparently need separate implementations
//prefix public func +++(inout x: CGFloat) -> CGFloat { defer { x += 1 }; return x }
//prefix public func +++(inout x: CGFloat) -> CGFloat { x += 1; return x }
// via "torquato"
postfix operator ++++ {}
postfix public func ++++<T: BidirectionalIndexType>(inout x: T) -> T { defer { x = x.successor() }; return x }
prefix operator ++++ {}
prefix public func ++++<T: BidirectionalIndexType>(inout x: T) -> T { x = x.successor(); return x }
postfix operator ---- {}
postfix public func ----<T: BidirectionalIndexType>(inout x: T) -> T { defer { x = x.predecessor() }; return x }
prefix operator ---- {}
prefix public func ----<T: BidirectionalIndexType>(inout x: T) -> T { x = x.predecessor(); return x }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.