Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 04, 2017 Passing Function as an argument to another Function | ||||
---|---|---|---|---|
| ||||
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 |
December 04, 2017 Re: Passing Function as an argument to another Function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vino | 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);
}
|
December 04, 2017 Re: Passing Function as an argument to another Function | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | 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);
}
|
December 04, 2017 Re: Passing Function as an argument to another Function | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | 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
|
December 04, 2017 Re: Passing Function as an argument to another Function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vino | 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.
|
December 04, 2017 Re: Passing Function as an argument to another Function | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | 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 |
December 04, 2017 Re: Passing Function as an argument to another Function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vino | 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"];
|
December 04, 2017 Re: Passing Function as an argument to another Function | ||||
---|---|---|---|---|
| ||||
Posted in reply to codephantom | 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"];
|
December 04, 2017 Re: Passing Function as an argument to another Function | ||||
---|---|---|---|---|
| ||||
Posted in reply to codephantom | 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
|
December 04, 2017 Re: Passing Function as an argument to another Function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vino | 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 |
Copyright © 1999-2021 by the D Language Foundation