packages feed

IntervalMap-0.2.1 / examples / Example.hs

import Data.IntervalMap as IMtype Person = Stringtype Details = String-- For readability, represent timestamps as strings. Also, to keep it shorter,-- we will only use time of the day in this example.type Time = Stringtype TimeSpan = Interval Time-- We have a time span include its start time but not its end time.mkTimeSpan :: Time -> Time -> TimeSpanmkTimeSpan from to = IntervalCO from totype Appointments = IM.IntervalMap Time [(Person, Details)]noAppointments :: AppointmentsnoAppointments = IM.emptyaddAppointment :: Person -> Time -> Time -> Details -> Appointments -> AppointmentsaddAppointment who from to what apps = IM.insertWith (++) (mkTimeSpan from to) [(who, what)] appssampleApps :: AppointmentssampleApps = addAppointment "Paul" "09:00" "11:00" "Dentist" $             addAppointment "John" "10:00" "11:30" "Meeting" $             addAppointment "Rosy" "10:00" "11:30" "Shopping" $             addAppointment "Lisa" "08:45" "09:15" "Bank" $             noAppointmentsappointmentsAt :: Time -> Appointments -> [(TimeSpan, Person, Details)]appointmentsAt t apps = [ (ts, p, d) | (ts, ps) <- hits, (p,d) <- ps ]  where    hits :: [(TimeSpan, [(Person, Details)])]    hits = apps `containing` tappointmentsDuring :: Time -> Time -> Appointments -> [(TimeSpan, Person, Details)]appointmentsDuring from to apps = [ (ts, p, d) | (ts, ps) <- hits, (p,d) <- ps ]  where    hits :: [(TimeSpan, [(Person, Details)])]    hits =  apps `intersecting` mkTimeSpan from tomain :: IO ()main = do putStrLn (show (appointmentsAt "09:00" sampleApps))          putStrLn (show (appointmentsDuring "09:30" "10:30" sampleApps))	  putStrLn (show (IM.toAscList sampleApps))