-
Notifications
You must be signed in to change notification settings - Fork 8
/
Integer.swift
124 lines (100 loc) · 2.25 KB
/
Integer.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// Integer.swift
// SwiftRuby
//
// Created by John Holdsworth on 26/09/2015.
// Copyright © 2015 John Holdsworth. All rights reserved.
//
// $Id: //depot/SwiftRuby/Integer.swift#8 $
//
// Repo: https://github.com/RubyNative/SwiftRuby
//
// See: http://ruby-doc.org/core-2.2.3/Integer.html
//
public typealias fixnum = Int
public protocol int_like {
var to_i: Int { get }
}
extension Int: int_like {
public var to_i: Int {
return self
}
public var to_f: Double {
return Double(self)
}
public var to_s: String {
return String(self)
}
public var to_b: String {
return String(self, radix: 2)
}
public var to_o: String {
return String(self, radix: 8 )
}
public var to_h: String {
return String(self, radix: 16, uppercase: false)
}
public var chr: String {
var chars = UInt16(self)
return String(utf16CodeUnits: &chars, count: 1)
}
public var denominator: Int {
return 1
}
public func downto(_ limit: Int, block: (_ i: Int) -> ()) -> Int {
var i = self
while i >= limit {
block(i)
i -= 1
}
return self
}
public var even: Bool {
return self & 0x1 == 0
}
// public func gcd(int2: Int) -> Int {
// RKNotImplemented("Integer.gcd")
// return -1
// }
//
// public func gcdlcm(int2: Int) -> Int {
// RKNotImplemented("Integer.gcd")
// return -1
// }
//
// public func lcm(int2: Int) -> Int {
// RKNotImplemented("Integer.gcd")
// return -1
// }
public var next: Int {
return self+1
}
public var numerator: Int {
return self+1
}
public var odd: Bool {
return !even
}
public var ord: Int {
return self
}
public var pred: Int {
return self-1
}
public func times(_ block: (_ i: Int) -> ()) -> Int {
var i = 1
while i <= self {
block(i)
i += 1
}
return self
}
public func upto(_ limit: Int, block: (_ i: Int) -> ()) -> Int {
var i = self
while i <= limit {
block(i)
i += 1
}
return self
}
}