From eea84ecb42ba097a19c50d42846b5b95cc739544 Mon Sep 17 00:00:00 2001 From: Tomas Carnecky Date: Tue, 10 Nov 2015 09:07:57 +0100 Subject: [PATCH 1/3] Add purescript-generics dependency --- bower.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bower.json b/bower.json index 499acf0..425796b 100644 --- a/bower.json +++ b/bower.json @@ -26,6 +26,7 @@ "dependencies": { "purescript-enums": "^0.7.0", "purescript-functions": "^0.1.0", - "purescript-globals": "^0.2.0" + "purescript-globals": "^0.2.0", + "purescript-generics": "~0.6.2" } } From d33c20eebf7472b12814b72b24c2fdf0aa3f4169 Mon Sep 17 00:00:00 2001 From: Tomas Carnecky Date: Thu, 26 Nov 2015 16:33:19 +0100 Subject: [PATCH 2/3] Derive Generic instances --- src/Data/Date.purs | 8 ++++++++ src/Data/Time.purs | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Data/Date.purs b/src/Data/Date.purs index 10daa89..8d5680a 100644 --- a/src/Data/Date.purs +++ b/src/Data/Date.purs @@ -20,11 +20,13 @@ module Data.Date import Prelude +import Control.Monad import Control.Monad.Eff import Data.Enum (Enum, Cardinality(..), fromEnum, defaultSucc, defaultPred) import Data.Function (on, Fn2(), runFn2, Fn3(), runFn3) import Data.Maybe (Maybe(..)) import Data.Time +import Data.Generic -- | A native JavaScript `Date` object. foreign import data JSDate :: * @@ -36,6 +38,12 @@ foreign import data JSDate :: * -- | modules. newtype Date = DateTime JSDate +instance genericDate :: Generic Date where + toSpine d = SProd "Date" [const (toSpine $ toEpochMilliseconds d)] + toSignature _ = SigProd [{ sigConstructor: "Date", sigValues: [const $ toSignature (anyProxy :: Proxy Milliseconds)] }] + fromSpine (SProd "Date" [msf]) = fromSpine (msf unit) >>= fromEpochMilliseconds + fromSpine _ = Nothing + instance eqDate :: Eq Date where eq = eq `on` toEpochMilliseconds diff --git a/src/Data/Time.purs b/src/Data/Time.purs index dbc6754..db64d2d 100644 --- a/src/Data/Time.purs +++ b/src/Data/Time.purs @@ -18,10 +18,14 @@ import Prelude , compare , show ) +import Data.Generic + -- | An hour component from a time value. Should fall between 0 and 23 -- | inclusive. newtype HourOfDay = HourOfDay Int +derive instance genericHourOfDay :: Generic HourOfDay + instance eqHourOfDay :: Eq HourOfDay where eq (HourOfDay x) (HourOfDay y) = x == y @@ -31,6 +35,8 @@ instance ordHourOfDay :: Ord HourOfDay where -- | A quantity of hours (not necessarily a value between 0 and 23). newtype Hours = Hours Number +derive instance genericHours :: Generic Hours + instance eqHours :: Eq Hours where eq (Hours x) (Hours y) = x == y @@ -61,6 +67,8 @@ instance showHours :: Show Hours where -- | inclusive. newtype MinuteOfHour = MinuteOfHour Int +derive instance genericMinuteOfHour :: Generic MinuteOfHour + instance eqMinuteOfHour :: Eq MinuteOfHour where eq (MinuteOfHour x) (MinuteOfHour y) = x == y @@ -70,6 +78,8 @@ instance ordMinuteOfHour :: Ord MinuteOfHour where -- | A quantity of minutes (not necessarily a value between 0 and 60). newtype Minutes = Minutes Number +derive instance genericMinutes :: Generic Minutes + instance eqMinutes :: Eq Minutes where eq (Minutes x) (Minutes y) = x == y @@ -100,6 +110,8 @@ instance showMinutes :: Show Minutes where -- | inclusive. newtype SecondOfMinute = SecondOfMinute Int +derive instance genericSecondOfMinute :: Generic SecondOfMinute + instance eqSecondOfMinute :: Eq SecondOfMinute where eq (SecondOfMinute x) (SecondOfMinute y) = x == y @@ -109,6 +121,8 @@ instance ordSecondOfMinute :: Ord SecondOfMinute where -- | A quantity of seconds (not necessarily a value between 0 and 60). newtype Seconds = Seconds Number +derive instance genericSeconds :: Generic Seconds + instance eqSeconds :: Eq Seconds where eq (Seconds x) (Seconds y) = x == y @@ -139,6 +153,8 @@ instance showSeconds :: Show Seconds where -- | inclusive. newtype MillisecondOfSecond = MillisecondOfSecond Int +derive instance genericMillisecondOfSecond :: Generic MillisecondOfSecond + instance eqMillisecondOfSecond :: Eq MillisecondOfSecond where eq (MillisecondOfSecond x) (MillisecondOfSecond y) = x == y @@ -148,6 +164,8 @@ instance ordMillisecondOfSecond :: Ord MillisecondOfSecond where -- | A quantity of milliseconds (not necessarily a value between 0 and 1000). newtype Milliseconds = Milliseconds Number +derive instance genericMilliseconds :: Generic Milliseconds + instance eqMilliseconds :: Eq Milliseconds where eq (Milliseconds x) (Milliseconds y) = x == y From 15304ed7cb61d53e872628d35707274407449e41 Mon Sep 17 00:00:00 2001 From: Tomas Carnecky Date: Tue, 1 Dec 2015 13:15:25 +0100 Subject: [PATCH 3/3] Bump purescript-generics dependency to 0.7.0 --- bower.json | 2 +- src/Data/Date.purs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/bower.json b/bower.json index 425796b..959fd27 100644 --- a/bower.json +++ b/bower.json @@ -27,6 +27,6 @@ "purescript-enums": "^0.7.0", "purescript-functions": "^0.1.0", "purescript-globals": "^0.2.0", - "purescript-generics": "~0.6.2" + "purescript-generics": "^0.7.0" } } diff --git a/src/Data/Date.purs b/src/Data/Date.purs index 8d5680a..3211b30 100644 --- a/src/Data/Date.purs +++ b/src/Data/Date.purs @@ -20,7 +20,8 @@ module Data.Date import Prelude -import Control.Monad +import Type.Proxy + import Control.Monad.Eff import Data.Enum (Enum, Cardinality(..), fromEnum, defaultSucc, defaultPred) import Data.Function (on, Fn2(), runFn2, Fn3(), runFn3) @@ -40,7 +41,7 @@ newtype Date = DateTime JSDate instance genericDate :: Generic Date where toSpine d = SProd "Date" [const (toSpine $ toEpochMilliseconds d)] - toSignature _ = SigProd [{ sigConstructor: "Date", sigValues: [const $ toSignature (anyProxy :: Proxy Milliseconds)] }] + toSignature _ = SigProd "Date" [{ sigConstructor: "Date", sigValues: [const $ toSignature (Proxy :: Proxy Milliseconds)] }] fromSpine (SProd "Date" [msf]) = fromSpine (msf unit) >>= fromEpochMilliseconds fromSpine _ = Nothing