Quick actions

cmd+k|ctrl+k

Navigation

Languages

Deck - double comprehension usage

Snippet info

Language

Elixir

Visibility

public

Author

jsonkody

Created

2024-09-18T13:09:38.834721Z

Updated

2024-09-18T14:05:10.400118Z

defmodule Deck do

    def cards do
        ranks = [ "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" ]
        suits = [ "♣", "♦", "♥", "♠" ]
  
        for rank <- ranks, suit <- suits, do: {rank, suit}
    end
    
    
    def random(num) do
        random(Deck.cards, num)
        |> Enum.shuffle
        |> Enum.take(num)
    end
    
    
    def random(cards, num) do
        cards
        |> Enum.shuffle
        |> Enum.take(num)
    end
    
    
    def hands(num_players, num_cards_hand) do
        Deck.random(num_players * num_cards_hand)
        |> Enum.chunk_every(num_cards_hand)
    end

end

IO.inspect Deck.hands 3, 5
INFO