Last active
April 19, 2019 11:22
-
-
Save kristopherjohnson/644625e0ce1415b375415ba839b6867c to your computer and use it in GitHub Desktop.
Implement "If", "Else", and "ElseIf" in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Implementation of If, ElseIf, and Else in Swift. | |
// | |
// Supports uses such as these: | |
// | |
// If (condition) { doAction() } | |
// | |
// If (condition) { doAction() } | |
// .Else { doOtherAction() } | |
// | |
// If (condition) { doAction() } | |
// .ElseIf (condition1) { doAction2() } | |
// .ElseIf (conditionN) { doActionN() } | |
// .Else { doOtherAction() } | |
import Foundation | |
/// Object that processes whatever comes after an If() or ElseIf() | |
protocol ElseContext { | |
func Else(_ action: () -> ()) | |
@discardableResult | |
func ElseIf(_ condition: @autoclosure () -> Bool, _ action: () -> ()) -> ElseContext | |
} | |
struct If { | |
/// If the If condition is false, then ContinueElse will execute | |
/// the Else part or go on to the next ElseIf. | |
struct ContinueElses: ElseContext { | |
func Else(_ action: () -> ()) { | |
action() | |
} | |
@discardableResult | |
func ElseIf(_ condition: @autoclosure () -> Bool, _ action: () -> ()) -> ElseContext { | |
if condition() { | |
action() | |
return SkipElses() | |
} | |
else { | |
return ContinueElses() | |
} | |
} | |
} | |
/// If the If condition is true, then remaining ElseIf/Else items | |
/// will do nothing. | |
struct SkipElses: ElseContext { | |
func Else(_ action: () -> ()) {} | |
@discardableResult | |
func ElseIf(_ condition: @autoclosure () -> Bool, _ action: () -> ()) -> ElseContext { | |
return self | |
} | |
} | |
let elseContext: ElseContext | |
init(_ condition: Bool, _ action: () -> ()) { | |
if condition { | |
elseContext = SkipElses() | |
action() | |
} | |
else { | |
elseContext = ContinueElses() | |
} | |
} | |
func Else(_ action: () -> ()) { | |
elseContext.Else(action) | |
} | |
@discardableResult | |
func ElseIf(_ condition: @autoclosure () -> Bool, _ action: () -> ()) -> ElseContext { | |
return elseContext.ElseIf(condition, action) | |
} | |
} | |
// Example | |
func testValue(_ value: Int) { | |
If (value % 2 == 0) { | |
print("\(value) is even") | |
} | |
.Else { | |
print("\(value) is odd") | |
} | |
If (value > 0) { | |
print("\(value) is positive") | |
} | |
.ElseIf (value < 0) { | |
print("\(value) is negative") | |
} | |
.Else { | |
print("\(value) is zero") | |
} | |
} | |
testValue(-10) | |
testValue(-3) | |
testValue(0) | |
testValue(5) | |
testValue(12) | |
// Output: | |
// -10 is even | |
// -10 is negative | |
// -3 is odd | |
// -3 is negative | |
// 0 is even | |
// 0 is zero | |
// 5 is odd | |
// 5 is positive | |
// 12 is even | |
// 12 is positive |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment