- Notifications
You must be signed in to change notification settings - Fork 313
Int
- Instance methods
- Class methods
#Instance methods ##Iteration
times <T> (function: () -> T)times (function: () -> ())
Iterates
function,selftimes.
times <T> (function: (Int) -> T)
Iterates
function, with anIntindex argument,selftimes.
5.times { println("Hi") } /* Prints → */ // → Hi // → Hi // → Hi // → Hi // → HiupTo (limit: Int, function: (Int) -> ())
Iterates
function, passing in integer values fromselfup to and includinglimit.
5.upTo(7, { println($0) }) /* Prints → */ // → 5 // → 6 // → 7downTo (limit: Int, function: (Int) -> ())
Iterates
function, passing in integer values fromselfdown to and includinglimit.
7.downTo(5, { println($0) }) /* Prints → */ // → 7 // → 6 // → 5##Math
isEven () -> Bool
Returns
trueifselfis even.
4.isEven() // → trueisOdd () -> Bool
Returns
trueifselfis odd.
3.isOdd() // → trueclamp (range: Range<Int>) -> Intclamp (min: Int, max: Int) -> Int
Computes the value of self clamped to a range defined by
range(ormin...max).
5.clamp(0...4) // → 4 1.clamp(2...4) // → 2abs () -> Int
Computes the absolute value of
self.
(-2).abs() // → 2 1.abs() // → 1gcd (n: Int) -> Int
Computes the greatest common divisor of
selfandn.
6.gcd(3) // → 3lcm (n: Int) -> Int
Computes the least common multiple of
selfandn.
3.lcm(4) // → 12isIn (range: Range<Int>, strict: Bool = false) -> Bool
If
strictis true checks thatrange.startIndex < self < range.endIndex - 1, otherwise,range.startIndex <= self <= range.endIndex - 1.
5.isIn(4..10) // → true##Conversion
digits () -> Array<Int>
Returns an array of integers where each element is a digit of
self.
512.digits() // → [5, 1, 2]#Class methods
random(min: Int = 0, max: Int) -> Int
Returns a random integer between
minandmax(inclusive).
Int.random(min: 5, max: 10) // → 6