File extension is in extensions list: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
No edit summary
Line 1: Line 1:
{{task}}Given a file name and a list of extensions (including the dot), tell whether the file's extension is in the extensions list. The check should be case insensitive.
{{task}}Given a file name and a list of extensions (including the dot), tell whether the file's extension is in the extensions list. The check should be case insensitive.

=={{header|Haskell}}==
<lang Haskell>
import Data.List
import qualified Data.Char as Ch

toLower :: String -> String
toLower = map Ch.toLower

isExt :: String -> [String] -> Bool
isExt filename extensions = any (`elem` (tails . toLower $ filename)) $ map toLower extensions
</lang>



=={{header|Python}}==
=={{header|Python}}==

Revision as of 16:54, 11 August 2014

Task
File extension is in extensions list
You are encouraged to solve this task according to the task description, using any language you may know.

Given a file name and a list of extensions (including the dot), tell whether the file's extension is in the extensions list. The check should be case insensitive.

Haskell

<lang Haskell> import Data.List import qualified Data.Char as Ch

toLower :: String -> String toLower = map Ch.toLower

isExt :: String -> [String] -> Bool isExt filename extensions = any (`elem` (tails . toLower $ filename)) $ map toLower extensions </lang>


Python

<lang Python> import os

def isExt(filename, extensions):

   return os.path.splitext(filename.lower())[-1] in [e.lower() for e in extensions]

</lang>


Scala

<lang Scala> def isExt(fileName: String, extensions: List[String]): Boolean = {

   extensions.map { _.toLowerCase }.exists { fileName.toLowerCase endsWith _ }

} </lang>