Note: This is a ‘sequel’ question to my previous permutations question.
I would like to generate all permutations of a list in Erlang, but I would like to filter out some of the permutations before they are added to the stack or stored anywhere.
I will filter out the permutations based on some custom ad-hoc rules (let’s call them “Filter”).
In other words, I would like to generate a list of permutations of a large list (50-300 elements), but I would like to throw out most of the generated permutations right during the process (I know that the full number of permutations is N!).
steenslag has given me a nice solution in Ruby:
res = [1,2,3,4].permutation(3).reject do |perm|
perm.first.even? #Filter: if this line is true, the perm will be rejected
end
How can I write something similar in Erlang?
(I have my Filter functions already written in Erlang, so I would like to do some code reuse).
By the way, can the desired Erlang solution be ‘intrinsically parallel’?
I did a possible implementation for the problem:
It uses empty lists as placeholders for lists that must be discarded. If this is not acceptable, you can change the implementation to not use list compreension.
The pred/1 function is just an example function to use as predicate.
Hope this helps.