Skip to content

Instantly share code, notes, and snippets.

@masakih
Created November 3, 2020 01:30
Show Gist options
  • Select an option

  • Save masakih/24138ba35b44cf8732ad19c51703bb26 to your computer and use it in GitHub Desktop.

Select an option

Save masakih/24138ba35b44cf8732ad19c51703bb26 to your computer and use it in GitHub Desktop.
Collectionから取り出した3つの要素の全組み合わせを列挙するやつ #CodePiece
extension Collection {
func list3() -> [[Element]] where Index == Int {
zip(0..., self).flatMap { (i: Int, e0: Element) -> [[Element]] in
zip((i + 1)..., self[(i + 1)...]).flatMap { (j: Int, e1: Element) -> [[Element]] in
self[(j + 1)...].map { (e2: Element) -> [Element] in
[e0, e1, e2]
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment