module Utils ( binaryToInt , countElem , xor , right , bet , (&+) , howMany , sublists , windows ) where import Data.Char (digitToInt) import Control.Monad import Data.Either import Data.List (inits, tails) binaryToInt :: String -> Int binaryToInt = foldl (\a x -> a * 2 + digitToInt x) 0 countElem :: (Eq a) => a -> [a] -> Int countElem c ls = sum $ map (fromEnum . (== c)) ls xor :: Bool -> Bool -> Bool xor a b = (not a && b) || (a && not b) right :: Either a b -> b right (Right b) = b right _ = undefined bet :: Int -> (Int, Int) -> Bool bet k (l, u) = k >= l && k <= u -- combine filter predicates (&+) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool) (&+) = liftM2 (&&) howMany :: (a -> Bool) -> [a] -> Int howMany predicate = length . filter predicate sublists :: [a] -> [[a]] sublists = concatMap inits . tails windows :: Int -> [a] -> [[a]] windows m = foldr (zipWith (:)) (repeat []) . take m . tails kadane :: [Int] -> Int kadane = go 0 0 where go :: Int -> Int -> [Int] -> Int go best _ [] = best go best current (l:ls) = go (max best (current + l)) (max current (current + l)) ls