File extension is in extensions list: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: Added a version using isSuffixOf (and avoiding the gzip false positive of the draft above)) |
|||
Line 858: | Line 858: | ||
MyData_v1.0.tar.bz2 True |
MyData_v1.0.tar.bz2 True |
||
MyData_v1.0.bz2 False</pre> |
MyData_v1.0.bz2 False</pre> |
||
We can also express this directly in terms of the '''isSuffix''' function, taking care not to reproduce the small glitch in the draft above, which shows a false positive for '''zip''' in '''.gzip''' (see task description bullet 2). |
|||
<lang haskell>import Data.List (find, isSuffixOf) |
|||
import Data.Maybe (fromMaybe) |
|||
import Data.Char (toLower) |
|||
extensionFound :: String -> [String] -> Maybe String |
|||
extensionFound fp xs = find (`isSuffixOf` fp) (('.' :) <$> xs) >>= Just |
|||
-- TESTS -------------------------------------------------- |
|||
main :: IO () |
|||
main = do |
|||
let w = maximum $ length <$> files |
|||
(putStrLn . unlines) $ |
|||
zipWith |
|||
(\fp mb -> rjust w ' ' fp ++ " -> " ++ fromMaybe "n/a" mb) |
|||
files |
|||
((`extensionFound` lcased extensions) <$> lcased files) |
|||
-- DATA --------------------------------------------------- |
|||
extensions, files :: [String] |
|||
extensions = ["zip", "rar", "7z", "gz", "archive", "A##", "tar.bz2"] |
|||
files = |
|||
[ "MyData.a##" |
|||
, "MyData.tar.Gz" |
|||
, "MyData.gzip" |
|||
, "MyData.7z.backup" |
|||
, "MyData..." |
|||
, "MyData" |
|||
, "MyData_v1.0.tar.bz2" |
|||
, "MyData_v1.0.bz2" |
|||
] |
|||
-- STRINGS ------------------------------------------------ |
|||
rjust :: Int -> Char -> String -> String |
|||
rjust n c = (drop . length) <*> (replicate n c ++) |
|||
lcased :: [String] -> [String] |
|||
lcased = fmap (fmap toLower)</lang> |
|||
{{Out}} |
|||
<pre> MyData.a## -> .a## |
|||
MyData.tar.Gz -> .gz |
|||
MyData.gzip -> n/a |
|||
MyData.7z.backup -> n/a |
|||
MyData... -> n/a |
|||
MyData -> n/a |
|||
MyData_v1.0.tar.bz2 -> .tar.bz2 |
|||
MyData_v1.0.bz2 -> n/a</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |