Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swift formatted code for v5 #16

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
9 changes: 6 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ let package = Package(
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "Algorithm",
targets: ["Algorithm"]),
targets: ["Algorithm"]
)
],
dependencies: [
// Dependencies declare other packages that this package depends on.
Expand All @@ -21,10 +22,12 @@ let package = Package(
.target(
name: "Algorithm",
dependencies: [],
path: "Sources"),
path: "Sources"
),
.testTarget(
name: "AlgorithmTests",
dependencies: ["Algorithm"],
path: "Tests"),
path: "Tests"
)
]
)
217 changes: 108 additions & 109 deletions Sources/Algorithm+Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,117 +24,116 @@
*/

extension Array where Element: Equatable {
/**
Removes a given Element from an Array if it exists.
- Parameter object: An Element.
- Returns: An optional Element if the removed
element exists.
*/
@discardableResult
mutating func remove(object: Element) -> Element? {
return firstIndex(of: object).map { self.remove(at: $0) }
}

/**
Removes a list of given Elements from an Array if
they exists.
- Parameter objects: A list of Elements.
*/
mutating func remove(objects: Element...) {
remove(objects: objects)
}

/**
Removes an Array of given Elements from an Array if
they exists.
- Parameter objects: An Array of Elements.
*/
mutating func remove(objects: [Element]) {
objects.forEach {
self.remove(object: $0)
/**
Removes a given Element from an Array if it exists.
- Parameter object: An Element.
- Returns: An optional Element if the removed
element exists.
*/
@discardableResult
mutating func remove(object: Element) -> Element? {
return firstIndex(of: object).map { self.remove(at: $0) }
}
}

/**
The total count for the given Elements.
- Parameter of elements: A list of Elements.
- Returns: An Int.
*/
public func count(of elements: Element...) -> Int {
return count(of: elements)
}

/**
The total count for the given Elements.
- Parameter of elements: An Array of Elements.
- Returns: An Int.
*/
public func count(of elements: [Element]) -> Int {

var c = 0
for e in elements {
for x in self where e == x {
c += 1
}

/**
Removes a list of given Elements from an Array if
they exists.
- Parameter objects: A list of Elements.
*/
mutating func remove(objects: Element...) {
remove(objects: objects)
}
return c
}

/**
The probability of getting the given Elements.
- Parameter of elements: A list of Elements.
- Returns: A Double.
*/
public func probability(of elements: Element...) -> Double {
return probability(of: elements)
}

/**
The probability of getting the given Elements.
- Parameter of elements: An Array of Elements.
- Returns: A Double.
*/
public func probability(of elements: [Element]) -> Double {
return 0 < count ? Double(count(of: elements)) / Double(count) : 0
}

/**
A probability method that uses a block to determine the member state of a condition.
- Parameter of elements: A list of Elements.
- Returns: A Double.
*/
public func probability(execute block: @escaping (Element) -> Bool) -> Double {
guard 0 < count else {
return 0

/**
Removes an Array of given Elements from an Array if
they exists.
- Parameter objects: An Array of Elements.
*/
mutating func remove(objects: [Element]) {
objects.forEach {
self.remove(object: $0)
}
}

var c = 0
for e in self {
if block(e) {
c += 1
}

/**
The total count for the given Elements.
- Parameter of elements: A list of Elements.
- Returns: An Int.
*/
public func count(of elements: Element...) -> Int {
return count(of: elements)
}

/**
The total count for the given Elements.
- Parameter of elements: An Array of Elements.
- Returns: An Int.
*/
public func count(of elements: [Element]) -> Int {
var c = 0
for e in elements {
for x in self where e == x {
c += 1
}
}
return c
}

/**
The probability of getting the given Elements.
- Parameter of elements: A list of Elements.
- Returns: A Double.
*/
public func probability(of elements: Element...) -> Double {
return probability(of: elements)
}

/**
The probability of getting the given Elements.
- Parameter of elements: An Array of Elements.
- Returns: A Double.
*/
public func probability(of elements: [Element]) -> Double {
return count > 0 ? Double(count(of: elements)) / Double(count) : 0
}

/**
A probability method that uses a block to determine the member state of a condition.
- Parameter of elements: A list of Elements.
- Returns: A Double.
*/
public func probability(execute block: @escaping (Element) -> Bool) -> Double {
guard count > 0 else {
return 0
}

var c = 0
for e in self {
if block(e) {
c += 1
}
}

return Double(c) / Double(count)
}

/**
Calculates the expected value of elements based on a given number of trials.
- Parameter trials: Number of trials.
- Parameter elements: A list of Elements.
- Returns: A Double.
*/
public func expectedValue(trials: Int, for elements: Element...) -> Double {
return expectedValue(trials: trials, for: elements)
}

/**
Calculates the expected value of elements based on a given number of trials.
- Parameter trials: Number of trials.
- Parameter elements: An Array of Elements.
- Returns: A Double.
*/
public func expectedValue(trials: Int, for elements: [Element]) -> Double {
return Double(trials) * probability(of: elements)
}

return Double(c) / Double(count)
}

/**
Calculates the expected value of elements based on a given number of trials.
- Parameter trials: Number of trials.
- Parameter elements: A list of Elements.
- Returns: A Double.
*/
public func expectedValue(trials: Int, for elements: Element...) -> Double {
return expectedValue(trials: trials, for: elements)
}

/**
Calculates the expected value of elements based on a given number of trials.
- Parameter trials: Number of trials.
- Parameter elements: An Array of Elements.
- Returns: A Double.
*/
public func expectedValue(trials: Int, for elements: [Element]) -> Double {
return Double(trials) * probability(of: elements)
}
}
Loading