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)) |
(→{{header|Python}}: Added a variant with a option-type return value - Nothing, or Just the extension found) |
||
Line 1,292: | Line 1,292: | ||
return True in map(fileName.lower().endswith, ("." + e.lower() for e in extensions)) |
return True in map(fileName.lower().endswith, ("." + e.lower() for e in extensions)) |
||
</lang> |
</lang> |
||
and we could also enrich the return value to a Maybe option type, returning Nothing if no file extension match is found, or Just the particular extension, if a match is found: |
|||
<lang python>'''Check for a specific set of file extensions''' |
|||
# extensionFound :: [Extension] -> FileName -> Maybe Extension |
|||
def extensionFound(xs): |
|||
'''Nothing if no matching extension is found, |
|||
or Just the extension (drawn from xs, and |
|||
a suffix of the filename, immediately following |
|||
a dot character). |
|||
''' |
|||
return lambda fn: find(fn.lower().endswith)( |
|||
['.' + x.lower() for x in xs] |
|||
) |
|||
# TEST ---------------------------------------------------- |
|||
# main :: IO () |
|||
def main(): |
|||
'''Check filenames for a particular set of extensions.''' |
|||
# checkExtension :: FileName -> Maybe Extension |
|||
def checkExtension(fn): |
|||
return extensionFound([ |
|||
'zip', 'rar', '7z', 'gz', 'archive', 'A##', 'tar.bz2' |
|||
])(fn) |
|||
print( |
|||
fTable(__doc__ + ':\n')(str)(str)( |
|||
compose(fromMaybe('n/a'))(checkExtension) |
|||
)([ |
|||
'MyData.a##', |
|||
'MyData.tar.Gz', |
|||
'MyData.gzip', |
|||
'MyData.7z.backup', |
|||
'MyData...', |
|||
'MyData', |
|||
'MyData_v1.0.tar.bz2', |
|||
'MyData_v1.0.bz2' |
|||
]) |
|||
) |
|||
# GENERIC ------------------------------------------------- |
|||
# Just :: a -> Maybe a |
|||
def Just(x): |
|||
'''Constructor for an inhabited Maybe (option type) value.''' |
|||
return {'type': 'Maybe', 'Nothing': False, 'Just': x} |
|||
# Nothing :: Maybe a |
|||
def Nothing(): |
|||
'''Constructor for an empty Maybe (option type) value.''' |
|||
return {'type': 'Maybe', 'Nothing': True} |
|||
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c |
|||
def compose(g): |
|||
'''Right to left function composition.''' |
|||
return lambda f: lambda x: g(f(x)) |
|||
# find :: (a -> Bool) -> [a] -> Maybe a |
|||
def find(p): |
|||
'''Just the first element in the list that matches p, |
|||
or Nothing if no elements match. |
|||
''' |
|||
def go(xs): |
|||
for x in xs: |
|||
if p(x): |
|||
return Just(x) |
|||
return Nothing() |
|||
return lambda xs: go(xs) |
|||
# fromMaybe :: a -> Maybe a -> a |
|||
def fromMaybe(x): |
|||
'''The default value x if mb is Nothing, |
|||
or the value contained in mb. |
|||
''' |
|||
return lambda mb: x if ( |
|||
mb.get('Nothing') |
|||
) else mb.get('Just') |
|||
# DISPLAY ------------------------------------------------- |
|||
# fTable :: String -> (a -> String) -> |
|||
# (b -> String) -> (a -> b) -> [a] -> String |
|||
def fTable(s): |
|||
'''Heading -> x display function -> fx display function -> |
|||
f -> xs -> tabular string. |
|||
''' |
|||
def go(xShow, fxShow, f, xs): |
|||
ys = [xShow(x) for x in xs] |
|||
w = max(map(len, ys)) |
|||
return s + '\n' + '\n'.join(map( |
|||
lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)), |
|||
xs, ys |
|||
)) |
|||
return lambda xShow: lambda fxShow: lambda f: lambda xs: go( |
|||
xShow, fxShow, f, xs |
|||
) |
|||
# MAIN --- |
|||
if __name__ == '__main__': |
|||
main()</lang> |
|||
{{Out}} |
|||
<pre>Check for a specific set of file extensions: |
|||
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|Racket}}== |
=={{header|Racket}}== |