Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit cd2347d

Browse files
committed
Add NonEmpty instance
1 parent e2c119d commit cd2347d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/Data/Generic.purs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import Data.Array (null, length, sortBy, zipWith)
2020
import Data.Either (Either(..))
2121
import Data.Foldable (all, and, find, fold, intercalate)
2222
import Data.Maybe (Maybe(..))
23+
import Data.NonEmpty (NonEmpty(..))
2324
import Data.String (joinWith)
2425
import Data.Traversable (traverse)
2526
import Data.Tuple (Tuple(..))
27+
2628
import Type.Proxy (Proxy(..))
2729

2830
-- | The Generic typeclass provides methods for sending data to/from spine
@@ -164,6 +166,28 @@ instance genericOrdering :: Generic Ordering where
164166
SProd "Data.Ordering.GT" [] -> Just GT
165167
_ -> Nothing
166168

169+
instance genericNonEmpty :: (Generic (f a), Generic a) => Generic (NonEmpty f a) where
170+
toSpine (NonEmpty x xs) =
171+
SProd "Data.NonEmpty.NonEmpty" [\_ -> toSpine x, \_ -> toSpine xs]
172+
toSignature x =
173+
SigProd
174+
"Data.NonEmpty.NonEmpty"
175+
[ { sigConstructor: "Data.NonEmpty.NonEmpty"
176+
, sigValues:
177+
[ \_ -> toSignature (headProxy x)
178+
, \_ -> toSignature (tailProxy x)
179+
]
180+
}
181+
]
182+
where
183+
headProxy :: Proxy (NonEmpty f a) -> Proxy a
184+
headProxy _ = Proxy
185+
tailProxy :: Proxy (NonEmpty f a) -> Proxy (f a)
186+
tailProxy _ = Proxy
187+
fromSpine (SProd "Data.NonEmpty.NonEmpty" [x, xs]) =
188+
NonEmpty <$> fromSpine (force x) <*> fromSpine (force xs)
189+
fromSpine _ = Nothing
190+
167191
-- | A GenericSpine is a universal representation of an arbitrary data
168192
-- | structure (that does not contain function arrows).
169193
data GenericSpine

0 commit comments

Comments
 (0)