|
1 | 1 | /*
|
2 |
| -* Copyright (C) 2015 - 2018, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.com>. |
3 |
| -* All rights reserved. |
4 |
| -* |
5 |
| -* Redistribution and use in source and binary forms, with or without |
6 |
| -* modification, are permitted provided that the following conditions are met: |
7 |
| -* |
8 |
| -* * Redistributions of source code must retain the above copyright notice, this |
9 |
| -* list of conditions and the following disclaimer. |
10 |
| -* |
11 |
| -* * Redistributions in binary form must reproduce the above copyright notice, |
12 |
| -* this list of conditions and the following disclaimer in the documentation |
13 |
| -* and/or other materials provided with the distribution. |
14 |
| -* |
15 |
| -* * Neither the name of CosmicMind nor the names of its |
16 |
| -* contributors may be used to endorse or promote products derived from |
17 |
| -* this software without specific prior written permission. |
18 |
| -* |
19 |
| -* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
20 |
| -* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 |
| -* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
22 |
| -* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
23 |
| -* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 |
| -* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
25 |
| -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
26 |
| -* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
27 |
| -* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 |
| -* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 |
| -*/ |
| 2 | + * Copyright (C) 2015 - 2019, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.com>. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * * Neither the name of CosmicMind nor the names of its |
| 16 | + * contributors may be used to endorse or promote products derived from |
| 17 | + * this software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 23 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 25 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 26 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 27 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
30 | 30 |
|
31 | 31 | extension Array where Element: Equatable {
|
32 |
| - /** |
33 |
| - Removes a given Element from an Array if it exists. |
34 |
| - - Parameter object: An Element. |
35 |
| - - Returns: An optional Element if the removed |
36 |
| - element exists. |
37 |
| - */ |
38 |
| - @discardableResult |
39 |
| - mutating func remove(object: Element) -> Element? { |
40 |
| - return index(of: object).map { self.remove(at: $0) } |
41 |
| - } |
42 |
| - |
43 |
| - /** |
44 |
| - Removes a list of given Elements from an Array if |
45 |
| - they exists. |
46 |
| - - Parameter objects: A list of Elements. |
47 |
| - */ |
48 |
| - mutating func remove(objects: Element...) { |
49 |
| - remove(objects: objects) |
50 |
| - } |
51 |
| - |
52 |
| - /** |
53 |
| - Removes an Array of given Elements from an Array if |
54 |
| - they exists. |
55 |
| - - Parameter objects: An Array of Elements. |
56 |
| - */ |
57 |
| - mutating func remove(objects: [Element]) { |
58 |
| - objects.forEach { |
59 |
| - self.remove(object: $0) |
60 |
| - } |
61 |
| - } |
62 |
| - |
63 |
| - /** |
64 |
| - The total count for the given Elements. |
65 |
| - - Parameter of elements: A list of Elements. |
66 |
| - - Returns: An Int. |
67 |
| - */ |
68 |
| - public func count(of elements: Element...) -> Int { |
69 |
| - return count(of: elements) |
70 |
| - } |
71 |
| - |
72 |
| - /** |
73 |
| - The total count for the given Elements. |
74 |
| - - Parameter of elements: An Array of Elements. |
75 |
| - - Returns: An Int. |
76 |
| - */ |
77 |
| - public func count(of elements: [Element]) -> Int { |
78 |
| - |
79 |
| - var c = 0 |
80 |
| - for e in elements { |
81 |
| - for x in self where e == x { |
82 |
| - c += 1 |
83 |
| - } |
84 |
| - } |
85 |
| - return c |
86 |
| - } |
87 |
| - |
88 |
| - /** |
89 |
| - The probability of getting the given Elements. |
90 |
| - - Parameter of elements: A list of Elements. |
91 |
| - - Returns: A Double. |
92 |
| - */ |
93 |
| - public func probability(of elements: Element...) -> Double { |
94 |
| - return probability(of: elements) |
95 |
| - } |
96 |
| - |
97 |
| - /** |
98 |
| - The probability of getting the given Elements. |
99 |
| - - Parameter of elements: An Array of Elements. |
100 |
| - - Returns: A Double. |
101 |
| - */ |
102 |
| - public func probability(of elements: [Element]) -> Double { |
103 |
| - return 0 < count ? Double(count(of: elements)) / Double(count) : 0 |
104 |
| - } |
105 |
| - |
106 |
| - /** |
107 |
| - A probability method that uses a block to determine the member state of a condition. |
108 |
| - - Parameter of elements: A list of Elements. |
109 |
| - - Returns: A Double. |
110 |
| - */ |
111 |
| - public func probability(execute block: @escaping (Element) -> Bool) -> Double { |
112 |
| - guard 0 < count else { |
113 |
| - return 0 |
114 |
| - } |
115 |
| - |
116 |
| - var c = 0 |
117 |
| - for e in self { |
118 |
| - if block(e) { |
119 |
| - c += 1 |
120 |
| - } |
121 |
| - } |
122 |
| - |
123 |
| - return Double(c) / Double(count) |
124 |
| - } |
125 |
| - |
126 |
| - /** |
127 |
| - Calculates the expected value of elements based on a given number of trials. |
128 |
| - - Parameter trials: Number of trials. |
129 |
| - - Parameter elements: A list of Elements. |
130 |
| - - Returns: A Double. |
131 |
| - */ |
132 |
| - public func expectedValue(trials: Int, for elements: Element...) -> Double { |
133 |
| - return expectedValue(trials: trials, for: elements) |
134 |
| - } |
135 |
| - |
136 |
| - /** |
137 |
| - Calculates the expected value of elements based on a given number of trials. |
138 |
| - - Parameter trials: Number of trials. |
139 |
| - - Parameter elements: An Array of Elements. |
140 |
| - - Returns: A Double. |
141 |
| - */ |
142 |
| - public func expectedValue(trials: Int, for elements: [Element]) -> Double { |
143 |
| - return Double(trials) * probability(of: elements) |
144 |
| - } |
| 32 | + /** |
| 33 | + Removes a given Element from an Array if it exists. |
| 34 | + - Parameter object: An Element. |
| 35 | + - Returns: An optional Element if the removed |
| 36 | + element exists. |
| 37 | + */ |
| 38 | + @discardableResult |
| 39 | + mutating func remove(object: Element) -> Element? { |
| 40 | + return index(of: object).map { self.remove(at: $0) } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + Removes a list of given Elements from an Array if |
| 45 | + they exists. |
| 46 | + - Parameter objects: A list of Elements. |
| 47 | + */ |
| 48 | + mutating func remove(objects: Element...) { |
| 49 | + remove(objects: objects) |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + Removes an Array of given Elements from an Array if |
| 54 | + they exists. |
| 55 | + - Parameter objects: An Array of Elements. |
| 56 | + */ |
| 57 | + mutating func remove(objects: [Element]) { |
| 58 | + objects.forEach { |
| 59 | + self.remove(object: $0) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + The total count for the given Elements. |
| 65 | + - Parameter of elements: A list of Elements. |
| 66 | + - Returns: An Int. |
| 67 | + */ |
| 68 | + public func count(of elements: Element...) -> Int { |
| 69 | + return count(of: elements) |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + The total count for the given Elements. |
| 74 | + - Parameter of elements: An Array of Elements. |
| 75 | + - Returns: An Int. |
| 76 | + */ |
| 77 | + public func count(of elements: [Element]) -> Int { |
| 78 | + |
| 79 | + var c = 0 |
| 80 | + for e in elements { |
| 81 | + for x in self where e == x { |
| 82 | + c += 1 |
| 83 | + } |
| 84 | + } |
| 85 | + return c |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + The probability of getting the given Elements. |
| 90 | + - Parameter of elements: A list of Elements. |
| 91 | + - Returns: A Double. |
| 92 | + */ |
| 93 | + public func probability(of elements: Element...) -> Double { |
| 94 | + return probability(of: elements) |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + The probability of getting the given Elements. |
| 99 | + - Parameter of elements: An Array of Elements. |
| 100 | + - Returns: A Double. |
| 101 | + */ |
| 102 | + public func probability(of elements: [Element]) -> Double { |
| 103 | + return 0 < count ? Double(count(of: elements)) / Double(count) : 0 |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + A probability method that uses a block to determine the member state of a condition. |
| 108 | + - Parameter of elements: A list of Elements. |
| 109 | + - Returns: A Double. |
| 110 | + */ |
| 111 | + public func probability(execute block: @escaping (Element) -> Bool) -> Double { |
| 112 | + guard 0 < count else { |
| 113 | + return 0 |
| 114 | + } |
| 115 | + |
| 116 | + var c = 0 |
| 117 | + for e in self { |
| 118 | + if block(e) { |
| 119 | + c += 1 |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return Double(c) / Double(count) |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + Calculates the expected value of elements based on a given number of trials. |
| 128 | + - Parameter trials: Number of trials. |
| 129 | + - Parameter elements: A list of Elements. |
| 130 | + - Returns: A Double. |
| 131 | + */ |
| 132 | + public func expectedValue(trials: Int, for elements: Element...) -> Double { |
| 133 | + return expectedValue(trials: trials, for: elements) |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + Calculates the expected value of elements based on a given number of trials. |
| 138 | + - Parameter trials: Number of trials. |
| 139 | + - Parameter elements: An Array of Elements. |
| 140 | + - Returns: A Double. |
| 141 | + */ |
| 142 | + public func expectedValue(trials: Int, for elements: [Element]) -> Double { |
| 143 | + return Double(trials) * probability(of: elements) |
| 144 | + } |
145 | 145 | }
|
0 commit comments