[Pkg-haskell-commits] [SCM] haskell-testpack branch, master, updated. debian/1.0.2-1-4-gb0d6b36

John Goerzen jgoerzen at complete.org
Fri Apr 23 15:04:41 UTC 2010


The following commit has been merged in the master branch:
commit 5f67a3cf290a82f7d19138c0489b78111aa3bd6c
Author: John Goerzen <jgoerzen at complete.org>
Date:   Tue Dec 27 01:56:19 2005 +0100

    New List function subIndex
    
    Plus tests for it
    Based on suggestion by Bulat Ziganshin

diff --git a/MissingH/List.hs b/MissingH/List.hs
index bc18448..fc46baf 100644
--- a/MissingH/List.hs
+++ b/MissingH/List.hs
@@ -50,12 +50,13 @@ module MissingH.List(-- * Tests
                      -- * Fixed-Width and State Monad Utilities
                      grab,
                      -- * Miscellaneous
-                     countElem, elemRIndex, alwaysElemRIndex, seqList
+                     countElem, elemRIndex, alwaysElemRIndex, seqList,
+                     subIndex
                      -- -- * Sub-List Selection
                      -- sub,
                     ) where
 import Data.List(intersperse, concat, isPrefixOf, isSuffixOf, elemIndices,
-                elemIndex, elemIndices, tails, find)
+                elemIndex, elemIndices, tails, find, findIndex)
 import IO
 import System.IO.Unsafe
 import Control.Monad.State(State, get, put)
@@ -381,3 +382,22 @@ grab count =
        put g'
        return x
 
+{- | Similar to Data.List.elemIndex.  Instead of looking for one element in a
+list, this function looks for the first occurance of a sublist in the list,
+and returns the index of the first element of that occurance.  If there is no
+such list, returns Nothing.
+
+If the list to look for is the empyt list, will return Just 0 regardless
+of the content of the list to search.
+
+Examples:
+
+>subIndex "foo" "asdfoobar" -> Just 3
+>subIndex "foo" [] -> Nothing
+>subIndex "" [] -> Just 0
+>subIndex "" "asdf" -> Just 0
+>subIndex "test" "asdftestbartest" -> Just 4
+>subIndex [(1::Int), 2] [0, 5, 3, 2, 1, 2, 4] -> Just 4
+ -}
+subIndex :: Eq a => [a] -> [a] -> Maybe Int
+subIndex substr str = findIndex (isPrefixOf substr) (tails str)
diff --git a/testsrc/Listtest.hs b/testsrc/Listtest.hs
index d2f0230..8d97e42 100644
--- a/testsrc/Listtest.hs
+++ b/testsrc/Listtest.hs
@@ -144,6 +144,16 @@ test_alwaysElemRIndex =
         ,f 'f' ['f', 'b', 'f', 'f', 'b'] 3
         ]
 
+test_subIndex = 
+    let f item inp exp = TestCase $ exp @=? subIndex item inp in 
+        [f "foo" "asdfoobar" (Just 3)
+        ,f "foo" [] (Nothing)
+        ,f "" [] (Just 0)
+        ,f "" "asdf" (Just 0)
+        ,f "test" "asdftestbartest" (Just 4)
+        ,f [(1::Int), 2] [0, 5, 3, 2, 1, 2, 4] (Just 4)
+        ]
+
 test_fixedWidth =
     let f inplen inplist exp = TestLabel ((show inplen) ++ ", " ++
                                           (show inplist)) $ TestCase $
@@ -190,7 +200,8 @@ tests = TestList [TestLabel "delFromAL" (TestList test_delFromAL),
                   TestLabel "replace" (TestList test_replace),
                   TestLabel "contains" (TestList test_contains),
                   TestLabel "strFromAL & strToAL" (TestList test_strToAL),
-                  TestLabel "fixedWidth" (TestList test_fixedWidth)]
+                  TestLabel "fixedWidth" (TestList test_fixedWidth),
+                  TestLabel "subIndex" (TestList test_subIndex)]
 
 
 

-- 
haskell-testpack



More information about the Pkg-haskell-commits mailing list