Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Adding functions as a protocol NumeralExtension on Int and Double

License

Notifications You must be signed in to change notification settings

seivan-archives/NumeralExtension

Repository files navigation

NumeralExtension

Overview

Extending ``IntwithNumericExtension``` protocol Adding random, clamping and etc.

Usage

100.randomFromZero // 52
-25.randomFromZero // -18

200.isEven // true
199.isEven // false

Double.random(min: -10, max: 100) // 44
Int.random(min: -100, max: 100) // -99

(-10).randomBetween(100) // 9
100.randomBetween(-200) // -67


(-230).clamp(min: 0, max: 100) // 0
230.clamp(min: 0, max: 100) // 100
10.clamp(min: 50, max: 100) // 50

//prints 1 to 20
var range = 20.times() { n in
  println(n)
}
//range.startIndex == 1
//range.endIndex == 20

//prints -10 to 5
range = (-10).upto(5) { n in
  println(n)
}
//range.startIndex == -10
//range.endIndex == 5

//prints 10 to -25
range = 10.downto(-25) { n in
  println(n)
}
//range.startIndex == 10
//range.endIndex == -25

API

typealias IndexHandler = (number:Int) -> Void

protocol NumeralExtension {
  var randomFromZero:Self { get }
  var isEven:Bool { get }
  class func random(#min:Int, max:Int) -> Self
  func clamp(#min:Self, max:Self) -> Self
  func times(block:IndexHandler) -> Range<Int>
  func upto(toValue:Self, _ block:IndexHandler) -> Range<Int>
  func downto(toValue:Self, _ block:IndexHandler) -> Range<Int>
}

###Contact

If you end up using NumeralExtension in a project, I'd love to hear about it.

email: [email protected]
twitter: @seivanheidari


License

NumeralExtension is © 2014 Seivan and may be freely distributed under the MIT license. See the LICENSE.md file.


About

Adding functions as a protocol NumeralExtension on Int and Double

Resources

License

Stars

Watchers

Forks

Packages

No packages published