packages feed

foldable-ix 0.1.0.0 → 0.2.0.0

raw patch · 3 files changed

+53/−15 lines, 3 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

+ Data.Foldable.Ix: B23 :: a -> !Int -> !Int -> TwoInThreeBang a b
+ Data.Foldable.Ix: B23L :: a -> ![Int] -> !Int -> TwoInThreeBang2 a
+ Data.Foldable.Ix: B34 :: b -> b -> !b -> ![a] -> ThreeInFourBang a b
+ Data.Foldable.Ix: data ThreeInFourBang a b
+ Data.Foldable.Ix: data TwoInThreeBang a b
+ Data.Foldable.Ix: data TwoInThreeBang2 a
+ Data.Foldable.Ix: findIdx1' :: (Eq a, Foldable t) => a -> t a -> Maybe Int
+ Data.Foldable.Ix: instance (GHC.Classes.Eq b, GHC.Classes.Eq a) => GHC.Classes.Eq (Data.Foldable.Ix.ThreeInFourBang a b)
+ Data.Foldable.Ix: instance GHC.Classes.Eq a => GHC.Classes.Eq (Data.Foldable.Ix.TwoInThreeBang a b)
+ Data.Foldable.Ix: s2L :: Eq a => Int -> Int -> [a] -> [a]
+ Data.Foldable.Ix: sliceToList :: (Eq a, Foldable t) => Int -> Int -> t a -> [a]

Files

CHANGELOG.md view
@@ -3,3 +3,7 @@ ## 0.1.0.0 -- 2021-01-02  * First version. Released on an unsuspecting world.++## 0.2.0.0 -- 2021-02-18++* Second version. Added slicing functions. Some code improvements.
Data/Foldable/Ix.hs view
@@ -1,9 +1,10 @@-{-# LANGUAGE CPP #-}+{-# LANGUAGE CPP, BangPatterns #-} {-# OPTIONS_HADDOCK show-extensions #-}+{-# OPTIONS_GHC -funbox-strict-fields #-}  -- | -- Module      :  Data.Foldable.Ix--- Copyright   :  (c) OleksandrZhabenko 2020+-- Copyright   :  (c) OleksandrZhabenko 2020-2021 -- License     :  MIT -- Stability   :  Experimental -- Maintainer  :  olexandr543@yahoo.com@@ -14,16 +15,32 @@  import Data.Foldable +data TwoInThreeBang a b = B23 a !Int !Int deriving Eq++data TwoInThreeBang2 a = B23L a ![Int] !Int++data ThreeInFourBang a b = B34 b b !b ![a] deriving Eq+ {-| Function to find out the \'index\' (as the reperesentative of the 'Integral' class) of the first element in the 'Foldable' structure (from the left with indices starting from 0), which equals to the first argument. Returns 'Nothing' if there are no such elements. -} findIdx1 :: (Eq a, Foldable t, Integral b) => a -> t a -> Maybe b-findIdx1 x js = (\(_,n1,_) -> if n1 == (-1) then Nothing else Just n1) . foldl' f v $ js-  where v = (x,-1,0)-        f (t,n,m) y-         | n >= 0 = (t,n,m + 1)-         | y == t = (t,m,m + 1)-         | otherwise = (t,n,m + 1)+findIdx1 x js = (\(_, n1, _) -> if n1 == (-1) then Nothing else Just n1) . foldl' f v $ js+  where v = (x, (-1), 0)+        f (t, n, m) y+         | n >= 0 = (t, n, m + 1)+         | y == t = (t, m, m + 1)+         | otherwise = (t, n, m + 1) +{-| A variant of the 'findIdx1' where the resulting 'Maybe' b is 'Maybe' 'Int'. Possibly can be more optimized.+-}+findIdx1' :: (Eq a, Foldable t) => a -> t a -> Maybe Int+findIdx1' x js = (\(B23 _ n1 _) -> if n1 == (-1) then Nothing else Just n1) . foldl' f v $ js+  where v = B23 x (-1) 0+        f (B23 t n m) y+         | n >= 0 = B23 t n (m + 1)+         | y == t = B23 t m (m + 1)+         | otherwise = B23 t n (m + 1)+          {-| Function to find out the \'indices\' of the elements in the 'Foldable' structure (from the left with indices starting from 0) that equal to the first argument. Returns empty list if there are no such elements. Uses two passes through the structure. -}@@ -38,8 +55,24 @@ pass through the structure and additional 'reverse' operation on the resulting list with 'foldl''. -} findIdxsL1 :: (Eq a, Foldable t) => a -> t a -> [Int]-findIdxsL1 x js = (\(_,ys,_) -> reverse ys) . foldl' f v $ js-  where v = (x,[],0)-        f (t,xs,m) y-         | y == t = (t,m:xs,m + 1)-         | otherwise = (t,xs,m + 1)+findIdxsL1 x js = (\(B23L _ ys _) -> reverse ys) . foldl' f v $ js+  where v = B23L x [] 0+        f (B23L t xs m) y+         | y == t = B23L t (m:xs) (m + 1)+         | otherwise = B23L t xs (m + 1)++{-| Inspired by the Data.Vector.slice function from the @vector@ package. Takes a \'slice\' for the 'Foldable' structure converting it to the list. The first argument is the \'index\' of the element in the structure starting from 0 from the left. The second one is the length of the slice.+-}+sliceToList :: (Eq a, Foldable t) => Int -> Int -> t a -> [a]+sliceToList idx l js = (\(B34 _ _ _ ys) -> reverse ys) . foldl' f v $ js+  where v = B34 idx l 0 []+        f (B34 idx l i xs) x+         | i >= idx && i <= idx + l - 1 = B34 idx l (i+1) (x:xs)+         | otherwise = B34 idx l (i+1) xs+{-# SPECIALIZE sliceToList :: (Eq a) => Int -> Int -> [a] -> [a] #-}+{-# NOINLINE[2] sliceToList #-}++{-# RULES "sliceToList/lists" sliceToList = s2L #-}+s2L :: (Eq a) => Int -> Int -> [a] -> [a]+s2L idx l = drop idx . take (idx + l)+{-# INLINABLE s2L #-}
foldable-ix.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/  name:                foldable-ix-version:             0.1.0.0+version:             0.2.0.0 synopsis:            Functions to find out the indices of the elements in the Foldable structures description:         Uses folds to pass through the structure. homepage:            https://hackage.haskell.org/package/foldable-ix@@ -19,7 +19,8 @@ library   exposed-modules:     Data.Foldable.Ix   -- other-modules:-  other-extensions:    CPP+  other-extensions:    CPP, BangPatterns+  ghc-options:         -funbox-strict-fields   build-depends:       base >=4.8 && <4.15   -- hs-source-dirs:   default-language:    Haskell2010