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

First Post!

mindok

mindok

Hi @roganjoshua,

There’s a pretty nice discussion about modelling a game (in this case Blackjack) from a functional and process point of view here: The Erlangelist - To spawn, or not to spawn?

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:

Last Post!

roganjoshua

roganjoshua

interesting

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement