module Control.Reactive.Util (
toUpperChar,
toLowerChar,
toUpperString,
toLowerString,
toCapitalString,
isSubstringOf,
isInfixOfNormalized,
isSubstringOfNormalized,
prefix,
suffix,
sep,
pre,
post,
wrap,
concatSep,
concatPre,
concatPost,
concatWrap,
concatLines,
divideList,
breakList,
concatMapN,
concatMapM,
concatMapA,
mapIndexed,
tau,
) where
import Prelude hiding (concat)
import Data.Monoid
import Control.Applicative
import Data.Foldable
import Data.Traversable
import Control.Monad (MonadPlus)
import qualified Data.Char as Char
import qualified Data.List as List
toUpperChar :: Char -> Char
toUpperChar = Char.toUpper
toLowerChar :: Char -> Char
toLowerChar = Char.toLower
toUpperString :: String -> String
toUpperString = fmap Char.toUpper
toLowerString :: String -> String
toLowerString = fmap Char.toLower
toCapitalString :: String -> String
toCapitalString [] = []
toCapitalString (x:xs) = toUpperChar x : toLowerString xs
isSubstringOf :: String -> String -> Bool
a `isSubstringOf` b
= a `List.isPrefixOf` b
|| a `List.isInfixOf` b
|| a `List.isSuffixOf` b
isInfixOfNormalized :: String -> String -> Bool
a `isInfixOfNormalized` b = toLowerString a `List.isInfixOf` toLowerString b
isSubstringOfNormalized :: String -> String -> Bool
a `isSubstringOfNormalized` b = toLowerString a `isSubstringOf` toLowerString b
prefix :: [a] -> [a] -> [a]
prefix x = (x ++)
suffix :: [a] -> [a] -> [a]
suffix x = (++ x)
sep :: a -> [a] -> [a]
sep = List.intersperse
pre :: a -> [a] -> [a]
pre x = (x :) . sep x
post :: a -> [a] -> [a]
post x = suffix [x] . sep x
wrap :: a -> a -> [a] -> [a]
wrap x y = (x :) . suffix [y] . sep x
concatSep :: [a] -> [[a]] -> [a]
concatSep x = concat . sep x
concatPre :: [a] -> [[a]] -> [a]
concatPre x = concat . pre x
concatPost :: [a] -> [[a]] -> [a]
concatPost x = concat . post x
concatWrap :: [a] -> [a] -> [[a]] -> [a]
concatWrap x y = concat . wrap x y
concatLines :: [String] -> String
concatLines = concatPost "\n"
divideList :: Int -> [a] -> [[a]]
divideList n xs
| length xs <= n = [xs]
| otherwise = [take n xs] ++ (divideList n $ drop n xs)
breakList :: Int -> [a] -> [a] -> [a]
breakList n z = mconcat . List.intersperse z . divideList n
concatMapN :: (Applicative f, Monoid b) => (a -> f b) -> [a] -> f b
concatMapN f = fmap mconcat . traverse f
concatMapM :: (MonadPlus m, Applicative f, Traversable t) => (a -> f (m b)) -> t a -> f (m b)
concatMapM f = fmap msum . traverse f
concatMapA :: (Alternative m, Applicative f, Traversable t) => (a -> f (m b)) -> t a -> f (m b)
concatMapA f = fmap asum . traverse f
mapIndexed :: (Int -> a -> b) -> [a] -> [b]
mapIndexed f as = map (uncurry f) (zip is as)
where
n = length as 1
is = [0..n]
tau :: Floating a => a
tau = 2 * pi