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

Commit 552c4f3

Browse files
authored
Merge pull request #43 from purescript/nonempty
Update `-arrays` and add NonEmpty instance
2 parents ee3f1bb + cd2347d commit 552c4f3

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"package.json"
2121
],
2222
"dependencies": {
23-
"purescript-arrays": "^2.0.0",
23+
"purescript-arrays": "^3.0.0",
2424
"purescript-either": "^2.0.0",
2525
"purescript-proxy": "^1.0.0",
2626
"purescript-strings": "^2.0.0"

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)