From 3e80ef521dea2f0c72aa391e12281b0a56bd76e2 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Fri, 2 Apr 2010 00:37:10 -0400 Subject: [PATCH] Separate a few functions out in to a ListUtils module. --- src/ListUtils.hs | 37 +++++++++++++++++++++++++++++++++++++ src/hath.hs | 33 +-------------------------------- 2 files changed, 38 insertions(+), 32 deletions(-) create mode 100644 src/ListUtils.hs 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) diff --git a/src/hath.hs b/src/hath.hs index 7b406a8..669ed4f 100644 --- a/src/hath.hs +++ b/src/hath.hs @@ -4,16 +4,7 @@ import qualified Numeric as N import System.Exit (exitFailure) import Text.Regex.Posix - --- 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 - +import ListUtils -- Takes an IP address in CIDR notation, and returns a list of its -- octets (converted to Int). @@ -27,28 +18,6 @@ maskbits :: String -> Int maskbits cidr = read ((splitWith (`elem` "/") cidr) !! 1) --- 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) - - -- Takes an Int, and returns its base-two representation as a String. base_two :: Int -> String base_two n = N.showIntAtBase 2 DC.intToDigit n "" -- 2.43.2