roganjoshua
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
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Hello
I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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?
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.garrison
Building upon this, it’s common in these sorts of apps to want to look up by
idand be able to iterate through a (sorted) list of entries. One simple way to do this is to use List.keyfind/4 to find a player in the list by id. That function is a nif so it should be a bit faster than iterating by hand.Alternatively you could keep a map of
id => playerand then keep a list[id]of ids like a secondary index. This is more optimized for lookups than thekeyfindapproach.brkn
Am I understanding the problem correctly, it feels awkward to update a struct couple levels deep? Because I had the same exact feeling, and I don’t know the answer either. Different modules feels tightly coupled? I compared my elixir code to what I would have write in ruby here: @berkan.dev on Bluesky
I’ve checked the repo linked in the erlangelist post. There
Round.dealcallsHand.deal, and this feels weird to me, in my case it wasRound→Player→Hand. Reminds me my ReactJs code ages ago where I drilled down actions through components.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/3andupdate_in/3, there are fancier libraries that help with ergonomics. One was recently discussed in this forum:JKWA
Working with a deck of cards is a good product/sum type problem.
roganjoshua
Thank you all for your input!
The Sasa link is really invaluable @mindok !!!
As an update, this approach seems reasonable, I will need an id for the player and perhaps the struct
Playershould include the atom ,although the atom feels structural rather than data.Now I can do
players[:player_one].rackfor example and iterate as I think @garrison mentioned.So this kind of thing can now work…
and
It is still a journey but I feel I am beginning to refine my approach a lttle.
Not 100% on
put_in/3as @derek-zhou mentioned but I can go and check the code to see what it actually does.Asd
I’d suggest using Pathex for this.
put_inand other*_infunctions have one big problem: they don’t work with arbitrary structures, because they expect every structure to implementAccessbehavior. 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 -pcrates 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.
garrison
Make sure you also check out the Keyword lists and Maps guide as it has some good info on this topic.
Your example is indeed similar to what I was talking about, but you’re using a keyword list so you can only have atom keys. In practice you would key a list like this by a database id, probably an integer or string, and the keyword lookup wouldn’t work in that case. That’s where you would use
List.keyfind/4instead.JKWA
This might be helpful on your journey.
Optics are likely overkill for your problem, but can be helpful to know the underlying ideas.