Skip to content

Instantly share code, notes, and snippets.

@NachoSoto
Created July 2, 2015 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NachoSoto/e4569159c001ab654a72 to your computer and use it in GitHub Desktop.
Save NachoSoto/e4569159c001ab654a72 to your computer and use it in GitHub Desktop.
//select ::[a] ->[(a, [a])]
//select[] = []
//select(x:xs) = (x, xs) : [(y, x:ys) | (y, ys) <-select xs]
func select<T>(input: [T]) -> [(T, [T])] {
if (input.isEmpty) {
return []
} else {
let head = input[0]
let rest = Array(input[1..<input.count])
return [(head, rest)] + select(rest).map { ($0, [head] + $1) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment