Tuesday, April 13, 2010

Shape contents traversal...

Here's a bit of code using Neume's StateMap class an a Hughes list that separates the shape of data from its contents. In the code here elementary could also be coded as map (map f), but more inventive "elemenentary" functions are possible.



module ShapeContents where

import Neume.Core.Utils
import Neume.Core.Utils.HList


elementary :: (a -> b) -> [[a]] -> [[b]]
elementary f xss = reassemble (map f contents) shape
where
(shape, contents) = decompose xss



decompose :: [[a]] -> ([[()]],[a])
decompose xss = fmap2b ($ []) $ stmap fn id xss
where
fn stf row = (row1, stf . h1) where (row1,h1) = decompose1 row

decompose1 :: StateMap f => f a -> (f (), H a)
decompose1 = stmap fn id where
fn stf a = ((), stf `snoc` a)


reassemble :: [a] -> [[()]] -> [[a]]
reassemble xs shape = fst $ stmap (stmap fn) xs shape where
fn (s:st) () = (s,st)
fn _ () = error $ "reassemble - run out of data"

Blog Archive

About Me

My photo
Disambiguating biog as there are a few Stephen Tetley's in the world. I'm neither a cage fighter or yachtsman. I studied Fine Art in the nineties (foundation Bradford 1992, degree Cheltenham 1992 - 95) then Computing part-time at Leeds Met graduating in 2003. I'm the Stephen Tetley on Haskell Cafe and Stackoverflow.