Skip to content

Instantly share code, notes, and snippets.

@RuiAAPeres
Last active August 29, 2015 14:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RuiAAPeres/3ef7af142a8958aaa62a to your computer and use it in GitHub Desktop.
Save RuiAAPeres/3ef7af142a8958aaa62a to your computer and use it in GitHub Desktop.
func curry<T,U,V>(f:(T,U)->V) -> T -> U -> V
{
return {x in { y in f(x,y)}}
}
extension Array {
var decompose : (T,[T])? {
return (count > 0) ? (self[0], Array(self[1..<count])) : nil
}
}
func quicksort(unsorted : [Int]) -> [Int]
{
switch (unsorted.decompose) {
case .Some(let head, let tail):
return quicksort(filter(tail, curry(>)(head))) + [head] + quicksort(filter(tail,curry(<=)(head)))
default:
return []
}
}
quicksort([2,1,4,0,1,4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment