roganjoshua

roganjoshua

Maintaining state in a deep map

Coming from an object-oriented background I am writing a scrabble clone in elixir/phoenix in order to learn elixir and functional programming.

So my instinct is to have a state map like so:

game_state = {
  players: [
    {
      name: "Bob",
      rack: ["A"],
      score: 3
    },
    {
      name: "Alice",
      rack: ["B"],
      score: 10
    }
  ],
  board: [
    %Tile{
      letter: "A",
      row: 0,
      column: -1
    },
    %Tile{
      letter: "T",
      row: 0,
      column: 0 
    }
  ],
  bag: [
    "A",
    "A",
    " "
  ]
}


This is enough to maintain the state of the game.

Doing it this way seems to give flexiblity, storage and atomicity.

Writing interactions with this state feel a bit awkward and I am thinking I am stuck in OO world and I am building an object rather being functional.

I am thinking modules for bag, players and board.

new_game_state = game_state |> Players.add_player("Fred")
Am I going the wrong way with this?

I am doing things like if Enum.member?(player_state, &(&1.name == name)) do which is hauntingly familiar.

Martin

Most Liked

Asd

Asd

I’d suggest using Pathex for this.

put_in and other *_in functions have one big problem: they don’t work with arbitrary structures, because they expect every structure to implement Access behavior. Most of structures don’t implement it. And even if they do, it introduces unnecessary boilerplate and runtime overhead.

Pathex doesn’t have any of these problems, plus it is declarative, performant, extensible, can to nested sets (like mkdir -p crates intermediary folders, pathex creates intermediary structures), works with tuples, and even has smart things like lenses, filters, etc.

I am using Pathex to maintain complex state in GenServers where there is a lot of logic involved and I also use Pathex to traverse deeply nested structures like parsed HTML, verbose services responses (like ElasticSearch), etc.

cmo

cmo

You could make the players state a map where their name or ID is the key. The board could be a map with {row, column} keys. That might make lookups a bit nicer.

derek-zhou

derek-zhou

You don’t have to colocate the functions that manipulate the struct in the same module that defines the struct. I frequently have one module with several sub-modules, all defined in the same file. The sub-modules have nothing but a struct, and all the business logic reside in the main module.

As for updating a struct couple levels deep, if you don’t like put_in/3 and update_in/3, there are fancier libraries that help with ergonomics. One was recently discussed in this forum:

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement