I know how to use comprehensions to generate combinations/permutations:

```
(for a <- 0..9,
b <- 0..9 |> Enum.reject(&Kernel.==(&1, a)),
c <- 0..9 |> Enum.reject(fn i -> i == a or i == b end),
do:
[a,b,c])
|> apply_criteria()
```

How would I do this lazily so that I stop generating permutations once I find the one that fulfills some criteria? I tried using `Stream.repeatedly`

:

```
Stream.repeatedly(Enum.random(0..9))
|> Stream.chunk_every(3)
|> Stream.filter(fn combo -> Enum.uniq(combo) == combo end)
|> apply_criteria()
```

But this only works in the cases for which there is a permutation that satisfies the criteria. If there is no match it will just go infinitely. In this example I’m using permutations of 3 items, but in the real problem the length of the permutations is variable. The best non lazy approach I’ve come up with generating permutations of variable length is recursive:

```
def gen_perms(0, combos), do: combos
def gen_perms(len, []) do
gen_perms(len - 1, Enum.map(0..9, fn i -> [i] end))
end
def gen_perms(len, combos) do
new_combos =
for combo <- combos,
n <- 0..9 |> Enum.filter(fn i -> not Enum.member?(combo, i) end),
do: [n | combo]
gen_perms(len - 1, new_combos)
end
```

(As an aside, in the Stream approach I used the random function to pick the starting digit because there is no reason to suspect that sequential order is going to be the most efficient path, even if in the worst case the randomness leads to never getting all permutations. I have not yet decided if that is a good tradeoff.)