File extension is in extensions list: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|REXX}}: added the REXX language. -- ~~~~)
m (→‎{{header|REXX}}: added a programming note.)
Line 35: Line 35:


=={{header|REXX}}==
=={{header|REXX}}==
Programming note:   extra code was added to display some error/warning messages.
<lang rexx>/*REXX program displays if a filename has a known extension (per a list)*/
<lang rexx>/*REXX program displays if a filename has a known extension (per a list)*/
extentions='.bat .cmd .com .ini .txt' /*this would be the complete list*/
extentions='.bat .cmd .com .ini .txt' /*this would be the complete list*/

Revision as of 20:11, 11 August 2014

File extension is in extensions list is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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. The file might not have an extension.

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>

The code defining isExt could be done point free: <lang Haskell> isExt filename = any (`elem` (tails . toLower $ filename)) . map toLower </lang>

Overcoming the one-liner urge on behalf of being more comprehensible would give: <lang Haskell> isExt filename extensions = any (`elem` allTails) lowerExtensions

                           where allTails = tails . toLower $ filename

lowerExtensions = 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>

REXX

Programming note:   extra code was added to display some error/warning messages. <lang rexx>/*REXX program displays if a filename has a known extension (per a list)*/ extentions='.bat .cmd .com .ini .txt' /*this would be the complete list*/ parse arg fn /*get the filename from the C.L. */ fn=strip(fn) /*remove any superfluous blanks. */ if fn== then exit /*No fn specified? Then exit.*/ p=lastpos('.',fn); if p==0 then do

                                  say 'Filename ' fn " has no extension."
                                  exit
                                  end

ft=substr(fn,p) /*pickoff the fileType (fileExt).*/ upper ft extentions /*uppercase a couple of REXX vars*/ if wordpos(ft,extentions)==0 then _='an unknown'

                             else _= 'a known'

say 'Filename ' fn "has" _ 'extension.'</lang>

Scala

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

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

} </lang>

Ruby

<lang ruby>extensions = [".c",".o",""] ["foo.C","foo.zkl","foo","foo."].each{|f| p extensions.include?( File.extname(f).downcase )} </lang>

Output:
true
false
true
true

zkl

<lang zkl>var exts=T(".c",".o",""); fcn hasExtension(fname){ exts.holds(File.splitFileName(fname)[3].toLower()) } T("foo.C","foo.zkl","foo","foo.").apply(hasExtension).println();</lang>

Output:
L(True,False,True,True)