Commits

Sergey Astanin committed 7eac713

Removed reverse in favour of foldr

  • Participants
  • Parent commits 013af39

Comments (0)

Files changed (1)

File Network/Bitly.hs

        -> IO Result -- ^ Either error or long source URL
 expand acc url = request acc "expand" [("shortUrl", url)]
                              [ "bitly", "results", code, "longUrl" ]
-  where code = reverse . takeWhile (/= '/') . reverse $ url -- ending of the URL
+  where
+    ending d = foldr (\x xs -> if d `elem` xs then xs else x:xs) ""
+    code = dropHeadIf (== '/') $ ending '/' url
 
+dropHeadIf :: (a -> Bool) -> [a] -> [a]
+dropHeadIf _ [] = []
+dropHeadIf p all@(x:xs)
+  | p x        = xs
+  | otherwise  = all
 
 -- | Internal function to accomodate all types of requests
 request :: Account    -- ^ Account to use