www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Passing Function as an argument to another Function

reply Vino <vino.bheeman hotmail.com> writes:
Hi All,

   Request your help on the below code, I want to send the name of 
the function ( First and Second) from main as an argument to 
another function(Mid) and the function "Mid" has to execute the 
function(First and Second).

Program:
import std.stdio;

void First (string Ftext) {
writeln("First :", Ftext);
}

void Second (string Stext) {
writeln("Second :", Stext);
}

void Mid( string Fun, string Mtext) {
Fun(Mtext);
}

void main () {
string Ftext = "FTest1";
string Stext = "STest2";
Mid(First, Mtext);
Mid(Second, Stext);
}

From,
Vino.B
Dec 04 2017
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 04/12/2017 8:22 AM, Vino wrote:
 Hi All,
 
    Request your help on the below code, I want to send the name of the 
 function ( First and Second) from main as an argument to another 
 function(Mid) and the function "Mid" has to execute the function(First 
 and Second).
 
 Program:
 import std.stdio;
 
 void First (string Ftext) {
 writeln("First :", Ftext);
 }
 
 void Second (string Stext) {
 writeln("Second :", Stext);
 }
 
 void Mid( string Fun, string Mtext) {
 Fun(Mtext);
 }
 
 void main () {
 string Ftext = "FTest1";
 string Stext = "STest2";
 Mid(First, Mtext);
 Mid(Second, Stext);
 }
 
 From,
 Vino.B
Not going to happen like that. void mid(void function(string) func, string mtext) { func(mtext); } void main() { string ftext = "ftest1", stext = "stest2"; mid(&first, ftext); mid(&second, stext); }
Dec 04 2017
next sibling parent Andrea Fontana <nospam example.com> writes:
On Monday, 4 December 2017 at 08:27:10 UTC, rikki cattermole 
wrote:
 On 04/12/2017 8:22 AM, Vino wrote:
 Hi All,
 
    Request your help on the below code, I want to send the 
 name of the function ( First and Second) from main as an 
 argument to another function(Mid) and the function "Mid" has 
 to execute the function(First and Second).
 
 Program:
 import std.stdio;
 
 void First (string Ftext) {
 writeln("First :", Ftext);
 }
 
 void Second (string Stext) {
 writeln("Second :", Stext);
 }
 
 void Mid( string Fun, string Mtext) {
 Fun(Mtext);
 }
 
 void main () {
 string Ftext = "FTest1";
 string Stext = "STest2";
 Mid(First, Mtext);
 Mid(Second, Stext);
 }
 
 From,
 Vino.B
Maybe: import std.stdio; void Mid(alias Fun)(string Mtext) { Fun(Mtext); } void main () { string Ftext = "FTest1"; string Stext = "STest2"; Mid!First(Ftext); Mid!Second(Stext); }
Dec 04 2017
prev sibling parent reply Vino <vino.bheeman hotmail.com> writes:
On Monday, 4 December 2017 at 08:27:10 UTC, rikki cattermole 
wrote:
 On 04/12/2017 8:22 AM, Vino wrote:
 Hi All,
 
    Request your help on the below code, I want to send the 
 name of the function ( First and Second) from main as an 
 argument to another function(Mid) and the function "Mid" has 
 to execute the function(First and Second).
 
 Program:
 import std.stdio;
 
 void First (string Ftext) {
 writeln("First :", Ftext);
 }
 
 void Second (string Stext) {
 writeln("Second :", Stext);
 }
 
 void Mid( string Fun, string Mtext) {
 Fun(Mtext);
 }
 
 void main () {
 string Ftext = "FTest1";
 string Stext = "STest2";
 Mid(First, Mtext);
 Mid(Second, Stext);
 }
 
 From,
 Vino.B
Not going to happen like that. void mid(void function(string) func, string mtext) { func(mtext); } void main() { string ftext = "ftest1", stext = "stest2"; mid(&first, ftext); mid(&second, stext); }
Hi Rikki, Thank you very much, I tired the above code and it is working, so another help on the same topic. IF my function (First) of below type when who do we define the Mid function Array!(Tuple!(string, string)) First(string Ftext) Tried the below but no luck void mid(Array!(Tuple!(string, string)) function(string) func, string mtext) { func(mtext); } From, Vino.B
Dec 04 2017
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 04/12/2017 10:36 AM, Vino wrote:
 Hi Rikki,
 
    Thank you very much, I tired the above code and it is working, so 
 another help on the same topic.
 IF my function (First) of below type when who do we define the Mid function
 
 Array!(Tuple!(string, string))  First(string Ftext)
 
 Tried the below  but no luck
 void mid(Array!(Tuple!(string, string)) function(string) func, string 
 mtext) {
       func(mtext);
 }
Looks valid. mid(&First, "Str"); So error + full source please.
Dec 04 2017
parent reply Vino <vino.bheeman hotmail.com> writes:
On Monday, 4 December 2017 at 10:46:03 UTC, rikki cattermole 
wrote:
 On 04/12/2017 10:36 AM, Vino wrote:
 Hi Rikki,
 
    Thank you very much, I tired the above code and it is 
 working, so another help on the same topic.
 IF my function (First) of below type when who do we define the 
 Mid function
 
 Array!(Tuple!(string, string))  First(string Ftext)
 
 Tried the below  but no luck
 void mid(Array!(Tuple!(string, string)) function(string) func, 
 string mtext) {
       func(mtext);
 }
Looks valid. mid(&First, "Str"); So error + full source please.
Hi Rikki, The original program is as below Full Program: import core.stdc.stdlib: exit; import std.algorithm: all, among, canFind, each, endsWith, filter, map, setDifference, sort, uniq, reduce; import std.array: join; import std.container.array; import std.conv: to; import std.datetime.systime: Clock, days, SysTime; import std.file: dirEntries, exists, getcwd, isFile, mkdir, remove, rmdirRecurse, SpanMode; import std.format: format; import std.net.isemail; import std.parallelism: parallel, taskPool; import std.path: baseName, dirName, globMatch, isValidFilename, isValidPath; import std.process: execute; import std.range: chain, chunks, empty, zip; import std.stdio: File, writefln, writeln; import std.string: chomp, chop, isNumeric, split, strip; import std.typecons: tuple, Tuple; import std.uni: isAlpha, isWhite, toLower; Array!(Tuple!(string, string)) coAgedDirClean (string FFs, string Step, int DirAged) { auto dFiles = Array!(Tuple!(string, string))(dirEntries(FFs, SpanMode.shallow).filter!(a => a.isDir && !globMatch(a.baseName, "*DND*") && a.timeCreated < dtAges(DirAged)).map!(a => tuple(a.name, a.timeCreated.toSimpleString[0 .. 20]))); if (Step == "run") { dFiles.each!(f => f[0].rmdirRecurse); } return dFiles; } void ptProcessFiles() (Array!string Dirlst, Array!(Tuple!(string, string)) function(string, string, int) coRoutine, File logF, File logE, string Step, int Aged) { Array!(Tuple!(string, string)) PFtext, PFdata; auto PFresult = taskPool.workerLocalStorage(PFtext); try { foreach (string FFs; parallel(Dirlst[0 .. $],1)) { PFresult.get ~= coRoutine(FFs.strip, Step, Aged); } logF.writeln("Function \t : Delete of the Files which are not placed in correct Location and list those deleted"); logF.writeln("Dir. Scanned \t :", Dirlst[]); logF.writeln("************************************************************************************"); logF.writefln("%-63s %.20s", "File Name", "CreationTime"); logF.writeln("************************************************************************************"); foreach(i; PFresult.toRange) { PFdata ~= i[][]; } if (!PFdata.empty) { logF.writefln("%(%-(%-63s %s %)\n%)", PFdata[].sort!((a,b) => a[0] < b[0])); } logF.writeln("************************************************************************************"); } catch (Exception e) { logE.writeln(e.msg); } } SysTime dtAges (int Ages) { auto ct2 = Clock.currTime(); auto st2 = ct2 + days(-Ages); return st2; } void main () { auto CleanDirlst = "C:\\Temp\\BACKUP, C:\\Temp\\EXPORT"; auto logF = File("C:\\Users\\bheev1\\Desktop\\Current\\Script\\D\\Logs\\Rlog.log", "a"); auto logE = File("C:\\Users\\bheev1\\Desktop\\Current\\Script\\D\\Logs\\Elog.log", "a"); string Step = "dryrun"; int DirAged = 1; ptProcessFiles(CleanDirlst, &coAgedDirClean, logF, logE, Step, DirAged); } Error: FunTest.d(52): Error: template FunTest.ptProcessFiles cannot deduce function from argument types !()(string, Array!(Tuple!(string, string)) function(string FFs, string Step, int DirAged), File, File, string, int), candidates are: FunTest.d(24): FunTest.ptProcessFiles()(Array!string Dirlst, Array!(Tuple!(string, string)) function(string, string, int) coRoutine, File logF, File logE , string Step, int Aged) Failed: ["dmd", "-v", "-o-", "FunTest.d", "-I."] From, Vino.B
Dec 04 2017
next sibling parent reply codephantom <me noyb.com> writes:
On Monday, 4 December 2017 at 11:05:22 UTC, Vino wrote:
  The original program is as below

 Error:

 FunTest.d(52): Error: template FunTest.ptProcessFiles cannot 
 deduce function from argument types !()(string, 
 Array!(Tuple!(string, string)) function(string FFs,
  string Step, int DirAged), File, File, string, int),
//auto CleanDirlst = "C:\\Temp\\BACKUP, C:\\Temp\\EXPORT"; Array!string CleanDirlst = ["C:\\Temp\\BACKUP, C:\\Temp\\EXPORT"];
Dec 04 2017
parent reply codephantom <me noyb.com> writes:
On Monday, 4 December 2017 at 11:30:02 UTC, codephantom wrote:
 On Monday, 4 December 2017 at 11:05:22 UTC, Vino wrote:
  The original program is as below

 Error:

 FunTest.d(52): Error: template FunTest.ptProcessFiles cannot 
 deduce function from argument types !()(string, 
 Array!(Tuple!(string, string)) function(string FFs,
  string Step, int DirAged), File, File, string, int),
//auto CleanDirlst = "C:\\Temp\\BACKUP, C:\\Temp\\EXPORT"; Array!string CleanDirlst = ["C:\\Temp\\BACKUP, C:\\Temp\\EXPORT"];
grrr... //auto CleanDirlst = "C:\\Temp\\BACKUP, C:\\Temp\\EXPORT"; Array!string CleanDirlst = ["C:\\Temp\\BACKUP", "C:\\Temp\\EXPORT"];
Dec 04 2017
parent reply Vino <vino.bheeman hotmail.com> writes:
On Monday, 4 December 2017 at 11:41:06 UTC, codephantom wrote:
 On Monday, 4 December 2017 at 11:30:02 UTC, codephantom wrote:
 On Monday, 4 December 2017 at 11:05:22 UTC, Vino wrote:
  The original program is as below

 Error:

 FunTest.d(52): Error: template FunTest.ptProcessFiles cannot 
 deduce function from argument types !()(string, 
 Array!(Tuple!(string, string)) function(string FFs,
  string Step, int DirAged), File, File, string, int),
//auto CleanDirlst = "C:\\Temp\\BACKUP, C:\\Temp\\EXPORT"; Array!string CleanDirlst = ["C:\\Temp\\BACKUP, C:\\Temp\\EXPORT"];
grrr... //auto CleanDirlst = "C:\\Temp\\BACKUP, C:\\Temp\\EXPORT"; Array!string CleanDirlst = ["C:\\Temp\\BACKUP", "C:\\Temp\\EXPORT"];
Hi, Thank you very much, request your help on 2 further questions Q1 : if the Variable CleanDirlst is defined as "auto" can we define "auto" as below(auto Dirlst). void ptProcessFiles() (auto Dirlst, Array!(Tuple!(string, string)) function(string, string, int) coRoutine, File logF, File logE, string Step, int Aged) Q2 : How do we define an "auto" function(auto function(string, string, int) coRoutine) void ptProcessFiles() (auto Dirlst, auto function(string, string, int) coRoutine, File logF, File logE, string Step, int Aged) From, Vino.B
Dec 04 2017
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/04/2017 04:52 AM, Vino wrote:

 if the Variable  CleanDirlst is defined as "auto"
Every expression has a type. 'auto' in that context (or 'const', etc.) just helps with not spelling-out that type. You can see the type with pragma(msg) and typeof: auto someExpression = [ "one" : 1, "two" : 2 ]; pragma(msg, typeof(someExpression)); // Prints int[string]
  can we define "auto" as below(auto Dirlst).
 void ptProcessFiles() (auto Dirlst,  Array!(Tuple!(string, string))
 function(string, string, int) coRoutine, File logF, File logE, string
 Step, int Aged)
You can use templates. Here is my introduction to the concept: http://ddili.org/ders/d.en/templates.html However, in this case a simple 'alias' will do: alias MyListType = int[string]; void foo(MyListType arg) { // ... }
 Q2 :
 How do we define an "auto" function(auto function(string, string, int)
 coRoutine)
 void ptProcessFiles() (auto Dirlst,  auto function(string, string, int)
 coRoutine, File logF, File logE, string Step, int Aged)
alias helps in that case as well: alias MyCoRoutineType = MyListType function(string, string, int); void ptProcessFiles(MyListType Dirlst, MyCoRoutineType coRoutine, File logF, File logE, string Step, int Aged) { // ... } Likewise, ptProcessFiles can be a template as well, where the type of coRoutine can be a template argument but not needed in this case. Ali
Dec 04 2017
parent Vino <vino.bheeman hotmail.com> writes:
On Monday, 4 December 2017 at 19:25:15 UTC, Ali Çehreli wrote:
 On 12/04/2017 04:52 AM, Vino wrote:

 [...]
Every expression has a type. 'auto' in that context (or 'const', etc.) just helps with not spelling-out that type. You can see the type with pragma(msg) and typeof: [...]
Hi Ali, Thank you very much, was able to resolve the issue using the template method. From, Vino.B
Dec 05 2017
prev sibling parent reply kdevel <kdevel vogtner.de> writes:
On Monday, 4 December 2017 at 11:05:22 UTC, Vino wrote:
 On Monday, 4 December 2017 at 10:46:03 UTC, rikki cattermole 
 wrote:
 FunTest.d(52): Error: template FunTest.ptProcessFiles cannot 
 deduce function from argument types !()(string, 
 Array!(Tuple!(string, string)) function(string FFs,
  string Step, int DirAged), File, File, string, int), 
 candidates are:
 FunTest.d(24):        FunTest.ptProcessFiles()(Array!string 
 Dirlst, Array!(Tuple!(string, string)) function(string, string, 
 int) coRoutine, File logF, File logE
 , string Step, int Aged)
 Failed: ["dmd", "-v", "-o-", "FunTest.d", "-I."]
Why do I get a different error?: .../linux/bin64/../../src/phobos/std/datetime.d(106): Error: module std.datetime from file .../linux/bin64/../../src/phobos/std/datetime.d conflicts with package name datetime
Dec 04 2017
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Monday, December 04, 2017 20:02:30 kdevel via Digitalmars-d-learn wrote:
 On Monday, 4 December 2017 at 11:05:22 UTC, Vino wrote:
 On Monday, 4 December 2017 at 10:46:03 UTC, rikki cattermole
 wrote:

 FunTest.d(52): Error: template FunTest.ptProcessFiles cannot
 deduce function from argument types !()(string,
 Array!(Tuple!(string, string)) function(string FFs,

  string Step, int DirAged), File, File, string, int),

 candidates are:
 FunTest.d(24):        FunTest.ptProcessFiles()(Array!string
 Dirlst, Array!(Tuple!(string, string)) function(string, string,
 int) coRoutine, File logF, File logE
 , string Step, int Aged)
 Failed: ["dmd", "-v", "-o-", "FunTest.d", "-I."]
Why do I get a different error?: .../linux/bin64/../../src/phobos/std/datetime.d(106): Error: module std.datetime from file .../linux/bin64/../../src/phobos/std/datetime.d conflicts with package name datetime
That implies you have a borked install of dmd. How did you install it? You didn't do something like unzip a newer release on top of an older one did you? If you're using the zip file to install dmd, you have to blow away the previous install before copying over the new one, or you're screwed anytime that files get removed or moved around. I wouldn't expect that particular error if dmd were installed via the package manager. In 2.076.0, std.datetime was split into a package. So, if you somehow ended up with both the old files and new ones, there's a conflict. With 2.076.0, it could result in linker problems, because the compiler didn't properly detected it, but it was since fixed so that the compiler sees that you have both module and a package of the same name and gives an error. - Jonathan M Davis
Dec 04 2017