MapSet.difference is reordring my list how to avoid that

I am just learning and am writing a word game.
I have a list of letters ordered by frequency of use and am doing a MapSet.difference() with letters [a-z] sorted by frequency of use & used letters to determine the next guess for an automated client

that works fine but the function re orders the new difference list alphabetically

I wanted to keep the [f] list order then just pick the next most frequent letter for a guess

f =
[“e”, “a”, “r”, “i”, “o”, “t”, “n”, “s”, “l”, “c”, “u”, “d”, “p”, “b”, “f”, “g”,
“h”, “j”, “k”, “m”, “q”, “v”, “w”, “x”, “y”, “z”]

s =
[“a”, “b”, “c”]

r = MapSet.difference(MapSet.new(f),MapSet.new(s))

the list does not start with “r” anymore [it’s new and that is fine, but I wanted to do an Enum.at(…0) to pick the next letter off the list… ]

r is {
MapSet.new([“d”, “e”, “f”, “g”, “h”, “i”, “j”, “k”, “l”, “m”, “n”, “o”, “p”,
“q”, “r”, “s”, “t”, “u”, “v”, “w”, “x”, “y”, “z”])
}

is there a way to do this?
Thanks!!

Sets are generally unordered.

So any order you observe is by accident and has to be considered an implementation detail.

If you need to maintain order, a MapSet is not the correct data structure to use.

4 Likes

Try using the -- operator on your lists directly instead.

1 Like

This does what I need, so simple.
But trying to wrap my head around functional programming
wow

def prune(list, items) do
list – items
end

a variable for game_state

def get_guess(_tally = %{used: state}) do
letters_by_frequency = [
“e”,“a”,“r”,“i”,“o”,“t”,“n”,“s”,“l”,“c”,“u”,“d”,“p”,
“b”,“f”,“g”,“h”,“j”,“k”,“m”,“q”,“v”,“w”,“x”,“y”,“z”
]

prune(letters_by_frequency, state)
|> Enum.at(0)

end

Enjoy the journey!

Just to consider alternatives to accomplish the same thing: you could still represent the most common letters as a list, but use a set to track guesses. Then you might have a function like this:

def get_guess(by_frequency, used) do
  Enum.find(by_frequency, fn letter ->
    letter not in used
  end)
end

This isn’t necessarily “better” (and may actually be technically slower for small lists like you’re dealing with), but thought it might be worth seeing a different way of accomplishing the same thing. (BTW the above will also work if used is a list.)