How to modularize/abstract away os.walkDir and os.walkDirRec - Nim - nim-lang

I wonder if it is possible with function pointers or macros or the like, to make the code below more modular, as in, not repeat the whole for loop, with the only difference being the walkDirRec vs walkDir.
I tried with function pointers and a macro, but did not manage to get either one working. (-> nim-noob)
import os
proc containsFilesWithSuffix(dir: string, suffix: string, recursive: bool = true) : bool =
if recursive:
for file in os.walkDirRec(dir):
if file.toLower().endsWith(suffix):
return true
else:
for file in os.walkDir(dir):
if file.toLower().endsWith(suffix):
return true
return false

import std/[os, strutils]
template anyOfIt*(sequence, predicate: untyped): bool =
var result = false
for it {.inject.} in sequence:
if predicate:
result = true
break
result
proc containsFilesWithSuffix(dir: string, suffix: string, recursive: bool = true) : bool =
if recursive: anyOfIt(walkDirRec(dir), it.toLower.endsWith(suffix))
else: anyOfIt(walkDir(dir), it.path.toLower.endsWith(suffix))
Something like that would work fine, though in this case I would just write a for loop anyway since it is easier to add more conditions/logic when I need it.

Here is a version using templates:
import os, strutils
template innerLoop(it, op: untyped): untyped =
for file {.inject.} in it:
if toLower(op).endsWith(suffix):
return true
proc containsFilesWithSuffix(dir: string, suffix: string, recursive: bool = true) : bool =
if recursive:
innerLoop(walkDirRec(dir), file)
else:
innerLoop(walkDir(dir), file.path)
return false

I find the main problem here is that walkDirRec and walkDir are {.inline.} iterators and have different signatures. Only {.closure.} iterators can be passed around freely (according to the manual). One way or another you have to unify their signatures, doing file vs file.path or interfacing.
I'm leaving this here to show how you can assign iterators to a variable if their signatures match:
import os, strutils
iterator walkRec(dir: string): string {.closure.} =
for file in walkDirRec(dir):
yield file
iterator walkDir(dir: string): string {.closure.} =
for _, file in walkDir(dir):
yield file
proc hasFile(dir, suffix: string, recursive: bool = true): bool =
let iterFiles: iterator(dir: string): string =
if recursive: walkRec else: walkDir
for file in iterFiles(dir):
if file.toLower().endsWith(suffix):
return true
Now you can call the hasFile proc as follows:
echo hasFile("path", ".exe") # Find .exe files recursively at path
echo hasFile("path", ".exe", recursive=false) # Same, but no recursion

All of these answers are pretty much the same, this is just another variant that balances readability against reusability a bit differently.
One complexity comes from the fact that your code as written won't compile: file.toLower in the walkDir branch doesn't compile, due to the fact that walkDir doesn't yield a string, but a tuple, so your branches aren't as similar as you present.
In this version i elide that fact with a hacky template that makes file.path work for either branch, and reduce the noise of passing more variables by declaring the templates in the scope of the proc.
import std/[os,strutils]
proc containsFilesWithsuffix(dir: string, suffix: string, recursive: bool = true): bool =
template path(s:string): string = s # walkDir/walkDirRec compatibility
template loopWith(walk):untyped =
for file in walk(dir):
if file.path.toLower.endsWith(suffix):
return true
if recursive:
loopWith(os.walkDirRec)
else:
loopWith(os.walkDir)

Related

Add method to string and modify self in Lua

How can I add a method to the string table and modify self inside it ?
Basically, I'm trying to mimic the behaviour of the io.StringIO.read method in python, which reads n char in the string and returns them, modifying the string by "consuming" it.
I tried this:
function string.read(str, n)
to_return = str:sub(1, n)
str = str:sub(n + 1)
return to_return
end
local foo = "heyfoobarhello"
print(string.read(foo, 3))
print(foo)
Output is:
hey
heyfoobarhello
I expected the second line to be only foobarhello.
How can I achieve this ?
To mimic Python's io.StringIO class, you must make an object that stores both the underlying string and the current position within that string. Reading from an IO stream normally does not modify the underlying data.
local StringIO_mt = {
read = function(self, n)
n = n or #self.buffer - self.position + 1
local result = self.buffer:sub(self.position, self.position + n - 1)
self.position = self.position + n
return result
end,
}
StringIO_mt.__index = StringIO_mt
local function StringIO(buffer)
local o = {buffer = buffer, position = 1}
setmetatable(o, StringIO_mt)
return o
end
local foo = StringIO"heyfoobarhello"
print(foo:read(3))
print(foo:read())
Output:
hey
foobarhello
I don't recommend adding this class or method to Lua's string library, because the object has to be more complex than just a string.
You can add methods to the datatype string independently from the string table.
Short example that shows that the string methods even work if string table gets deleted...
string=nil
return _VERSION:upper():sub(1,3)
-- Returning: LUA
So you can add a method...
-- read.lua
local read = function(self, n1, n2)
return self:sub(n1, n2)
end
getmetatable(_VERSION).__index.read=read
return read
...for all strings.
( Not only _VERSION )
And use it...
do require('read') print(_VERSION:read(1,3):upper()) end
-- Print out: LUA

How to prevent setmetatable to be overridden

I want to share content in a secure way without exposing it to malicious code. Let's say I have a
Base addon
local BaseAddon = {}
local secretTable = {someContent = "hidden content"}
local function index(t,k)
if (k == "SECRET") then
return secretTable
end
end
setmetatable(BaseAddon, {
__index = index,
__newindex = function() end,
__metatable = false, -- disallow setmetatable again
})
return BaseAddon -- or a global hook method...
and a Sub addon that accesses the Base addon hidden content
local SubAddon = require("BaseAddon") -- or a global hook method...
SubAddon = SubAddon["SECRET"]
print(SubAddon.somelib) -- returns content
SubAddon = SubAddon["SECRETS"]
print(SubAddon.somelib) -- returns index local 'SubAddon' (a nil value)
but this is still not safe. I could now just simply do the following to catch my secret:
function newSetmetatable(table, mt)
mt.__metatable = nil
mt.__index = function(t,k)
print(k)
return t[k]
end
return originalSetmetatable (table, mt)
end
originalSetmetatable = setmetatable
setmetatable = newSetmetatable
Is there any way to prevent this or another solution to actually share the secret table?
Your original code is as good as you can get in Lua. Any sort of security like that you set up in Lua needs to either be written in C, or be ran before any untrusted code runs.

Grails convert String to Map with comma in string values

I want convert string to Map in grails. I already have a function of string to map conversion. Heres the code,
static def StringToMap(String reportValues){
Map result=[:]
result=reportValues.replace('[','').replace(']','').replace(' ','').split(',').inject([:]){map,token ->
List tokenizeStr=token.split(':');
tokenizeStr.size()>1?tokenizeStr?.with {map[it[0]?.toString()?.trim()]=it[1]?.toString()?.trim()}:tokenizeStr?.with {map[it[0]?.toString()?.trim()]=''}
map
}
return result
}
But, I have String with comma in the values, so the above function doesn't work for me. Heres my String
[program_type:, subsidiary_code:, groupName:, termination_date:, effective_date:, subsidiary_name:ABC, INC]
my function returns ABC only. not ABC, INC. I googled about it but couldnt find any concrete help.
Generally speaking, if I have to convert a Stringified Map to a Map object I try to make use of Eval.me. Your example String though isn't quite right to do so, if you had the following it would "just work":
// Note I have added '' around the values.
​String a = "[program_type:'', subsidiary_code:'', groupName:'', termination_date:'', effective_date:'', subsidiary_name:'ABC']"
Map b = Eval.me(a)​
// returns b = [program_type:, subsidiary_code:, groupName:, termination_date:, effective_date:, subsidiary_name:ABC]
If you have control of the String then if you can create it following this kind of pattern, it would be the easiest solution I suspect.
In case it is not possible to change the input parameter, this might be a not so clean and not so short option. It relies on the colon instead of comma values.
​String reportValues = "[program_type:, subsidiary_code:, groupName:, termination_date:, effective_date:, subsidiary_name:ABC, INC]"
reportValues = reportValues[1..-2]
def m = reportValues.split(":")
def map = [:]
def length = m.size()
m.eachWithIndex { v, i ->
if(i != 0) {
List l = m[i].split(",")
if (i == length-1) {
map.put(m[i-1].split(",")[-1], l.join(","))
} else {
map.put(m[i-1].split(",")[-1], l[0..-2].join(","))
}
}
}
map.each {key, value -> println "key: " + key + " value: " + value}
BTW: Only use eval on trusted input, AFAIK it executes everything.
You could try messing around with this bit of code:
String tempString = "[program_type:11, 'aa':'bb', subsidiary_code:, groupName:, termination_date:, effective_date:, subsidiary_name:ABC, INC]"
List StringasList = tempString.tokenize('[],')
def finalMap=[:]
StringasList?.each { e->
def f = e?.split(':')
finalMap."${f[0]}"= f.size()>1 ? f[1] : null
}
println """-- tempString: ${tempString.getClass()} StringasList: ${StringasList.getClass()}
finalMap: ${finalMap.getClass()} \n Results\n finalMap ${finalMap}
"""
Above produces:
-- tempString: class java.lang.String StringasList: class java.util.ArrayList
finalMap: class java.util.LinkedHashMap
Results
finalMap [program_type:11, 'aa':'bb', subsidiary_code:null, groupName:null, termination_date:null, effective_date:null, subsidiary_name:ABC, INC:null]
It tokenizes the String then converts ArrayList by iterating through the list and passing each one again split against : into a map. It also has to check to ensure the size is greater than 1 otherwise it will break on f[1]

Algorithm to delete duplicate characters from a String

Let us a have a string "abbashbhqa". We have to remove the duplicate characters in such a manner that the output should be "abshq". One possible solution is to check each character with the others present in the string and then manipulate. But this requires O(n^2) time complexity. Is there any optimised approach to do so ?
O(n):
Define an array L[26] of booleans. Set all to FALSE.
Construct a new empty string
Walk over the string and for each letter check if L [x] is FALSE. If so, append x to the new string and set L [x] to 1.
Copy new string to the old one.
as soon as you iterate string you create a set (or hash set). in case the alphabet is limited (English letters as in your example) you just can create a 256 boolean array and use ASCII code as a key to it. Make all booleans to be false at starting point. Each iteration you check if array[] is false or true. In case it's false, the symbol is not a duplicate, so you mark it into array[] = true, do not remove from the string and go on. in case it's true - the symbol is a duplicate
Probably this will be the implementation of the above problem
import java.util.*;
import java.io.*;
public class String_Duplicate_Removal
{
public static String duplicate_removal(String s)
{
if(s.length()<2)
return s;
else if(s.length()==2)
{
if(s.charAt(0)==s.charAt(1))
s = Character.toString(s.charAt(0));
return s;
}
boolean [] arr = new boolean[26];
for(int i=0;i<s.length();i++)
{
if(arr[s.charAt(i)-'a']==false)
arr[s.charAt(i)-'a']=true;
else
{
s= ((new StringBuilder(s)).deleteCharAt(i)).toString();
i--;
}
}
return s;
}
public static void main(String [] args)
{
String s = "abbashbhqa";
System.out.println(duplicate_removal(s));
}
}
I am solving using Python and it works in O(n) time and O(n) space --
I am using set() as set does not allow duplicates ---
In this case the order of elements gets changed --
If u want the order to remain same then u can use OrderedDict() and it also works in O(n) time --
def remove_duplicates(s , ans_set):
for i in s: # O(n)
ans_set.add(i) # O(1)
ans = ''
for char in ans_set:
ans += char
print ans
s = raw_input()
ans_set = set()
remove_duplicates(s , ans_set)
from collections import OrderedDict
def remove_duplicates_maintain_order(a):
ans_dict = OrderedDict()
for i in a: # O(n)
ans_dict[i] = ans_dict.get(i , 0) + 1 # O(1)
ans = ''
for char in ans_dict:
ans += char
print ans
s = raw_input()
remove_duplicates_maintain_order(s)

How to make a function that compares strings?

I want to make a function which compares strings.
I don't want to use equal operators (==), I want it worked only with Swift language.
First I made a function which takes 2 strings, and returns bool type.
then I looped these strings with for in syntax.
And want to compare these characters, if strings have equal value, it should return true, if not, then false. Is there any better way?
func isEqual(str1:String, str2:String) -> Bool {
var result = false
for char in str1 {
}
for char2 in str2 {
}
//Compare characters.
return result
}
== works fine with Strings in Swift. For educational purposes
(as I conclude from your comment "because I'm practicing...")
you can implement it as:
func myStringCompare(str1 : String, str2 : String) -> Bool {
if count(str1) != count(str2) {
return false
}
for (c1, c2) in zip(str1, str2) {
if c1 != c2 {
return false
}
}
return true
}
zip(str1, str2) returns a sequence of pairs from the given
sequences, this is a convenient way to enumerate the strings
"in parallel".
Once you have understood how it works, you can shorten it,
for example to:
func myStringCompare(str1 : String, str2 : String) -> Bool {
return count(str1) == count(str2) && !contains(zip(str1, str2), { $0 != $1 })
}
Comparing the string length is necessary because the zip() sequence
terminates as soon as one of the strings is exhausted. Have a look at
#drewag's answer to In Swift I would like to "join" two sequences in to a sequence of tuples
for an alternative Zip2WithNilPadding sequence.
If you don't want to use the built-in zip() function (again for
educational/self-learning purposes!) then you can use the fact
that Strings are sequences, and enumerate them in parallel using
the sequence generator. This would work not only for strings but
for arbitrary sequences, as long as the underlying elements can
be tested for equality, so let's make it a generic function:
func mySequenceCompare<S : SequenceType where S.Generator.Element : Equatable>(lseq : S, rseq : S) -> Bool {
var lgen = lseq.generate()
var rgen = rseq.generate()
// First elements (or `nil`):
var lnext = lgen.next()
var rnext = rgen.next()
while let lelem = lnext, relem = rnext {
if lelem != relem {
return false
}
// Next elements (or `nil`):
lnext = lgen.next()
rnext = rgen.next()
}
// Are both sequences exhausted?
return lnext == nil && rnext == nil
}
Tests:
mySequenceCompare("xa", "xb") // false
mySequenceCompare("xa", "xa") // true
mySequenceCompare("a", "aa") // false
mySequenceCompare("aa", "a") // false
My solution differ a little as I didn't know about the zip operator, I guess is not as efficient as the one post by Martin great use of tuple.
Great question alphonse
func isEqual(str1:String, str2:String) -> Bool {
if count(str1) != count(str2){
return false
}
for var i = 0; i < count(str1); ++i {
let idx1 = advance(str1.startIndex,i)
let idx2 = advance(str2.startIndex,i)
if str1[idx1] != str2[idx2]{
return false
}
}
return true
}
As pointed by Martin each string needs its own index, as explained by him:
"The "trick" is that "🇩🇪" is an "extended grapheme cluster" and consists of two Unicode code points, but counts as one Swift character."
Link for more details about extended grapheme cluster

Resources