Created
April 15, 2015 11:55
-
-
Save cobysy/6331108ed944d2e03582 to your computer and use it in GitHub Desktop.
recursiveFlatMap.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
// Copyright (c) 2015 bysy.io. All rights reserved. | |
func recursiveFlatMap<TResult>(#root: AnyObject, | |
@noescape children: (AnyObject) -> [AnyObject]) -> [TResult] | |
{ | |
var result = [TResult]() | |
if let value = root as? TResult { | |
result.append(value) | |
} | |
result += children(root).flatMap( { recursiveFlatMap(root: $0, children: children) as [TResult] } ) | |
return result | |
} | |
// Usage | |
let allLabelsInView = recursiveFlatMap(root: someView, | |
children: { $0.subviews as! [UIView] }) as [UILabel] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment