Skip to content

Instantly share code, notes, and snippets.

@latant
Created November 7, 2020 17:55
Show Gist options
  • Save latant/fd192527b0ecc4827a74558089aabaaf to your computer and use it in GitHub Desktop.
Save latant/fd192527b0ecc4827a74558089aabaaf to your computer and use it in GitHub Desktop.
efficient permutations in Kotlin
import org.pcollections.PSet
fun <T> PSet<T>.permutations(): Sequence<Sequence<T>> = when {
size < 2 -> sequenceOf(asSequence())
else -> asSequence().flatMap { x -> minus(x).permutations().asSequence().map { sequenceOf(x) + it } }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment