Skip to content
This repository was archived by the owner on Mar 29, 2018. It is now read-only.
pNre edited this page Jun 24, 2014 · 5 revisions

Contents

#Instance methods ##Iteration

times

  • times <T> (function: () -> T)
  • times (function: () -> ())

Iterates function , self times.

  • times <T> (function: (Int) -> T)

Iterates function , with an Int index argument, self times.

Example

5.times { println("Hi") } /* Prints → */ // → Hi // → Hi // → Hi // → Hi // → Hi

upTo

  • upTo (limit: Int, function: (Int) -> ())

Iterates function, passing in integer values from self up to and including limit.

Example

5.upTo(7, { println($0) }) /* Prints → */ // → 5 // → 6 // → 7

downTo

  • downTo (limit: Int, function: (Int) -> ())

Iterates function, passing in integer values from self down to and including limit.

Example

7.downTo(5, { println($0) }) /* Prints → */ // → 7 // → 6 // → 5

##Math

isEven

  • isEven () -> Bool

Returns true if self is even.

Example

4.isEven() // → true

isOdd

  • isOdd () -> Bool

Returns true if self is odd.

Example

3.isOdd() // → true

clamp

  • clamp (range: Range<Int>) -> Int
  • clamp (min: Int, max: Int) -> Int

Computes the value of self clamped to a range defined by range (or min...max).

Example

5.clamp(0...4) // → 4 1.clamp(2...4) // → 2

abs

  • abs () -> Int

Computes the absolute value of self.

Example

(-2).abs() // → 2 1.abs() // → 1

gcd

  • gcd (n: Int) -> Int

Computes the greatest common divisor of self and n.

Example

6.gcd(3) // → 3

lcm

  • lcm (n: Int) -> Int

Computes the least common multiple of self and n.

Example

3.lcm(4) // → 12

isIn

  • isIn (range: Range<Int>, strict: Bool = false) -> Bool

If strict is true checks that range.startIndex < self < range.endIndex - 1, otherwise, range.startIndex <= self <= range.endIndex - 1.

Example

5.isIn(4..10) // → true

##Conversion

digits

  • digits () -> Array<Int>

Returns an array of integers where each element is a digit of self.

Example

512.digits() // → [5, 1, 2]

#Class methods

random

  • random(min: Int = 0, max: Int) -> Int

Returns a random integer between min and max (inclusive).

Example

Int.random(min: 5, max: 10) // → 6

Clone this wiki locally