Skip to content

Instantly share code, notes, and snippets.

@codeincontext
Created September 26, 2013 01:35
Show Gist options
  • Save codeincontext/6708684 to your computer and use it in GitHub Desktop.
Save codeincontext/6708684 to your computer and use it in GitHub Desktop.
Look how easy it is to build a deck of cards in Elixir! Example shamelessly taken from "Programming Elixir", which you should definitely buy
import Enum
# build a deck of cards
deck = lc rank inlist '23456789TJQKA', suit inlist 'CDHS', do: [suit, rank]
# -> ['C2', 'D2', 'H2', 'S2', 'C3', 'D3', ... ]
# take 5 random cards from the deck
hand = deck |> shuffle |> take(5)
# -> ['D8', 'CQ', 'H2', 'H3', 'HK']
# split the deck into hands of 5
hands = deck |> shuffle |> chunks(5)
# -> [['DQ', 'S6', 'HJ', 'H4', 'C7'], ['D6', 'SJ', 'S9', 'D7', 'HA'],
# ['S4', 'C2', 'CT', 'C8', 'C3'], ['D3', 'C5', 'C6', 'C4', 'SQ'],
# ['S2', 'SA', 'DJ', 'D8', 'SK'], ['H5', 'ST', 'CK', 'D5', 'H2'],
# ['CA', 'DT', 'HQ', 'CQ', 'S7'], ['HT', 'S3', 'H7', 'H6', 'H9'],
# ['H8', 'D9', 'C9', 'HK', 'S8'], ['H3', 'D2', 'DA', 'CJ', 'S5']]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment