mirror of
https://github.com/thma/LtuPatternFactory.git
synced 2024-12-02 08:33:20 +03:00
Add code for TemplateMethod example
This commit is contained in:
parent
42e10eb4d7
commit
0cd89d7169
@ -591,7 +591,9 @@ addMinutesToWallTime x (WallTime (h, m)) =
|
|||||||
hNew = h + hAdd
|
hNew = h + hAdd
|
||||||
mNew = m + mAdd
|
mNew = m + mAdd
|
||||||
in if mNew >= 60
|
in if mNew >= 60
|
||||||
then WallTime ((hNew + 1) `rem` 24, mNew-60)
|
then
|
||||||
|
let (dnew, hnew') = (hNew + 1) `quotRem` 24
|
||||||
|
in WallTime (24*dnew + hnew', mNew-60)
|
||||||
else WallTime (hNew, mNew)
|
else WallTime (hNew, mNew)
|
||||||
|
|
||||||
-- this is our time representation in Minutes that we want to use in the frontend
|
-- this is our time representation in Minutes that we want to use in the frontend
|
||||||
|
@ -22,7 +22,9 @@ addMinutesToWallTime x (WallTime (h, m)) =
|
|||||||
hNew = h + hAdd
|
hNew = h + hAdd
|
||||||
mNew = m + mAdd
|
mNew = m + mAdd
|
||||||
in if mNew >= 60
|
in if mNew >= 60
|
||||||
then WallTime ((hNew + 1) `rem` 24, mNew-60)
|
then
|
||||||
|
let (dnew, hnew') = (hNew + 1) `quotRem` 24
|
||||||
|
in WallTime (24*dnew + hnew', mNew-60)
|
||||||
else WallTime (hNew, mNew)
|
else WallTime (hNew, mNew)
|
||||||
|
|
||||||
-- this is our time representation in Minutes that we want to use in the frontend
|
-- this is our time representation in Minutes that we want to use in the frontend
|
||||||
|
@ -7,6 +7,7 @@ import Composite
|
|||||||
import Visitor
|
import Visitor
|
||||||
import Adapter
|
import Adapter
|
||||||
import Builder
|
import Builder
|
||||||
|
import TemplateMethod
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
@ -18,4 +19,5 @@ main = do
|
|||||||
visitorDemo
|
visitorDemo
|
||||||
adapterDemo
|
adapterDemo
|
||||||
builderDemo
|
builderDemo
|
||||||
|
templateMethodDemo
|
||||||
|
|
||||||
|
34
src/TemplateMethod.hs
Normal file
34
src/TemplateMethod.hs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
module TemplateMethod where
|
||||||
|
|
||||||
|
import Adapter (unmarshalWM, marshalMW, addMinutesToWallTime, Minute (..), WallTime (..) )
|
||||||
|
|
||||||
|
addMinutesTemplate :: (Int -> WallTime -> WallTime) -> Int -> Minute -> Minute
|
||||||
|
addMinutesTemplate tf x =
|
||||||
|
unmarshalWM .
|
||||||
|
tf x .
|
||||||
|
marshalMW
|
||||||
|
|
||||||
|
-- implements linear addition even for values > 1440
|
||||||
|
linearTimeAdd :: Int -> Minute -> Minute
|
||||||
|
linearTimeAdd = addMinutesTemplate addMinutesToWallTime
|
||||||
|
|
||||||
|
-- implements cyclic addition, respecting a 24 hour (1440 Min) cycle
|
||||||
|
cyclicTimeAdd :: Int -> Minute -> Minute
|
||||||
|
cyclicTimeAdd = addMinutesTemplate addMinutesToWallTime'
|
||||||
|
|
||||||
|
|
||||||
|
-- a 24 hour (1440 min) cyclic version of addition: 1400 + 100 = 60
|
||||||
|
addMinutesToWallTime' :: Int -> WallTime -> WallTime
|
||||||
|
addMinutesToWallTime' x (WallTime (h, m)) =
|
||||||
|
let (hAdd, mAdd) = x `quotRem` 60
|
||||||
|
hNew = h + hAdd
|
||||||
|
mNew = m + mAdd
|
||||||
|
in if mNew >= 60
|
||||||
|
then WallTime ((hNew + 1) `rem` 24, mNew-60)
|
||||||
|
else WallTime (hNew, mNew)
|
||||||
|
|
||||||
|
templateMethodDemo = do
|
||||||
|
putStrLn "TemplateMethod -> higher order function -> typeclass default implementations"
|
||||||
|
putStrLn $ "linear time: " ++ (show $ linearTimeAdd 100 (Minute 1400))
|
||||||
|
putStrLn $ "cyclic time: " ++ (show $ cyclicTimeAdd 100 (Minute 1400))
|
||||||
|
putStrLn ""
|
Loading…
Reference in New Issue
Block a user