-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
Return a successful value or the provided default | ||
- If the left hand value is `.some`, this will return the left hand value | ||
- If the left hand value is `.none`, this will return the default on the | ||
right hand side | ||
- parameter lhs: A value of type `Optional<T>` | ||
- parameter rhs: A value of type `Optional<T>` | ||
- returns: a value of type `Optional<T>` | ||
*/ | ||
public func <|> <T>(lhs: T?, rhs: @autoclosure () -> T?) -> T? { | ||
return lhs.or(rhs) | ||
} | ||
|
||
/** | ||
Return an empty context of `.none` | ||
This is the dual of `pure`. | ||
- returns: An instance of `.none` of the type `T?` | ||
*/ | ||
public func empty<T>() -> T? { | ||
return .none | ||
} | ||
|
||
public extension Optional { | ||
/** | ||
Return a successful value or the provided default | ||
- If `self` is `.some`, this will return `self` | ||
- If `self` is `.none`, this will return the provided default | ||
- parameter other: A value of type `Optional<T>` | ||
- returns: a value of type `Optional<T>` | ||
*/ | ||
func or(_ other: @autoclosure () -> Wrapped?) -> Wrapped? { | ||
switch self { | ||
case .some: return self | ||
case .none: return other() | ||
} | ||
} | ||
} |
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