A tiny collection of (more or less) useful Swift operators.
dependencies: [
// Dependencies declare other packages that this package depends on.
...
.package(url: "https://github.com/Pyroh/SmoothOperators.git", .upToNextMajor(from: "0.4.0")),
...
],
- Adds back prefixed and postfixed
++
and--
operators. - Introduces postfixed
+
and-
operators that respectively returnvalue + 1
andvalue - 1
. - Adds an exponent operator
**
.
Features a variant for ==
, !=
, >
, <
, >=
, <=
respectively ==?
, !=?
, >?
, <?
, >=?
, <=?
that return an optional value instead of a boolean one.
If the outcome should have been true
the left value is returned in a form of an optional. If the outcome should have been false
nil
is returned.
5 ==? 5 // Optional(5)
5 ==? 6 // nil
5 !=? 6 // Optional(5)
5 !=? 5 // nil
5 >? 4 // Optional(5)
5 >? 6 // nil
5 >=? 5 // Optional(5)
5 >=? 6 // nil
5 <? 6 // Optional(5)
5 <? 4 // nil
5 <=? 5 // Optional(5)
5 <=? 4 // nil
This infix operator take take the left variable, transform it using the closure at its right and put the resulting value back in the left variable.
var a: Int = 42
a <- { $0 - 2 } // a == 40
func divideByTwo(_ value: Int) -> Int {
value / 2
}
a <- divideByTwo // a == 20
This prefixed operator returns false
if the value its attached to is nil
. true
otherwise.
let a: Int? = 5
let b: Int? = nil
!!a // true
!!a // false
If the receiver is of type Optional<Bool>
the wrapped value is returned or false
if the receiver is nil
Is the same as the nil-coalescing operator but for floating point values. These values can be NaN.
This operator returns the value if it's not NaN otherwise it returns the default value.
let a: Double? = 42
let b: Double? = nil
let c: Double = 42
let d: Double = .nan
let e: Double? = .nan
a ?? 0 // 42.0
b ?? 0 // 0.0
c ?? 0 // 42.0
d ?? 0 // 0.0
(e ?? 0) ?? 1 // 1.0
Is the same as the nil-coalescing operator but for collections. This operator returns the collection if it's not empty otherwise it returns the default collection.
Assign the right hand side value to the left hand side optional if its nil.
a ?= 42
is equivalent to a = a ?? 42
for any var a: Int?
.
See LICENSE.