Skip to content

Instantly share code, notes, and snippets.

@amitaibu
Last active March 1, 2016 19:59
Show Gist options
  • Save amitaibu/ba49f59662d8d37a87ac to your computer and use it in GitHub Desktop.
Save amitaibu/ba49f59662d8d37a87ac to your computer and use it in GitHub Desktop.
module Jammin where
import Data.List
data Fruit =
Peach
| Plum
| Apple
| Blackberry
deriving (Eq, Show, Ord)
data JamJars =
Jam { kind :: Fruit
, num :: Int }
deriving (Eq, Show)
row1 = Jam Peach 100
row2 = Jam Plum 1
row3 = Jam Apple 3
row4 = Jam Peach 2
row5 = Jam Peach 6
row6 = Jam Plum 4
allJam = [row1, row2, row3, row4, row5, row6]
getAllJarsNum :: [JamJars] -> [Int]
getAllJarsNum =
map num
sumAllJarsNum :: [JamJars] -> Int
sumAllJarsNum =
foldr ((+) . num) 0
maxJars :: [JamJars] -> JamJars
maxJars =
maximumBy comp
where comp x y = if (num x) > (num y) then GT else LT
sortJams :: [JamJars] -> [JamJars]
sortJams =
sortBy compareKind
compareKind :: JamJars -> JamJars -> Ordering
compareKind (Jam k _) (Jam k' _) = compare k k'
groupJams :: [JamJars] -> [[JamJars]]
groupJams =
(groupBy groupByKind) . sortJams
groupByKind :: JamJars -> JamJars -> Bool
groupByKind (Jam k _) (Jam k' _) = k == k'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment