File extension is in extensions list: Difference between revisions

Separated task and extra task. Added code for task.
(Added Wren)
(Separated task and extra task. Added code for task.)
Line 1,261:
 
=={{header|Nim}}==
===Task===
For the task, it is possible to use the “splitFile” procedure from the standard module “os” as it split the extension at the last dot.
 
To build the extension list, we can use a compile time function:
<lang Nim>import os, strutils
 
let fileNameList = ["MyData.a##", "MyData.tar.Gz", "MyData.gzip",
"MyData.7z.backup", "MyData...", "MyData"]
 
func buildExtensionList(extensions: varargs[string]): seq[string] {.compileTime.} =
for ext in extensions:
result.add('.' & ext.toLowerAscii())
 
const ExtList = buildExtensionList("zip", "rar", "7z", "gz", "archive", "A##")
 
for fileName in fileNameList:
echo fileName, ": ", fileName.splitFile().ext.toLowerAscii() in ExtList</lang>
 
Another way consists to use “map” from standard module “sequtils”:
<lang Nim>import os, sequtils, strutils
 
let fileNameList = ["MyData.a##", "MyData.tar.Gz", "MyData.gzip",
"MyData.7z.backup", "MyData...", "MyData"]
 
const ExtList = map(["zip", "rar", "7z", "gz", "archive", "A##"],
proc(ext: string): string = '.' & ext.toLowerAscii())
 
for fileName in fileNameList:
echo fileName, ": ", fileName.splitFile().ext.toLowerAscii() in ExtList</lang>
 
{{out}}
<pre>MyData.a##: true
MyData.tar.Gz: true
MyData.gzip: false
MyData.7z.backup: false
MyData...: false
MyData: false</pre>
 
===Extra task===
The “splitFile” procedure no longer works in this case. We have to use “endsWith” from the “strutils” module:
 
<lang nim>import strutils
import sequtils
Anonymous user