grids 0.1.0.0 → 0.1.1.0
raw patch · 3 files changed
+33/−31 lines, 3 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Data.Grid: chunkVector :: forall n a. KnownNat n => Proxy n -> Vector a -> [Vector a]
- Data.Grid: fromCoord' :: Dims -> [Int] -> Int
- Data.Grid: fromFinite :: Num n => Finite m -> n
- Data.Grid: sizeof :: forall (dims :: [Nat]). KnownNat (GridSize dims) => Proxy dims -> Int
- Data.Grid: toCoord' :: Dims -> Int -> [Int]
- Data.Grid: toFinite :: KnownNat n => Integral m => m -> Finite n
- Data.Grid: toNestedLists :: forall dims a. NestLists dims => Grid dims a -> NestedLists dims a
- Data.Grid: type Dims = [Int]
- Data.Grid: type NumericConstraints dims = (KnownNat (GridSize dims))
+ Data.Grid: gridSize :: forall (dims :: [Nat]). KnownNat (GridSize dims) => Proxy dims -> Int
- Data.Grid: class (NumericConstraints dims, KnownNat (GridSize dims)) => Dimensions (dims :: [Nat])
+ Data.Grid: class (KnownNat (GridSize dims)) => Dimensions (dims :: [Nat])
- Data.Grid: type family NestedLists (dims :: [Nat]) a
+ Data.Grid: type family Coord (dims :: [Nat])
Files
- README.md +7/−4
- grids.cabal +2/−2
- src/Data/Grid.hs +24/−25
README.md view
@@ -1,8 +1,11 @@ # Grids Grids can have an arbitrary amount of dimensions, specified by a type-level-list of `Nat`s, here's how we might represent a Tic-Tac-Toe board:+list of `Nat`s. They're backed by a single contiguous Vector and gain the associated performance benefits. Currently+only boxed immutable vectors are supported, but let me know if you need other variants. +Here's how we might represent a Tic-Tac-Toe board:+ ```haskell data Piece = X | O deriving Show toPiece n = if even n then X@@ -76,11 +79,11 @@ λ> g (Grid [[0,1,2] ,[3,4,5]])-λ> g `R.index` (1 :# 1)+λ> g `index` (1 :# 1) 4-λ> g `R.index` (1 :# 0)+λ> g `index` (1 :# 0) 3-λ> g `R.index` (0 :# 2)+λ> g `index` (0 :# 2) 2 ```
grids.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: 934ce335991599a57471bf99e8fd952b854e86050c0098f06acf32c8430ee1a8+-- hash: a9762c7345dd2ce0dbbd1c65d6e6529407c552773f52e506e39795e5ca9b3793 name: grids-version: 0.1.0.0+version: 0.1.1.0 description: Arbitrary sized type-safe grids with useful combinators category: Data Structures homepage: https://github.com/ChrisPenner/grids#readme
src/Data/Grid.hs view
@@ -14,7 +14,20 @@ {-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE UndecidableSuperClasses #-} -module Data.Grid where+module Data.Grid (+ Grid(..)+ , GridSize+ , Coord+ , (:#)(..)+ , gridSize+ , Dimensions(..)+ , generate+ , NestLists(..)+ , UnNestLists(..)+ , fromNestedLists+ , fromList+ , (//)+ ) where import Data.Distributive import Data.Functor.Rep@@ -48,7 +61,6 @@ liftA2 f (Grid v) (Grid u) = Grid $ V.zipWith f v u type family GridSize dims :: Nat where- GridSize '[] = 0 GridSize (x:'[]) = x GridSize (x:xs) = (x N.* GridSize xs) @@ -61,14 +73,11 @@ Coord '[n] = Finite n Coord (n:xs) = Finite n :# Coord xs -sizeof :: forall (dims :: [Nat]) . KnownNat (GridSize dims) => Proxy dims -> Int-sizeof _ = fromIntegral (L.natVal (Proxy @(GridSize dims)))--type NumericConstraints dims = (KnownNat (GridSize dims))--type Dims = [Int]+gridSize+ :: forall (dims :: [Nat]) . KnownNat (GridSize dims) => Proxy dims -> Int+gridSize _ = fromIntegral (L.natVal (Proxy @(GridSize dims))) -class (NumericConstraints dims, KnownNat (GridSize dims)) => Dimensions (dims :: [Nat]) where+class (KnownNat (GridSize dims)) => Dimensions (dims :: [Nat]) where toCoord :: Proxy dims -> Finite (GridSize dims) -> Coord dims fromCoord :: Proxy dims -> Coord dims -> Finite (GridSize dims) @@ -76,16 +85,6 @@ toCoord _ i = i fromCoord _ i = i -toCoord' :: Dims -> Int -> [Int]-toCoord' [] _ = []-toCoord' [_ ] n = [n]-toCoord' (_ : ds) n = (n `div` product ds) : toCoord' ds (n `mod` product ds)--fromCoord' :: Dims -> [Int] -> Int-fromCoord' _ [] = 1-fromCoord' _ [c ] = c-fromCoord' (_ : ds) (c : cs) = c * product ds + fromCoord' ds cs- toFinite :: (KnownNat n) => Integral m => m -> Finite n toFinite = finite . fromIntegral @@ -95,12 +94,12 @@ instance (KnownNat (x N.* GridSize (y:xs)), KnownNat x, Dimensions (y:xs)) => Dimensions (x:y:xs) where toCoord _ n = firstCoord :# toCoord (Proxy @(y:xs)) remainder where- firstCoord = toFinite (n `div` fromIntegral (sizeof (Proxy @(y:xs))))- remainder = toFinite (fromFinite n `mod` sizeof (Proxy @(y:xs)))+ firstCoord = toFinite (n `div` fromIntegral (gridSize (Proxy @(y:xs))))+ remainder = toFinite (fromFinite n `mod` gridSize (Proxy @(y:xs))) fromCoord _ (x :# ys) = toFinite $ firstPart + rest where- firstPart = fromFinite x * sizeof (Proxy @(y:xs))+ firstPart = fromFinite x * gridSize (Proxy @(y:xs)) rest = fromFinite (fromCoord (Proxy @(y:xs)) ys) instance (Dimensions dims) => Distributive (Grid dims) where@@ -109,7 +108,7 @@ instance (Dimensions dims) => Representable (Grid dims) where type Rep (Grid dims) = Coord dims index (Grid v) ind = v V.! fromIntegral (fromCoord (Proxy @dims) ind)- tabulate f = Grid $ V.generate (fromIntegral $ sizeof (Proxy @dims)) (f . toCoord (Proxy @dims) . fromIntegral)+ tabulate f = Grid $ V.generate (fromIntegral $ gridSize (Proxy @dims)) (f . toCoord (Proxy @dims) . fromIntegral) instance (Dimensions dims, ind ~ Coord dims) => FunctorWithIndex ind (Grid dims) where@@ -124,7 +123,7 @@ itraverse = itraverseRep generate :: forall dims a . Dimensions dims => (Int -> a) -> Grid dims a-generate f = Grid $ V.generate (sizeof (Proxy @dims)) f+generate f = Grid $ V.generate (gridSize (Proxy @dims)) f type family NestedLists (dims :: [Nat]) a where NestedLists '[] a = a@@ -170,7 +169,7 @@ fromList :: forall a dims . (Dimensions dims) => [a] -> Maybe (Grid dims a) fromList xs = let v = V.fromList xs- in if V.length v == sizeof (Proxy @dims) then Just $ Grid v else Nothing+ in if V.length v == gridSize (Proxy @dims) then Just $ Grid v else Nothing (//) :: forall dims a