-- |
-- Module      :  Data.List.InnToOut
-- Copyright   :  (c) OleksandrZhabenko 2019
-- License     :  MIT
--
-- Maintainer  :  olexandr543@yahoo.com
--
-- Various additional operations on lists.
--

module Data.List.InnToOut
  (
    -- * Operation to apply a function that creates an inner list to an element of the outer list   
       mapI
       , mapI2
  ) where

-- | Function that applies additional function @f :: a -> [a]@ to @a@ if @p a = True@
mapI :: (a -> Bool) -> (a -> [a]) -> [a] -> [a]
mapI p f = concatMap (\x -> if p x then f x else [x])
{-#INLINE mapI#-}

-- | Function that applies additional function @f :: a -> b@ to @a@ if @p a = True@ and otherwise another function @g :: a -> [b]@  to @[a]@ to obtain @[b]@
mapI2 :: (a -> Bool) -> (a -> b) -> (a -> [b]) -> [a] -> [b]
mapI2 p f g = concatMap (\x -> if p x then [f x] else g x)
{-#INLINE mapI2#-}

-- | Function that can apply two different ways of computing something depending of the predicate value @p :: a -> Bool@ for the @[a]@. Similar to arrow techniques.
mapI3 :: (a -> Bool) -> (a -> b) -> (b -> d) -> (a -> c) -> (c -> d) -> [a] -> [d]
mapI3 p f1 g f2 h = map (\x -> if p x then g (f1 x) else h (f2 x))
{-#INLINE mapI3#-}