Skip to content

Instantly share code, notes, and snippets.

@toastal
Last active December 31, 2016 10:16
Show Gist options
  • Save toastal/739a16c21d9060e3c2d36eef2bf02e58 to your computer and use it in GitHub Desktop.
Save toastal/739a16c21d9060e3c2d36eef2bf02e58 to your computer and use it in GitHub Desktop.
Elm Card Stuff
import Html exposing (text)
import List.Extra as List
{-
List.andThen : (a -> List b) -> List a -> List b
List.andThen =
List.concatMap
List.lift2 : (a -> b -> c) -> List a -> List b -> List c
List.lift2 f la lb =
la |> List.andThen (\a -> lb |> List.andThen (\b -> [ f a b ]))
-}
type Suit
= Heart
| Diamond
| Club
| Spade
type Value
= Two
| Three
| Four
| Five
| Six
| Seven
| Eight
| Nine
| Ten
| Jack
| Queen
| King
| Ace
type Card
= Card Suit Value
deck : List Card
deck =
List.lift2 Card
[ Heart, Diamond, Club, Spade ]
[ Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace ]
suitCount : Suit -> List Card -> Int
suitCount suit =
List.foldl (\(Card s _) -> if s == suit then ((+) 1) else identity) 0
countSuits : List Card -> List ( Suit, Int )
countSuits cs =
List.map (\s -> ( s, suitCount s cs )) [ Heart, Diamond, Club, Spade ]
main : Html String
main =
text << toString <| countSuits deck
--=> [(Heart,13),(Diamond,13),(Club,13),(Spade,13)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment