diff --git a/bower.json b/bower.json index fc16fc9..66db52d 100644 --- a/bower.json +++ b/bower.json @@ -16,14 +16,14 @@ "package.json" ], "dependencies": { - "purescript-enums": "^1.0.0", - "purescript-functions": "^1.0.0", - "purescript-generics": "^1.0.0", - "purescript-integers": "^1.0.0", + "purescript-enums": "^2.0.0", + "purescript-functions": "^2.0.0", + "purescript-generics": "^3.0.0", + "purescript-integers": "^2.0.0", "purescript-math": "^2.0.0" }, "devDependencies": { - "purescript-assert": "^1.0.0", - "purescript-console": "^1.0.0" + "purescript-assert": "^2.0.0", + "purescript-console": "^2.0.0" } } diff --git a/src/Data/Date/Component.purs b/src/Data/Date/Component.purs index e6bde33..2051d6a 100644 --- a/src/Data/Date/Component.purs +++ b/src/Data/Date/Component.purs @@ -18,8 +18,8 @@ import Data.Maybe (Maybe(..)) -- | acquire a year value from an integer. newtype Year = Year Int -derive instance eqYear :: Eq Year -derive instance ordYear :: Ord Year +derive newtype instance eqYear :: Eq Year +derive newtype instance ordYear :: Ord Year derive instance genericYear :: Generic Year -- Note: these seemingly arbitrary bounds come from relying on JS for date @@ -122,8 +122,8 @@ instance showMonth :: Show Month where -- | acquire a day value from an integer. newtype Day = Day Int -derive instance eqDay :: Eq Day -derive instance ordDay :: Ord Day +derive newtype instance eqDay :: Eq Day +derive newtype instance ordDay :: Ord Day derive instance genericDay :: Generic Day instance boundedDay :: Bounded Day where diff --git a/src/Data/DateTime/Instant.purs b/src/Data/DateTime/Instant.purs index 6a6944d..1f59a37 100644 --- a/src/Data/DateTime/Instant.purs +++ b/src/Data/DateTime/Instant.purs @@ -24,8 +24,8 @@ import Partial.Unsafe (unsafePartial) -- | `DateTime` type. newtype Instant = Instant Milliseconds -derive instance eqDateTime :: Eq Instant -derive instance ordDateTime :: Ord Instant +derive newtype instance eqDateTime :: Eq Instant +derive newtype instance ordDateTime :: Ord Instant derive instance genericDateTime :: Generic Instant instance boundedInstant :: Bounded Instant where diff --git a/src/Data/DateTime/Locale.purs b/src/Data/DateTime/Locale.purs index 157d0f3..4ad7701 100644 --- a/src/Data/DateTime/Locale.purs +++ b/src/Data/DateTime/Locale.purs @@ -7,6 +7,7 @@ import Control.Comonad (class Comonad, class Extend) import Data.DateTime (Date, Time, DateTime) import Data.Generic (class Generic) import Data.Maybe (Maybe) +import Data.Newtype (class Newtype) import Data.Time.Duration (Minutes) -- | A date/time locale specifying an offset in minutes and an optional name for @@ -23,8 +24,9 @@ instance showLocale :: Show Locale where -- | The name of a date/time locale. For example: "GMT", "MDT", "CET", etc. newtype LocaleName = LocaleName String -derive instance eqLocaleName :: Eq LocaleName -derive instance ordLocaleName :: Ord LocaleName +derive instance newtypeLocaleName :: Newtype LocaleName _ +derive newtype instance eqLocaleName :: Eq LocaleName +derive newtype instance ordLocaleName :: Ord LocaleName derive instance genericLocaleName :: Generic LocaleName instance showLocaleName :: Show LocaleName where diff --git a/src/Data/Time.purs b/src/Data/Time.purs index 4ed9eaa..190c4d6 100644 --- a/src/Data/Time.purs +++ b/src/Data/Time.purs @@ -15,8 +15,9 @@ import Data.Enum (fromEnum, toEnum) import Data.Generic (class Generic) import Data.Int as Int import Data.Maybe (fromJust) +import Data.Newtype (unwrap) import Data.Time.Component (Hour, Millisecond, Minute, Second) -import Data.Time.Duration (class Duration, Days(..), Milliseconds(..), unMilliseconds, fromDuration, toDuration) +import Data.Time.Duration (class Duration, Days(..), Milliseconds(..), fromDuration, toDuration) import Data.Tuple (Tuple(..)) import Math as Math @@ -80,7 +81,7 @@ adjust d t = d' = fromDuration d tLength = timeToMillis t dayLength = 86400000.0 - wholeDays = Days $ Math.floor (unMilliseconds d' / dayLength) + wholeDays = Days $ Math.floor (unwrap d' / dayLength) msAdjust = d' - fromDuration wholeDays msAdjusted = tLength + msAdjust wrap = if msAdjusted > maxTime then 1.0 else if msAdjusted < -maxTime then -1.0 else 0.0 @@ -100,9 +101,8 @@ timeToMillis t = Milliseconds + Int.toNumber (fromEnum (millisecond t)) millisToTime :: Milliseconds -> Time -millisToTime ms = +millisToTime ms@(Milliseconds ms') = let - ms' = unMilliseconds ms hourLength = 3600000.0 minuteLength = 60000.0 secondLength = 1000.0 diff --git a/src/Data/Time/Component.purs b/src/Data/Time/Component.purs index 879d0a7..345a592 100644 --- a/src/Data/Time/Component.purs +++ b/src/Data/Time/Component.purs @@ -19,8 +19,8 @@ import Data.Maybe (Maybe(..)) -- | lowered to a plain integer with the `fromEnum` function. newtype Hour = Hour Int -derive instance eqHour :: Eq Hour -derive instance ordHour :: Ord Hour +derive newtype instance eqHour :: Eq Hour +derive newtype instance ordHour :: Ord Hour derive instance genericHour :: Generic Hour instance boundedHour :: Bounded Hour where @@ -49,8 +49,8 @@ instance showHour :: Show Hour where -- | be lowered to a plain integer with the `fromEnum` function. newtype Minute = Minute Int -derive instance eqMinute :: Eq Minute -derive instance ordMinute :: Ord Minute +derive newtype instance eqMinute :: Eq Minute +derive newtype instance ordMinute :: Ord Minute derive instance genericMinute :: Generic Minute instance boundedMinute :: Bounded Minute where @@ -79,8 +79,8 @@ instance showMinute :: Show Minute where -- | be lowered to a plain integer with the `fromEnum` function. newtype Second = Second Int -derive instance eqSecond :: Eq Second -derive instance ordSecond :: Ord Second +derive newtype instance eqSecond :: Eq Second +derive newtype instance ordSecond :: Ord Second derive instance genericSecond :: Generic Second instance boundedSecond :: Bounded Second where @@ -110,8 +110,8 @@ instance showSecond :: Show Second where -- | function. newtype Millisecond = Millisecond Int -derive instance eqMillisecond :: Eq Millisecond -derive instance ordMillisecond :: Ord Millisecond +derive newtype instance eqMillisecond :: Eq Millisecond +derive newtype instance ordMillisecond :: Ord Millisecond derive instance genericMillisecond :: Generic Millisecond instance boundedMillisecond :: Bounded Millisecond where diff --git a/src/Data/Time/Duration.purs b/src/Data/Time/Duration.purs index c14cb5c..ea0dcf6 100644 --- a/src/Data/Time/Duration.purs +++ b/src/Data/Time/Duration.purs @@ -3,25 +3,17 @@ module Data.Time.Duration where import Prelude import Data.Generic (class Generic) +import Data.Newtype (class Newtype, over) -- | A duration measured in milliseconds. newtype Milliseconds = Milliseconds Number -unMilliseconds :: Milliseconds -> Number -unMilliseconds (Milliseconds ms) = ms - -derive instance eqMilliseconds :: Eq Milliseconds -derive instance ordMilliseconds :: Ord Milliseconds +derive instance newtypeMilliseconds :: Newtype Milliseconds _ derive instance genericMilliseconds :: Generic Milliseconds - -instance semiringMilliseconds :: Semiring Milliseconds where - add (Milliseconds x) (Milliseconds y) = Milliseconds (x + y) - mul (Milliseconds x) (Milliseconds y) = Milliseconds (x * y) - zero = Milliseconds 0.0 - one = Milliseconds 1.0 - -instance ringMilliseconds :: Ring Milliseconds where - sub (Milliseconds x) (Milliseconds y) = Milliseconds (x - y) +derive newtype instance eqMilliseconds :: Eq Milliseconds +derive newtype instance ordMilliseconds :: Ord Milliseconds +derive newtype instance semiringMilliseconds :: Semiring Milliseconds +derive newtype instance ringMilliseconds :: Ring Milliseconds instance showMilliseconds :: Show Milliseconds where show (Milliseconds n) = "(Milliseconds " <> show n <> ")" @@ -29,21 +21,12 @@ instance showMilliseconds :: Show Milliseconds where -- | A duration measured in seconds. newtype Seconds = Seconds Number -unSeconds :: Seconds -> Number -unSeconds (Seconds s) = s - -derive instance eqSeconds :: Eq Seconds -derive instance ordSeconds :: Ord Seconds +derive instance newtypeSeconds :: Newtype Seconds _ derive instance genericSeconds :: Generic Seconds - -instance semiringSeconds :: Semiring Seconds where - add (Seconds x) (Seconds y) = Seconds (x + y) - mul (Seconds x) (Seconds y) = Seconds (x * y) - zero = Seconds 0.0 - one = Seconds 1.0 - -instance ringSeconds :: Ring Seconds where - sub (Seconds x) (Seconds y) = Seconds (x - y) +derive newtype instance eqSeconds :: Eq Seconds +derive newtype instance ordSeconds :: Ord Seconds +derive newtype instance semiringSeconds :: Semiring Seconds +derive newtype instance ringSeconds :: Ring Seconds instance showSeconds :: Show Seconds where show (Seconds n) = "(Seconds " <> show n <> ")" @@ -51,21 +34,12 @@ instance showSeconds :: Show Seconds where -- | A duration measured in minutes. newtype Minutes = Minutes Number -unMinutes :: Minutes -> Number -unMinutes (Minutes m) = m - -derive instance eqMinutes :: Eq Minutes -derive instance ordMinutes :: Ord Minutes +derive instance newtypeMinutes :: Newtype Minutes _ derive instance genericMinutes :: Generic Minutes - -instance semiringMinutes :: Semiring Minutes where - add (Minutes x) (Minutes y) = Minutes (x + y) - mul (Minutes x) (Minutes y) = Minutes (x * y) - zero = Minutes 0.0 - one = Minutes 1.0 - -instance ringMinutes :: Ring Minutes where - sub (Minutes x) (Minutes y) = Minutes (x - y) +derive newtype instance eqMinutes :: Eq Minutes +derive newtype instance ordMinutes :: Ord Minutes +derive newtype instance semiringMinutes :: Semiring Minutes +derive newtype instance ringMinutes :: Ring Minutes instance showMinutes :: Show Minutes where show (Minutes n) = "(Minutes " <> show n <> ")" @@ -73,21 +47,12 @@ instance showMinutes :: Show Minutes where -- | A duration measured in hours. newtype Hours = Hours Number -unHours :: Hours -> Number -unHours (Hours m) = m - -derive instance eqHours :: Eq Hours -derive instance ordHours :: Ord Hours +derive instance newtypeHours :: Newtype Hours _ derive instance genericHours :: Generic Hours - -instance semiringHours :: Semiring Hours where - add (Hours x) (Hours y) = Hours (x + y) - mul (Hours x) (Hours y) = Hours (x * y) - zero = Hours 0.0 - one = Hours 1.0 - -instance ringHours :: Ring Hours where - sub (Hours x) (Hours y) = Hours (x - y) +derive newtype instance eqHours :: Eq Hours +derive newtype instance ordHours :: Ord Hours +derive newtype instance semiringHours :: Semiring Hours +derive newtype instance ringHours :: Ring Hours instance showHours :: Show Hours where show (Hours n) = "(Hours " <> show n <> ")" @@ -95,21 +60,12 @@ instance showHours :: Show Hours where -- | A duration measured in days, where a day is assumed to be exactly 24 hours. newtype Days = Days Number -unDays :: Days -> Number -unDays (Days m) = m - -derive instance eqDays :: Eq Days -derive instance ordDays :: Ord Days +derive instance newtypeDays :: Newtype Days _ derive instance genericDays :: Generic Days - -instance semiringDays :: Semiring Days where - add (Days x) (Days y) = Days (x + y) - mul (Days x) (Days y) = Days (x * y) - zero = Days 0.0 - one = Days 1.0 - -instance ringDays :: Ring Days where - sub (Days x) (Days y) = Days (x - y) +derive newtype instance eqDays :: Eq Days +derive newtype instance ordDays :: Ord Days +derive newtype instance semiringDays :: Semiring Days +derive newtype instance ringDays :: Ring Days instance showDays :: Show Days where show (Days n) = "(Days " <> show n <> ")" @@ -128,17 +84,17 @@ instance durationMilliseconds :: Duration Milliseconds where toDuration = id instance durationSeconds :: Duration Seconds where - fromDuration = Milliseconds <<< (_ * 1000.0) <<< unSeconds - toDuration (Milliseconds ms) = Seconds (ms / 1000.0) + fromDuration = over Seconds (_ * 1000.0) + toDuration = over Milliseconds (_ / 1000.0) instance durationMinutes :: Duration Minutes where - fromDuration = Milliseconds <<< (_ * 60000.0) <<< unMinutes - toDuration (Milliseconds ms) = Minutes (ms / 60000.0) + fromDuration = over Minutes (_ * 60000.0) + toDuration = over Milliseconds (_ / 60000.0) instance durationHours :: Duration Hours where - fromDuration = Milliseconds <<< (_ * 3600000.0) <<< unHours - toDuration (Milliseconds ms) = Hours (ms / 3600000.0) + fromDuration = over Hours (_ * 3600000.0) + toDuration = over Milliseconds (_ / 3600000.0) instance durationDays :: Duration Days where - fromDuration = Milliseconds <<< (_ * 86400000.0) <<< unDays - toDuration (Milliseconds ms) = Days (ms / 86400000.0) + fromDuration = over Days (_ * 86400000.0) + toDuration = over Milliseconds (_ / 86400000.0) diff --git a/test/Test/Main.purs b/test/Test/Main.purs index b67125a..a5a147b 100644 --- a/test/Test/Main.purs +++ b/test/Test/Main.purs @@ -5,7 +5,7 @@ import Prelude import Control.Monad.Eff (Eff) import Control.Monad.Eff.Console (CONSOLE, log) -import Data.Enum (class BoundedEnum, Cardinality, toEnum, enumFromTo, cardinality, runCardinality, succ, fromEnum, pred) +import Data.Enum (class BoundedEnum, Cardinality, toEnum, enumFromTo, cardinality, succ, fromEnum, pred) import Data.Date as Date import Data.Time as Time import Data.Time.Duration as Duration @@ -14,6 +14,7 @@ import Data.DateTime as DateTime import Data.DateTime.Instant as Instant import Data.Maybe (Maybe(..), fromJust) import Data.Tuple (Tuple(..), snd) +import Data.Newtype (unwrap) import Type.Proxy (Proxy(..)) import Test.Assert (ASSERT, assert) @@ -144,5 +145,5 @@ checkBounded _ = do checkBoundedEnum :: forall e. BoundedEnum e => Proxy e -> Tests checkBoundedEnum p = do checkBounded p - let card = runCardinality (cardinality :: Cardinality e) + let card = unwrap (cardinality :: Cardinality e) assert $ Array.length (enumFromTo bottom (top :: e)) == card