Created
August 7, 2016 04:41
-
-
Save hsavit1/021015ff349eb1d0721dfda056e890b3 to your computer and use it in GitHub Desktop.
Head and tail for 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
extension Array { | |
var match : (head: T, tail: [T])? { | |
return (count > 0) ? (self[0],Array(self[1..<count])) : nil | |
} | |
} | |
func map<A,B>(f: A -> B, arr: [A]) -> [B] { | |
if let (head,tail) = arr.match { | |
return [f(head)] + map(f, tail) | |
} else { | |
return [] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment