borybar

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.

Showing Posts 1 to 10

tomkonidas

tomkonidas

Map.take/2 should do what you need

borybar

borybar OP

So you suggest that I change my list to a map and then use Map.take/2?

tomkonidas

tomkonidas

Sorry I misread the original post.

Maybe I would do something like this. (it is not the prettiest, but it should work)

index = Enum.find_index(user_cards.bets, &(&1 == :some_term))
{user_bet, bets} = List.pop_at(user_cards.bets, index)
user_bets = [user_bet | user_bets]

%UserCards{user_cards | bets: bets, user_bets: user_bets}
borybar

borybar OP

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 this bets element and inserts it to user_bets.

Something like this:

`def select_bet(bet) do
    finds bet in %WarGame.Core.Game{}.bets
    inserts it to %WarGame.Core.Game{}.user_bets
end`
tomkonidas

tomkonidas

In this case, (I thought you also wanted to remove it from the bets)

def select_bet(%Game{bets: bets} = game, bet) when bet in bets do
    %{game | user_bets: [bet | game.user_bets]}
end

def select_bet(game, _bet), do: game

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

borybar OP

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: bets

tomkonidas

tomkonidas

Can also do it like this

def select_bet(%Game{bets: bets} = game, bet) do
  if bet in bets do
    %{game | user_bets: [bet | game.user_bets]}
  else
    game
  end
end
borybar

borybar OP

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 :slight_smile:

tomkonidas

tomkonidas

Sure, so from the top:
Here we pattern match on the bets inside a game, we could also just done game.bets inside the if statement.

def select_bet(%Game{bets: bets} = game, bet) do

Inside the function, we have a simple if condition, If we find the bet item inside the passed in game’s bets list, then we use the shorthand update syntax for a map.

 %{map| key: new_value}

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.

[bet | game.user_bets]

If the If condition is not true (bet is not found in bets) then we return the game with no modifications

borybar

borybar OP

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

Where Next? Top

Trending in Questions Top

Hellowman
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
yarrichar
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
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
mandar1045
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
rlipscombe
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
saleyn
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 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...
261 13143 112
New
dominicletz
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
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
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
OndrejValenta
(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
kip
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews