Skip to content

Commit

Permalink
Minor changes for better undertanding of how the protocols Comparable…
Browse files Browse the repository at this point in the history
… and Equatable are used and what functions lies in which protocol.
  • Loading branch information
Aviral190694 authored and aciidgh committed Feb 19, 2018
1 parent d0aa41f commit a0d48d4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
16 changes: 8 additions & 8 deletions Sources/PlayingCard/PlayingCard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ public struct PlayingCard {

// MARK: - Equatable

extension PlayingCard: Equatable {}

public func ==(lhs: PlayingCard, rhs: PlayingCard) -> Bool {
return lhs.rank == rhs.rank && lhs.suit == rhs.suit
extension PlayingCard: Equatable {
public static func ==(lhs: PlayingCard, rhs: PlayingCard) -> Bool {
return lhs.rank == rhs.rank && lhs.suit == rhs.suit
}
}

// MARK: - Comparable

extension PlayingCard: Comparable {}

public func <(lhs: PlayingCard, rhs: PlayingCard) -> Bool {
return lhs.rank == rhs.rank ? lhs.suit < rhs.suit : lhs.rank < rhs.rank
extension PlayingCard: Comparable {
public static func <(lhs: PlayingCard, rhs: PlayingCard) -> Bool {
return lhs.rank == rhs.rank ? lhs.suit < rhs.suit : lhs.rank < rhs.rank
}
}

// MARK: - CustomStringConvertible
Expand Down
20 changes: 10 additions & 10 deletions Sources/PlayingCard/Rank.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ public enum Rank : Int {

// MARK: - Comparable

extension Rank : Comparable {}

public func <(lhs: Rank, rhs: Rank) -> Bool {
switch (lhs, rhs) {
case (_, _) where lhs == rhs:
return false
case (.ace, _):
return false
default:
return lhs.rawValue < rhs.rawValue
extension Rank : Comparable {
public static func <(lhs: Rank, rhs: Rank) -> Bool {
switch (lhs, rhs) {
case (_, _) where lhs == rhs:
return false
case (.ace, _):
return false
default:
return lhs.rawValue < rhs.rawValue
}
}
}

Expand Down
24 changes: 12 additions & 12 deletions Sources/PlayingCard/Suit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ public enum Suit: String {

// MARK: - Comparable

extension Suit: Comparable {}

public func <(lhs: Suit, rhs: Suit) -> Bool {
switch (lhs, rhs) {
case (_, _) where lhs == rhs:
return false
case (.spades, _),
(.hearts, .diamonds), (.hearts, .clubs),
(.diamonds, .clubs):
return false
default:
return true
extension Suit: Comparable {
public static func <(lhs: Suit, rhs: Suit) -> Bool {
switch (lhs, rhs) {
case (_, _) where lhs == rhs:
return false
case (.spades, _),
(.hearts, .diamonds), (.hearts, .clubs),
(.diamonds, .clubs):
return false
default:
return true
}
}
}

Expand Down

0 comments on commit a0d48d4

Please sign in to comment.