Skip to content

Instantly share code, notes, and snippets.

@Sonictherocketman
Created February 6, 2017 19:48
Show Gist options
  • Save Sonictherocketman/e3449da4c9b47dfd86119b4e71a43d25 to your computer and use it in GitHub Desktop.
Save Sonictherocketman/e3449da4c9b47dfd86119b4e71a43d25 to your computer and use it in GitHub Desktop.
A set of functional methods for Swift arrays.
// 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