3 ms·
I am not sure if there is a name for this type of function, so I called it someFunction: func someFunction<T>(initial: T, next: T -> T?) -> T {
by cballard 11y ago
I am not sure if there is a name for this type of function, so I called it someFunction:
func someFunction<T>(initial: T, next: T -> T?) -> T
{
var current = initial
while true
{
switch next(current)
{
case .Some(let value):
current = value
case .None:
return current
}
}
}
Now you can express that idea generally, e.g.:
extension UIView
{
var rootView: UIView
{
return someFunction(self, next: { view in view.superview })
}
}
I also didn't test it, so it might not work.
Of course, it would be more Swifty to create a "Parentable" protocol and write that as an extension of it!