-
Notifications
You must be signed in to change notification settings - Fork 317
Int
- Instance methods
- Class methods
#Instance methods ##Iteration
times <T> (function: () -> T)
times (function: () -> ())
Iterates
function
,self
times.
times <T> (function: (Int) -> T)
Iterates
function
, with anInt
index argument,self
times.
5.times { println("Hi") }
/* Prints → */
// → Hi
// → Hi
// → Hi
// → Hi
// → Hi
upTo (limit: Int, function: (Int) -> ())
Iterates
function
, passing in integer values fromself
up to and includinglimit
.
5.upTo(7, { println($0) })
/* Prints → */
// → 5
// → 6
// → 7
downTo (limit: Int, function: (Int) -> ())
Iterates
function
, passing in integer values fromself
down to and includinglimit
.
7.downTo(5, { println($0) })
/* Prints → */
// → 7
// → 6
// → 5
##Math
isEven () -> Bool
Returns
true
ifself
is even.
4.isEven()
// → true
isOdd () -> Bool
Returns
true
ifself
is odd.
3.isOdd()
// → true
clamp (range: Range<Int>) -> Int
clamp (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)
// → 2
abs () -> Int
Computes the absolute value of
self
.
(-2).abs()
// → 2
1.abs()
// → 1
gcd (n: Int) -> Int
Computes the greatest common divisor of
self
andn
.
6.gcd(3)
// → 3
lcm (n: Int) -> Int
Computes the least common multiple of
self
andn
.
3.lcm(4)
// → 12
isIn (range: Range<Int>, strict: Bool = false) -> Bool
If
strict
is 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
min
andmax
(inclusive).
Int.random(min: 5, max: 10)
// → 6