functional programming - What is the Swift equivalent of C#/.NET/LINQ's Enumerable.All method? -
i want function applies given function sequence , returns true iff given function returns true every element of sequence, enumerable.all c#/.net/linq world.
there isn't built-in function this, can add own protocol extension method:
extension sequencetype { func all(@noescape predicate: (self.generator.element) throws -> bool) rethrows -> bool { in self { if !(try predicate(i)) { return false } } return true } }
and use on sequence like:
let allpositive = [1, 2, 3].all { $0 > 0 }
Comments
Post a Comment