stevensonmt
Lazily generate permutations
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.)
Most Liked
dimitarvp
As an aside: filtering is supported out of the box by Elixir’s for comprehension:
(for a <- 0..9, b <- 0..9, c <- 0..9, a != b and b != c, do: [a,b,c])
al2o3cr
If you know the length of the permutation you want, then RecursiveStream.all is doing more computation than you need.
RecursiveStream.all_exactly(alphabet, desired_permutation_length) gives a stream of permutations of a specific length; RecursiveStream.all concatenates those streams for all possible lengths.
APB9785
I think you need to go in order, otherwise you won’t know when you have exhausted all possibilities. I would use the recursive approach like such:
def gen_perms(criteria), do: gen_perms({0, 0, 0}, criteria)
def gen_perms({_, _, 10}, criteria), do: {:no_match, criteria}
def gen_perms({a, 10, c}, criteria), do: gen_perms({a, 0, c+1}, criteria)
def gen_perms({10, b, c}, criteria), do: gen_perms({0, b+1, c}, criteria)
def gen_perms({a, b, c}, criteria) do
if criteria_match?({a, b, c}, criteria) do
{:ok, {a, b, c}, criteria}
else
gen_perms({a+1, b, c}, criteria)
end
end
This way the function will go through each possibility one-by-one, and immediately stop when a match is found.
al2o3cr
Here’s a fully-lazy approach:
defmodule RecursiveStream do
def all_exactly(_, 0), do: []
def all_exactly([], _), do: []
def all_exactly(alphabet, 1) do
Stream.map(alphabet, &[&1])
end
def all_exactly(alphabet, n) do
streams = Enum.map(alphabet, &prepend(&1, all_exactly(alphabet -- [&1], n-1)))
Stream.concat(streams)
end
def all(alphabet) do
Stream.iterate(1, &(&1 + 1))
|> Stream.take(length(alphabet))
|> Stream.map(&all_exactly(alphabet, &1))
|> Stream.concat()
end
defp prepend(x, stream) do
Stream.map(stream, &([x] ++ &1))
end
end
alphabet = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
result =
RecursiveStream.all(alphabet) |> Stream.take(1000) |> Enum.to_list()
IO.inspect(result, limit: :infinity)
This produces results in order of length: all the length-1 permutations, all the length-2, etc
The maximum length of a result in the output is bounded by the length of the input alphabet, since the values cannot appear more than once. The Stream.take(length(alphabet)) makes sure that the calculation terminates; otherwise, all_exactly will return [] for too-large n and the Stream.concat will iterate forever.
thepeoplesbourgeois
You’ll generate the permutations faster with a map







