borybar
I have a struct that looks like this:
defmodule WarGame.Core.Game do
defstruct user_cards: [],
croupier_cards: [],
user_bets: [],
bets: [:card_odd, :color_player],
deck: %{},
used: [],
title: nil
def new(fields) do
struct!(__MODULE__, fields)
end
end
I would like to create a function that can take a chosen element of a list bets and move it to user_bets. How could I achieve this? There will be only finite (5) elements in this list.
Trending in Questions
Hello everyone!
I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++.
Does anyone ...
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
Other Trending Topics
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
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
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
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
(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
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
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
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #security
- #elixirconf-eu
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
tomkonidas
Map.take/2 should do what you need
borybar
So you suggest that I change my list to a map and then use
Map.take/2?tomkonidas
Sorry I misread the original post.
Maybe I would do something like this. (it is not the prettiest, but it should work)
borybar
I am not sure that I follow.
In my head I would like to create a function that as an argument takes an element from
bets, uses it to pick thisbetselement and inserts it touser_bets.Something like this:
tomkonidas
In this case, (I thought you also wanted to remove it from the bets)
Usually you would want to also pass in the game you are working with in the function.
This will return you a new game with the updated user_bets or the game untouched
borybar
Thanks for your time and effort!
When I am trying to run this code I get -
(ArgumentError) invalid right argument for operator "in", it expects a compile-time proper list or compile-time range on the right side when used in guard expressions, got: betstomkonidas
Can also do it like this
borybar
Thanks for your help, I really appreciate it!
If it’s not too much to ask I would be very grateful if you could explain a bit what you did, as I am having hard time understanding it
tomkonidas
Sure, so from the top:
Here we pattern match on the bets inside a game, we could also just done
game.betsinside the if statement.Inside the function, we have a simple if condition, If we find the
betitem inside the passed in game’s bets list, then we use the shorthand update syntax for a map.We also prepend the bet to the user_bets using the list shorthand syntax, This will give us a new list with the bet at the beginning of the list. It is more efficient to put a new element at the beginning of a list, rather than append to the end, appending to the end requires you to traverse the whole list.
If the
Ifcondition is not true (bet is not found in bets) then we return the game with no modificationsborybar
You are the best!
Now I understand what your code meant and will probably be able to do it on my own next time! That’s so helpful!
THANKS!!!