Created
February 6, 2017 19:48
-
-
Save Sonictherocketman/e3449da4c9b47dfd86119b4e71a43d25 to your computer and use it in GitHub Desktop.
A set of functional methods for Swift arrays.
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
// Array+Functional+Extension.swift | |
// A set of functional methods for arrays. | |
// Created by Brian Schrader on 2/6/17. | |
import Foundation | |
extension Array { | |
/** | |
* Determine if all elements in the array are true. | |
*/ | |
func all() -> Bool { | |
return all { $0 as? Bool == true } | |
} | |
/** | |
* Determine if all elements in the array meet some criteria. | |
*/ | |
func all(_ criteria: (_ element: Element)->(Bool)) -> Bool { | |
for element in self { | |
if !criteria(element) { | |
return false | |
} | |
} | |
return true | |
} | |
/** | |
* Determine if any element in the array is true. | |
*/ | |
func any() -> Bool { | |
return any { $0 as? Bool == true } | |
} | |
/** | |
* Determine if any element in the array meets some criteria. | |
*/ | |
func any(_ criteria: (_ element: Element)->(Bool)) -> Bool { | |
for element in self { | |
if criteria(element) { | |
return true | |
} | |
} | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment