X-Git-Url: https://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FListUtils.hs;fp=src%2FListUtils.hs;h=5ec9120fc57384933e64719cba14206d851e8818;hb=3e80ef521dea2f0c72aa391e12281b0a56bd76e2;hp=0000000000000000000000000000000000000000;hpb=95b54c5699d04db1f311e5a204600c5859361064;p=hath.git diff --git a/src/ListUtils.hs b/src/ListUtils.hs new file mode 100644 index 0000000..5ec9120 --- /dev/null +++ b/src/ListUtils.hs @@ -0,0 +1,37 @@ +module ListUtils +( pad_left_to, + pad_right_to, + splitWith +) where + + +-- Stolen from ByteString. Splits a list at each element satisfying +-- the predicate p. +splitWith :: (a -> Bool) -> [a] -> [[a]] +splitWith p xs = + ys : case zs of + [] -> [] + _:ws -> splitWith p ws + where (ys,zs) = break p xs + + +-- Pads a list (on the left) to length len by prepending pad_elem. +pad_left_to :: Int -> a -> [a] -> [a] +pad_left_to len pad_elem xs = + if (length xs) >= len then + xs + else + (replicate padcount pad_elem) ++ xs + where + padcount = len - (length xs) + + +-- Pads a list (on the right) to length len by appending pad_elem. +pad_right_to :: Int -> a -> [a] -> [a] +pad_right_to len pad_elem xs = + if (length xs) >= len then + xs + else + xs ++ (replicate padcount pad_elem) + where + padcount = len - (length xs)