[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 14:55:35 UTC 2010


The following commit has been merged in the master branch:
commit 7e7df5b9b5e3a2a7554776e4d2f4a4ddd25f661c
Author: John Goerzen <jgoerzen at complete.org>
Date:   Tue Apr 5 22:47:01 2005 +0100

    New wholeMap, fixedWidth functions
    
    Keywords:
    
    
    (jgoerzen at complete.org--projects/missingh--head--0.7--patch-211)

diff --git a/ChangeLog b/ChangeLog
index 4f6c75f..172f788 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,19 @@
 # arch-tag: automatic-ChangeLog--jgoerzen at complete.org--projects/missingh--head--0.7
 #
 
+2005-04-05 16:47:01 GMT	John Goerzen <jgoerzen at complete.org>	patch-211
+
+    Summary:
+      New wholeMap, fixedWidth functions
+    Revision:
+      missingh--head--0.7--patch-211
+
+
+    modified files:
+     ChangeLog MissingH/List.hs debian/changelog
+     testsrc/Listtest.hs
+
+
 2005-04-01 20:44:21 GMT	John Goerzen <jgoerzen at complete.org>	patch-210
 
     Summary:
diff --git a/MissingH/List.hs b/MissingH/List.hs
index 0fede95..005e962 100644
--- a/MissingH/List.hs
+++ b/MissingH/List.hs
@@ -45,6 +45,8 @@ module MissingH.List(-- * Tests
                      -- * Conversions
                      split, join, replace, genericJoin, takeWhileList,
                      dropWhileList, spanList, breakList,
+                     -- * Advanced Conversions
+                     WholeFunc(..), wholeMap, fixedWidth,
                      -- * Miscellaneous
                      countElem, elemRIndex, alwaysElemRIndex, seqList
                      -- -- * Sub-List Selection
@@ -258,3 +260,76 @@ alwaysElemRIndex item list =
 seqList :: [a] -> [a]
 seqList [] = []
 seqList (x:xs) = seq (seqList xs) (x:xs)
+
+--------------------------------------------------
+-- Advanced Conversions
+--------------------------------------------------
+
+{- | The type used for functions for 'wholeMap'.  See 'wholeMap' for details.
+-}
+newtype WholeFunc a b = WholeFunc ([a] -> (WholeFunc a b, [a], [b]))
+
+{- | This is an enhanced version of the concatMap or map functions in 
+Data.List.
+
+Unlike those functions, this one:
+
+ * Can consume a varying number of elements from the input list during
+   each iteration
+
+ * Can arbitrarily decide when to stop processing data
+
+ * Can return a varying number of elements to insert into the output list
+
+ * Can actually switch processing functions mid-stream
+
+ * Is not even restricted to processing the input list intact
+
+The function used by wholeMap, of type 'WholeFunc', is repeatedly called
+with the input list.  The function returns three things: the function
+to call for the next iteration (if any), what remains of the input list,
+and the list of output elements generated during this iteration.  The return
+value of 'wholeMap' is the concatenation of the output element lists from
+all iterations.
+
+Processing stops when the remaining input list is empty.  An example
+of a 'WholeFunc' is 'fixedWidth'. -}
+wholeMap :: WholeFunc a b -> [a] -> [b]
+wholeMap _ [] = []              -- Empty input, empty output.
+wholeMap (WholeFunc func) inplist =
+    let (nextfunc, nextlist, output) = func inplist
+        in
+        output ++ wholeMap nextfunc nextlist
+
+{- | A parser designed to process fixed-width input fields.  Use it with
+'wholeMap'.
+
+The Int list passed to this function is the list of the field widths desired
+from the input.  The result is a list of those widths, if possible.  If any
+of the input remains after processing this list, it is added on as the final
+element in the result list.  If the input is less than the sum of the requested
+widths, then the result list will be short the appropriate number of elements,
+and its final element may be shorter than requested.
+
+Examples:
+
+>wholeMap (fixedWidth [1, 2, 3]) "1234567890"
+> --> ["1","23","456","7890"]
+>wholeMap (fixedWidth (repeat 2)) "123456789"
+> --> ["12","34","56","78","9"]
+>wholeMap (fixedWidth []) "123456789"
+> --> ["123456789"]
+>wholeMap (fixedWidth [5, 3, 6, 1]) "Hello, This is a test."
+> --> ["Hello",", T","his is"," ","a test."]
+-}
+fixedWidth :: [Int] -> WholeFunc a [a]
+fixedWidth len = 
+    WholeFunc (fixedWidthFunc len)
+    where -- Empty input: Empty output, stop
+          fixedWidthFunc _ [] = ((fixedWidth []), [], [])
+          -- Empty length: Stop here.
+          fixedWidthFunc [] x = ((fixedWidth []), [], [x])
+          -- Stuff to process: Do it.
+          fixedWidthFunc (len:lenxs) input =
+              (fixedWidth lenxs, next, [this])
+              where (this, next) = splitAt len input
\ No newline at end of file
diff --git a/debian/changelog b/debian/changelog
index 80522da..b5398b7 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -2,6 +2,7 @@ missingh (0.9.2) unstable; urgency=low
 
   * New modules: MissingH.Debian, MissingH.Debian.ControlParser
   * Temporary changes to work with new Hugs
+  * New MissingH.List functions: wholeMap, fixedWidth
 
  -- John Goerzen <jgoerzen at complete.org>  Tue, 15 Mar 2005 09:18:10 -0600
 
diff --git a/testsrc/Listtest.hs b/testsrc/Listtest.hs
index 045caba..ba7c4e8 100644
--- a/testsrc/Listtest.hs
+++ b/testsrc/Listtest.hs
@@ -144,6 +144,25 @@ test_alwaysElemRIndex =
         ,f 'f' ['f', 'b', 'f', 'f', 'b'] 3
         ]
 
+test_fixedWidth =
+    let f inplen inplist exp = TestLabel ((show inplen) ++ ", " ++
+                                          (show inplist)) $ TestCase $
+                               wholeMap (fixedWidth inplen) inplist @=? exp in
+        [
+         f [] ([]::[Int]) ([]::[[Int]])
+        ,f [1] [5] [[5]]
+        ,f [1] [3, 4, 5, 6] [[3], [4, 5, 6]]
+        ,f [1] ([]::[Int]) ([]::[[Int]])
+        ,f [2] [3] [[3]]
+        ,f [2] [3, 4, 5, 6] [[3, 4], [5, 6]]
+        ,f [2] [3, 4, 5] [[3, 4], [5]]
+        ,f [1, 2, 3] "1234567890"  ["1","23","456","7890"]
+        ,f (repeat 2) "123456789" ["12","34","56","78","9"]
+        ,f [] "123456789" ["123456789"]
+        ,f [5, 3, 6, 1] "Hello, This is a test." 
+               ["Hello",", T","his is"," ","a test."]
+        ]
+
 test_strToAL =
     let f inp exp = TestLabel (show inp) $ TestCase $ do let r = strFromAL inp
                                                          exp @=? r
@@ -170,7 +189,8 @@ tests = TestList [TestLabel "delFromAL" (TestList test_delFromAL),
                   TestLabel "alwaysElemRIndex" (TestList test_alwaysElemRIndex),
                   TestLabel "replace" (TestList test_replace),
                   TestLabel "contains" (TestList test_contains),
-                  TestLabel "strFromAL & strToAL" (TestList test_strToAL)]
+                  TestLabel "strFromAL & strToAL" (TestList test_strToAL),
+                  TestLabel "fixedWidth" (TestList test_fixedWidth)]
 
 
 

-- 
haskell-testpack



More information about the Pkg-haskell-commits mailing list