yx 0.0.4.1 → 0.0.4.2
raw patch · 3 files changed
+23/−7 lines, 3 filesPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
API changes (from Hackage documentation)
+ Data.Geometry.YX: boxDepth :: Box -> YX -> Maybe Int
Files
- src/Data/Geometry/YX.hs +13/−5
- test/Spec.hs +8/−0
- yx.cabal +2/−2
src/Data/Geometry/YX.hs view
@@ -16,7 +16,7 @@ Box, box, arrayBox, boundingBox, boxBounds, topLeft, bottomRight, boxHeight, boxWidth,- inBox, boxRange, boxRows, boxIntersection,+ inBox, boxDepth, boxRange, boxRows, boxIntersection, boxNeighbors4, boxNeighbors8, -- * Transformations Center(..), Direction(..), rotate,@@ -40,8 +40,6 @@ -- | A 2D coordinate. ----- YX implements 'Num'. Integers are converted to their diagonal equivalent (for example @2@ becomes--- @YX 2 2@). data YX = YX { y :: !Int, x :: !Int } deriving (Eq, Ord, Show) lift1 :: (Int -> Int) -> YX -> YX@@ -50,6 +48,7 @@ lift2 :: (Int -> Int -> Int) -> YX -> YX -> YX lift2 f (YX y1 x1) (YX y2 x2) = YX (f y1 y2) (f x1 x2) +-- | Integers are converted to their diagonal equivalent (for example @2@ becomes @YX 2 2@). instance Num YX where (+) = lift2 (+) (*) = lift2 (*)@@ -154,6 +153,15 @@ inBox :: YX -> Box -> Bool inBox yx (Box tl br) = joinLeq yx br && meetLeq tl yx +-- | Returns the shortest distance of the point to the box' edge, or 'Nothing' if the point is not+-- within the box.+--+-- @since 0.0.4.2+boxDepth :: Box -> YX -> Maybe Int+boxDepth b@(Box (YX y0 x0) (YX y1 x1)) yx@(YX y x) = if yx `inBox` b+ then Just $ minimum [y - y0, y1 - y, x - x0, x1 - x]+ else Nothing+ -- | Returns the box' coordinates, sorted and grouped by row. boxRows :: Box -> [[YX]] boxRows (Box tl br) = groupBy (\(YX y1 _) (YX y2 _) -> y1 == y2) $ Ix.range (tl, br)@@ -231,13 +239,13 @@ elems yxs bs' = sequenceA $ fmap (uncurry f) $ zip yxs (filter (/= '\n') $ BS.unpack bs') -- | Parses a newline delimited bytestring into an array.-byteStringToArray :: (IArray a e) => (Char -> Maybe e) -> ByteString -> Either String (a YX e)+byteStringToArray :: IArray a e => (Char -> Maybe e) -> ByteString -> Either String (a YX e) byteStringToArray f bs = byteStringToArrayM (const toElem) bs where toElem c = case f c of Just e -> Right e Nothing -> Left $ "unknown char: " ++ show c -- | Serializes an array into a bytestring. This function is the reverse of 'byteStringToArray'.-arrayToByteString :: (IArray a e) => (e -> Char) -> a YX e -> ByteString+arrayToByteString :: IArray a e => (e -> Char) -> a YX e -> ByteString arrayToByteString f arr = BS.intercalate "\n" lines where lines = fmap (BS.pack . fmap (f . (arr IArray.!))) . boxRows . uncurry Box . IArray.bounds $ arr
test/Spec.hs view
@@ -37,6 +37,14 @@ YX 1 2 `inBox` b `shouldBe` True YX 1 3 `inBox` b `shouldBe` False YX 0 0 `inBox` b `shouldBe` False+ it "computes depth" $ do+ let Just b = box (YX 1 0) 4+ boxDepth b 0 `shouldBe` Nothing+ boxDepth b (YX 1 0) `shouldBe` Just 0+ boxDepth b (YX 3 4) `shouldBe` Just 0+ boxDepth b 3 `shouldBe` Just 1+ boxDepth b 2 `shouldBe` Just 1+ boxDepth b (YX 2 0) `shouldBe` Just 0 it "returns rows" $ do let Just b = box (YX 1 0) (YX 2 2) boxRows b `shouldBe` [[YX 1 0, YX 1 1, YX 1 2], [YX 2 0, YX 2 1, YX 2 2]]
yx.cabal view
@@ -1,5 +1,5 @@ name: yx-version: 0.0.4.1+version: 0.0.4.2 synopsis: Row-major coordinates description: A minimal library for handling 2D coordinates. homepage: https://github.com/mtth/yx@@ -22,7 +22,7 @@ , lattices >= 2.0 , mtl >= 2.2 default-language: Haskell2010- ghc-options: -Wall+ ghc-options: -Wall -Wno-name-shadowing test-suite yx-test type: exitcode-stdio-1.0