-- |
-- Module     : Simulation.Aivika.Experiment.Utils
-- Copyright  : Copyright (c) 2012-2017, David Sorokin <david.sorokin@gmail.com>
-- License    : BSD3
-- Maintainer : David Sorokin <david.sorokin@gmail.com>
-- Stability  : experimental
-- Tested with: GHC 8.0.1
--
-- It defines utility functions missed in the standard library.
--

module Simulation.Aivika.Experiment.Utils
       (divideBy, replace) where

import Data.List
import Data.List.Split

-- | Divide into the groups removing those elements
-- that satisfy the predicate.
divideBy :: (a -> Bool) -> [a] -> [[a]]
divideBy :: forall a. (a -> Bool) -> [a] -> [[a]]
divideBy a -> Bool
p [a]
xs =
  case forall a. (a -> Bool) -> [a] -> [a]
dropWhile a -> Bool
p [a]
xs of
    []  -> []
    [a]
xs' -> [a]
ys forall a. a -> [a] -> [a]
: forall a. (a -> Bool) -> [a] -> [[a]]
divideBy a -> Bool
p [a]
xs''
           where ([a]
ys, [a]
xs'') = forall a. (a -> Bool) -> [a] -> ([a], [a])
break a -> Bool
p [a]
xs'

-- | Replace the string.
replace :: String -> String -> String -> String
replace :: String -> String -> String -> String
replace String
old String
new = forall a. [a] -> [[a]] -> [a]
intercalate String
new forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Eq a => [a] -> [a] -> [[a]]
splitOn String
old