Skip to content

Instantly share code, notes, and snippets.

@leifmetcalf
Created April 13, 2019 07:30
Show Gist options
  • Save leifmetcalf/f9600ffabcdb73dd19b3c3655ca58150 to your computer and use it in GitHub Desktop.
Save leifmetcalf/f9600ffabcdb73dd19b3c3655ca58150 to your computer and use it in GitHub Desktop.
module Supermarket where
import Data.List
import Control.Arrow
-- Some type synonyms to clean up type signatures
type User = String
type History = [[String]]
type Map key value = [(key, value)]
-- Your dataset
users = ["user1", "user4"]
items = ["Banana", "Orange", "Kiwi", "Apple", "Pineapple", "Lemon"]
purchasesHistory =
[ ("user1", [ ["Banana","Orange","Kiwi"]
, ["Banana","Orange","Apple"] ])
, ("user4", [])]
-- Helper functions
freqList :: Ord a => [a] -> Map a Int
freqList = fmap (head &&& length) . group . sort
deleteAll :: Eq a => a -> [a] -> [a]
deleteAll x = filter (/= x)
-- Is this close to what you want to do?
getStats :: Map User History -> Map User (Map String Int)
getStats = fmap fmap fmap (fmap freqList concat)
-- This function is so you don't have to deal with `Maybe`
getCount :: Eq a => a -> Map a Int -> Int
getCount item map = case lookup item map of
Just n -> n
Nothing -> 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment