July 10, 2018 Re: Passing function(whose parameter would be dynamic and the type is unknown) as a parameter to another function. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex | On Monday, 9 July 2018 at 18:07:49 UTC, Alex wrote: > On Monday, 9 July 2018 at 17:26:30 UTC, vino.B wrote: >> >> Request Help: >> void process(alias coRoutine, T...)(Array!string Dirlst, T params) >> { >> ReturnType!coRoutine rData; ///// This line is not working >> alias scRType = typeof(coRoutine(string.init, T.init)); >> auto PFresult = taskPool.workerLocalStorage!scRType(); >> foreach (string FFs; parallel(Dirlst[],1)) { PFresult.get ~= coRoutine(FFs, params); } >> foreach(i; PFresult.toRange) { rData ~= i[][]; } >> } >> >> Error: >> test.d(206): Error: template instance `std.traits.ReturnType!(coAgedDirClean)` does not match template declaration ReturnType(func...) if (func.length == 1 && isCallable!func) > > Yeah... for ReturnType to work, you need a function, but you have only a template. > > The easy solution is to execute the template and to ask the result for its type: > > ´´´ > void main() > { > process!fun(); > } > > void process(alias coRoutine, T...)(T params) > { > auto res = coRoutine(params); > pragma(msg, typeof(res)); > } > > auto fun(T...)(T params) > { > return 42; > } > ´´´ > > If you need it in advance... It is a little bit longer. There was a place, where I used this once... > > See > https://run.dlang.io/is/Xy6Xf4 > > However, I wonder why you need this, especially as your process is void. Why not just using auto for results of the coroutines? Hi Alex, The reason the I am storing the output of "PFresult.toRange" to another array "rData" is that the output of the PFresult.toRange is different each time we execute the code.(Data is correct) but the way the it output is different. Is there any way to get the result in a single array - Whole Data. Single array - Whole Data ["C:\\Temp\\TEAM1\\Test Result-Team1.docx", 2018-Jun-28 17:37:45.9376229, "C:\\Temp\\TEAM2\\Test Result-Team2.docx", 2018-Jun-28 17:37:45.9376229, "C:\\Temp\\SAPNAS3\\TEAM3\\Test Result-Team3.docx", 2018-Jun-28 17:37:45.9376229 ] One array - For Each Data [ C:\\Temp\\TEAM1\\Test Result-Team1.docx", 2018-Jun-28 17:37:45.9376229] - arr1 [ C:\\Temp\\TEAM2\\Test Result-Team2.docx", 2018-Jun-28 17:37:45.9376229] - arr2 [ C:\\Temp\\TEAM3\\Test Result-Team3.docx", 2018-Jun-28 17:37:45.9376229] - arr3 The code in the program. foreach(i; PFresult.toRange) { rData ~= i[][]; } if (!rData[].empty) { rData[].sort!((a,b) => a[1] < b[1]).each!(e => logF.writefln!"%-83s %.20s"(e[0].replace(`\\?\`, ""), e[1].to!string)); } From, Vino.B |
July 10, 2018 Re: Passing function(whose parameter would be dynamic and the type is unknown) as a parameter to another function. | ||||
---|---|---|---|---|
| ||||
Posted in reply to vino.B | On Tuesday, 10 July 2018 at 14:38:03 UTC, vino.B wrote: > Hi Alex, > > The reason the I am storing the output of "PFresult.toRange" to another array "rData" is that the output of the PFresult.toRange is different each time we execute the code.(Data is correct) but the way the it output is different. Is there any way to get the result in a single array - Whole Data. > > Single array - Whole Data > ["C:\\Temp\\TEAM1\\Test Result-Team1.docx", 2018-Jun-28 17:37:45.9376229, > "C:\\Temp\\TEAM2\\Test Result-Team2.docx", 2018-Jun-28 17:37:45.9376229, > "C:\\Temp\\SAPNAS3\\TEAM3\\Test Result-Team3.docx", 2018-Jun-28 17:37:45.9376229 > ] > > One array - For Each Data > [ C:\\Temp\\TEAM1\\Test Result-Team1.docx", 2018-Jun-28 17:37:45.9376229] - arr1 > [ C:\\Temp\\TEAM2\\Test Result-Team2.docx", 2018-Jun-28 17:37:45.9376229] - arr2 > [ C:\\Temp\\TEAM3\\Test Result-Team3.docx", 2018-Jun-28 17:37:45.9376229] - arr3 > > The code in the program. > > foreach(i; PFresult.toRange) { rData ~= i[][]; } > if (!rData[].empty) { rData[].sort!((a,b) => a[1] < b[1]).each!(e => logF.writefln!"%-83s %.20s"(e[0].replace(`\\?\`, ""), e[1].to!string)); } > > From, > Vino.B Not sure, if I get your point, but are you aware of tuples? https://dlang.org/phobos/std_typecons.html#tuple |
July 10, 2018 Re: Passing function(whose parameter would be dynamic and the type is unknown) as a parameter to another function. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex | On Tuesday, 10 July 2018 at 14:50:53 UTC, Alex wrote: > On Tuesday, 10 July 2018 at 14:38:03 UTC, vino.B wrote: >> Hi Alex, >> >> The reason the I am storing the output of "PFresult.toRange" to another array "rData" is that the output of the PFresult.toRange is different each time we execute the code.(Data is correct) but the way the it output is different. Is there any way to get the result in a single array - Whole Data. >> >> Single array - Whole Data >> ["C:\\Temp\\TEAM1\\Test Result-Team1.docx", 2018-Jun-28 17:37:45.9376229, >> "C:\\Temp\\TEAM2\\Test Result-Team2.docx", 2018-Jun-28 17:37:45.9376229, >> "C:\\Temp\\SAPNAS3\\TEAM3\\Test Result-Team3.docx", 2018-Jun-28 17:37:45.9376229 >> ] >> >> One array - For Each Data >> [ C:\\Temp\\TEAM1\\Test Result-Team1.docx", 2018-Jun-28 17:37:45.9376229] - arr1 >> [ C:\\Temp\\TEAM2\\Test Result-Team2.docx", 2018-Jun-28 17:37:45.9376229] - arr2 >> [ C:\\Temp\\TEAM3\\Test Result-Team3.docx", 2018-Jun-28 17:37:45.9376229] - arr3 >> >> The code in the program. >> >> foreach(i; PFresult.toRange) { rData ~= i[][]; } >> if (!rData[].empty) { rData[].sort!((a,b) => a[1] < b[1]).each!(e => logF.writefln!"%-83s %.20s"(e[0].replace(`\\?\`, ""), e[1].to!string)); } >> >> From, >> Vino.B > > Not sure, if I get your point, but are you aware of tuples? > https://dlang.org/phobos/std_typecons.html#tuple Hi Alex, I am getting the output as tuples of multiple arrays, but the requirement is to get the all the tuple in a single array like the below so that we can perform sorting and printing the output is easy. [ "C:\\Temp\\TEAM1\\Test Result-Team1.docx", 2018-Jun-28 17:37:45.9376229, "C:\\Temp\\TEAM2\\Test Result-Team2.docx", 2018-Jun-28 17:37:45.9376229, "C:\\Temp\\SAPNAS3\\TEAM3\\Test Result-Team3.docx", 2018-Jun-28 17:37:45.9376229 ] if (!rData[].empty) { rData[].sort!((a,b) => a[1] < b[1]).each!(e => logF.writefln!"%-83s %.20s"(e[0], e[1].to!string)); } From, Vino.B |
July 10, 2018 Re: Passing function(whose parameter would be dynamic and the type is unknown) as a parameter to another function. | ||||
---|---|---|---|---|
| ||||
Posted in reply to vino.B | On Tuesday, 10 July 2018 at 14:58:42 UTC, vino.B wrote:
> Hi Alex,
>
> I am getting the output as tuples of multiple arrays, but the requirement is to get the all the tuple in a single array like the below so that we can perform sorting and printing the output is easy.
Something along the way
unittest
{
import std.typecons : Tuple, tuple;
alias pair = Tuple!(string, int);
pair[] values;
values ~= tuple("du", 7);
values ~= tuple("Vino", 3);
values ~= tuple(",", 4);
values ~= tuple("hur", 5);
values ~= tuple("Hej", 2);
values ~= tuple("?", 8);
values ~= tuple("mår", 6);
import std.algorithm : sort, each;
import std.stdio : write;
values.sort!((p1, p2) => p1[1] < p2[1])
.each!(p => write(p[0] ~ " "));
}
?
|
Copyright © 1999-2021 by the D Language Foundation