Skip to content

Instantly share code, notes, and snippets.

@Teddy95
Last active Oct 17, 2016
Embed
What would you like to do?
Power-, Root-, Squareroot-, Increase- and Decrease-Operators for Swift
/**
* Power operator
*/
precedencegroup powerPrecedence {
associativity: left
higherThan: MultiplicationPrecedence
}
infix operator ^^ : powerPrecedence
func ^^ (base: Double, exp: Double) -> Double {
return pow(base, exp)
}
/**
* Root operator
*/
precedencegroup rootPrecedence {
associativity: left
higherThan: MultiplicationPrecedence
}
infix operator : rootPrecedence
func (lvl: Int, square: Double) -> Double {
return square ^^ (1 / Double(lvl))
}
/**
* Squareroot operator
*/
prefix operator
prefix func (square: Double) -> Double {
return sqrt(square)
}
/**
* Increase operator (++n)
*/
prefix operator ++
prefix func ++ (_ n: inout Int) -> Int {
n += 1
return n
}
/**
* Decrease operator (--n)
*/
prefix operator --
prefix func -- (_ n: inout Int) -> Int {
n -= 1
return n
}
/**
* Increase operator (n++)
*/
postfix operator ++
postfix func ++ (_ n: inout Int) -> Int {
let callback: Int = n
n += 1
return callback
}
/**
* Decrease operator (n--)
*/
postfix operator --
postfix func -- (_ n: inout Int) -> Int {
let callback: Int = n
n -= 1
return callback
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment