jwaldner

jwaldner

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!!

Showing Posts 1 to 4

NobbZ

NobbZ

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.

zachallaun

zachallaun

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

jwaldner

jwaldner OP

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

zachallaun

zachallaun

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.)

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews