module Playground04 where

ifEven :: Integral t => (t -> t) -> t -> t
ifEven myFunction x = if even x
                        then myFunction x
                        else x


genIfEven :: Integral t => (t -> t) -> t -> t
genIfEven f = (\x -> ifEven f x)

inc :: Num a => a -> a
inc x = x + 1

ifEvenIncrement :: Integral t => t -> t
ifEvenIncrement x = genIfEven inc x

-- Listing 4.2 

getRequestURL :: [Char] -> [Char] -> [Char] -> [Char] -> [Char]
getRequestURL host apiKey resource resourceId = host ++ 
                                        "/" ++
                                        resource ++
                                        "/" ++
                                        resourceId ++
                                        "?token=" ++
                                        apiKey

-- genHostRequestBuilder :: String t =>  t -> (t -> t -> t -> t)
genHostRequestBuilder :: [Char] -> [Char] -> [Char] -> [Char] -> [Char]
genHostRequestBuilder host = (\apiKey resource resourceId ->
                                getRequestURL host apiKey resource resourceId)

genApiRequestBuilder ::  (t3 -> t2 -> t1 -> t) -> t3 -> t2 -> t1 -> t
genApiRequestBuilder hostBuilder apiKey = (\resource resourceId ->
    hostBuilder apiKey resource resourceId)

genResorceRequestBuilder :: (t2 -> t1 -> t) -> t2 -> t1 -> t
genResorceRequestBuilder apiKeyBuilder resource = 
    (\resourceId -> apiKeyBuilder resource resourceId)

-- partial application

exampleUrlBuilder :: [Char] -> [Char] -> [Char] -> [Char]
exampleUrlBuilder = getRequestURL "http://www.crosta.com"

getBook :: [Char] -> [Char]
getBook = exampleUrlBuilder "eeofih" "books"

doAction :: [a] -> [a] -> [a]
doAction noun verb = noun ++ verb

doAction' :: [a] -> [a] -> [a]
doAction' verb noun = doAction noun verb

flippy :: (t2 -> t1 -> t) -> t1 -> t2 -> t
flippy func = (\x y -> func y x )

subtract2 :: Num a => a -> a
subtract2 x = (-) x 2