Skip to content

Instantly share code, notes, and snippets.

@cobysy
Created April 15, 2015 11:55
Show Gist options
  • Save cobysy/6331108ed944d2e03582 to your computer and use it in GitHub Desktop.
Save cobysy/6331108ed944d2e03582 to your computer and use it in GitHub Desktop.
recursiveFlatMap.swift
// 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