Skip to content

Instantly share code, notes, and snippets.

@hsavit1
Created August 7, 2016 04:41
Show Gist options
  • Save hsavit1/021015ff349eb1d0721dfda056e890b3 to your computer and use it in GitHub Desktop.
Save hsavit1/021015ff349eb1d0721dfda056e890b3 to your computer and use it in GitHub Desktop.
Head and tail for Swift
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