Re: Passing function(whose parameter would be dynamic and the type is unknown) as a parameter to another function.
On Sunday, 8 July 2018 at 19:22:32 UTC, Timoses wrote: On Sunday, 8 July 2018 at 18:46:31 UTC, vino.B wrote: Hi All, Request you help, in the below code we pass the function "Testfun" as a parameter to another function "process" in order for the function "process" to work we have to specify the type of the parameter that is passed to the function "(T function(string, int) coRoutine, string Test, int Size) ", so now how do we pass a function whose parameter would be dynamic and the type is unknown. void process(T)(T function(string, int) coRoutine, string Test, int Size) { This would templetize the return type of the coRoutine, thus within that function you could do T returnedValue = coRoutine(string.init, int.init); alias scRType = typeof(coRoutine(string.init, int.init)); Eg: Run1 : process(&Testfun, Test, Size); void process(T ...)(T function(string, int) coRoutine, string Test) { alias scRType = typeof(coRoutine(string.init, int.init)); Run2 : process(&Testfun, Test, Size, Str1); void process(T)(T function(string, int, string) coRoutine, string Test, int Size, string Str1) { alias scRType = typeof(coRoutine(string.init, int.init, string.int)); Run3 : process(&Testfun, Test); void process(T)(T function(string, string) coRoutine, string Test, int Size) { alias scRType = typeof(coRoutine(string.init)); PFresult.get = coRoutine(args); Some what like this auto Testfun (string FFs, int Size) { return tuple(FFs, Size); } void process(T ...)(T function(T args) coRoutine, T args) { This would mean that if you pass a function that returns for example an int, it must also - take an int as argument - and process has to accept another int e.g. process((int i) => i+1, 3); if T would be (int, string) you would have to pass something like process((int i, string s) { return AliasSeq!(int, string); }, // error: not even sure how to express this 3, "hello"); where I'm not sure how to express the return type of (int, string)... Does anybody know this? Would the Tuple Dip make this possible? (https://forum.dlang.org/post/p3bdp1$2b4e$1...@digitalmars.com) alias scRType = typeof(coRoutine(T.init)); PFresult.get = coRoutine(T); void main() { string Test ="C:\\Temp\\BACKUP1"; int Size = 1; process(&Testfun, Test, Size); } Code : Working import std.stdio: writeln; import std.container.array; import std.typecons: tuple; import std.parallelism: taskPool; auto Testfun (string FFs, int Size) { return tuple(FFs, Size); } void process(T)(T function(string, int) coRoutine, string Test, int Size) { alias scRType = typeof(coRoutine(string.init, int.init)); auto PFresult = taskPool.workerLocalStorage!scRType(); PFresult.get = coRoutine(Test, Size); foreach(i; PFresult.toRange) { writeln(i[][]); } } void main() { string Test ="C:\\Temp\\BACKUP1"; int Size = 1; process(&Testfun, Test, Size); } From, Vino.B I suggest taking a look at https://dlang.org/phobos/std_traits.html . E.g. ReturnType and Parameters: int func() { return 3; } assert(is(ReturnType!func == int)); void gunc(int i, double j) {} import std.meta : AliasSeq; assert(is(Parameters!gunc == AliasSeq!(int, double))); Perhaps you could tell us what your goal is. People here might come up with a nice solution. Why do you feel like having to use templated functions in the first place? That is, what is the generic goal of the functions you are trying to define? Hi Timoses, We are converting a Power shell script to D in phased manner; the PS script has many functions and we converted few function to D in Phase 1. Phase 1: Structure of the Program Main -> Thread Manager->CoFunction1(Fs1,2,3,4,5) Main -> Thread Manager->CoFunction2(Fs1,2,3,4,5) The thread manager will call the Cofunctions and the function gets executed on “N” of file systems each of size 5-10 TB. The function that we transformed all has the same number of parameters (3) and the type was same (string, string, int), so we wrote a static thread manager as below void ptManager (T)(T function(string, string, int) coRoutine, Array!string Dirlst, string Step, int Size) { alias scRType = typeof(coRoutine(string.init, string.init, int.init)); auto PFresult = taskPool.workerLocalStorage!scRType(); ReturnType!coRoutine rData; foreach (string FFs; parallel(Dirlst[0 .. $],1)) { PFresult.get ~= coRoutine(FFs.strip, Step); } foreach(i; PFresult.toRange) { writeln(i[][]); } } void main () { ptManager(&function1, Fn1Dirlst, Step, Size); ptManager(&function2, Fn2Dirlst, Step, Age); } Phase 2: In phase 2 we are transferring few more function to the existing D code, and these functions has variable number of parameter and different type eg: Function3(string, string, string), Function(string, int, string, int). Initially I tried to re-write the ptManager function for each type of function which ended with 8 ptManager functions, Eg : ptManager1(string,
Re: How can I point an array to existing data in memory while using Better C?
On Sunday, 8 July 2018 at 21:11:53 UTC, Stijn Herreman wrote: On Sunday, 8 July 2018 at 20:27:34 UTC, Stijn Herreman wrote: I should point out that I only have a vague idea of what I'm doing, I tried things until it compiled and worked (at first glance). If there are any docs that properly explain the casting of pointers, I'd appreciate the links. I found the following works as desired: environment.d public __gshared header* GPT_header; public __gshared partition_entry* GPT_entries; main.d GPT_header = cast(header*)0x7e00; GPT_entries = cast(partition_entry*)0x8000; That still lets me access GPT_entries with an index, e.g. GPT_entries[0]. Is this how it's supposed to be done, or is there a better way still? You can also easily make slices out of pointers. E.g.: // Some backing memory int[10] x; // Take a pointer int *xp = x.ptr; // Convert pointer to D slice int[] x2 = xp[0..10]; assert (x2.length == 10); (Of course, you don't need to break it into steps in real code, and it works fine in betterC.) There's still not a lot of info out there about doing betterC, but maybe you can find some examples in the following code. It works without any druntime at all, including TypeInfo. This is the entry point after a bootloader loads the D code and enters 32b protected mode: https://gitlab.com/sarneaud/xanthe/blob/master/src/os/drivers-bios.d#L198
Re: How can I point an array to existing data in memory while using Better C?
On Sunday, 8 July 2018 at 20:27:34 UTC, Stijn Herreman wrote: public __gshared partition_entry[128] GPT_entries; That's a block of memory rather than a pointer, so there's nothing to reassign there; the .ptr property is read-only there (and your reassignment is instead copying TO the address, instead of copying the address). Instead just use a partition_entry[] GPT_entries; and it is a reassignable pointer.
Re: How can I point an array to existing data in memory while using Better C?
On Sunday, 8 July 2018 at 21:10:50 UTC, ag0aep6g wrote: On 07/08/2018 10:27 PM, Stijn Herreman wrote: https://forum.dlang.org/thread/ddckhvcxlyuvuiyaz...@forum.dlang.org is similar to what I want to do, but the code by Adam cannot be used when using Better C (I assume slicing isn't supported). Slicing a pointer works fine for me with -betterC (DMD 2.081.0). Why do you think it doesn't work? Oh it does, thank you for the example. I don't recall the code I had, but the compiler gave an error about TypeInfo (or a similar object) not being supported.
Re: How can I point an array to existing data in memory while using Better C?
On 07/08/2018 10:27 PM, Stijn Herreman wrote: https://forum.dlang.org/thread/ddckhvcxlyuvuiyaz...@forum.dlang.org is similar to what I want to do, but the code by Adam cannot be used when using Better C (I assume slicing isn't supported). Slicing a pointer works fine for me with -betterC (DMD 2.081.0). Why do you think it doesn't work? I have the following code: environment.d public __gshared header* GPT_header; public __gshared partition_entry[128] GPT_entries; main.d GPT_header = cast(header*)0x7e00; *(GPT_entries).ptr = *(cast(partition_entry*)0x8000); Disassembled, the code from main.d looks like this: mov ds:_D4boot11environment10GPT_headerPSQBg3gpt6header, 7E00h mov esi, 8000h mov edi, offset _D4boot11environment11GPT_entriesG128SQBk3gpt15partition_entry mov ecx, 20h rep movsd So GPT_header points to 0x7e00 as desired, but for GPT_entries it copies the first array element to a different location and makes it point to that location instead of 0x8000. As far as I can tell, the assembler code does exactly what your D code says: Copy one partition_entry (size is apparently 128 bytes?) from 0x8000 to GPT_entries[0]. If you want to set the .ptr of GPT_entries explicitly, I don't know if that's possible. But the code you have is not how you'd do it. You're dereferencing GPT_entries.ptr, you're not setting it. You can get a partition_entry[] that starts at 0x8000 by casting a pointer and slicing that. Works for me with -betterC: struct partition_entry {} public __gshared partition_entry[] GPT_entries; extern(C) void main() { GPT_entries = (cast(partition_entry*) 0x8000)[0 .. 128]; }
Re: How can I point an array to existing data in memory while using Better C?
On Sunday, 8 July 2018 at 20:27:34 UTC, Stijn Herreman wrote: I should point out that I only have a vague idea of what I'm doing, I tried things until it compiled and worked (at first glance). If there are any docs that properly explain the casting of pointers, I'd appreciate the links. I found the following works as desired: environment.d public __gshared header* GPT_header; public __gshared partition_entry* GPT_entries; main.d GPT_header = cast(header*)0x7e00; GPT_entries = cast(partition_entry*)0x8000; That still lets me access GPT_entries with an index, e.g. GPT_entries[0]. Is this how it's supposed to be done, or is there a better way still?
How can I point an array to existing data in memory while using Better C?
https://forum.dlang.org/thread/ddckhvcxlyuvuiyaz...@forum.dlang.org is similar to what I want to do, but the code by Adam cannot be used when using Better C (I assume slicing isn't supported). I have the following code: environment.d public __gshared header* GPT_header; public __gshared partition_entry[128] GPT_entries; main.d GPT_header = cast(header*)0x7e00; *(GPT_entries).ptr = *(cast(partition_entry*)0x8000); Disassembled, the code from main.d looks like this: mov ds:_D4boot11environment10GPT_headerPSQBg3gpt6header, 7E00h mov esi, 8000h mov edi, offset _D4boot11environment11GPT_entriesG128SQBk3gpt15partition_entry mov ecx, 20h rep movsd So GPT_header points to 0x7e00 as desired, but for GPT_entries it copies the first array element to a different location and makes it point to that location instead of 0x8000. I should point out that I only have a vague idea of what I'm doing, I tried things until it compiled and worked (at first glance). If there are any docs that properly explain the casting of pointers, I'd appreciate the links.
Re: Passing function(whose parameter would be dynamic and the type is unknown) as a parameter to another function.
On Sunday, 8 July 2018 at 18:46:31 UTC, vino.B wrote: Hi All, Request you help, in the below code we pass the function "Testfun" as a parameter to another function "process" in order for the function "process" to work we have to specify the type of the parameter that is passed to the function "(T function(string, int) coRoutine, string Test, int Size) ", so now how do we pass a function whose parameter would be dynamic and the type is unknown. void process(T)(T function(string, int) coRoutine, string Test, int Size) { This would templetize the return type of the coRoutine, thus within that function you could do T returnedValue = coRoutine(string.init, int.init); alias scRType = typeof(coRoutine(string.init, int.init)); Eg: Run1 : process(&Testfun, Test, Size); void process(T ...)(T function(string, int) coRoutine, string Test) { alias scRType = typeof(coRoutine(string.init, int.init)); Run2 : process(&Testfun, Test, Size, Str1); void process(T)(T function(string, int, string) coRoutine, string Test, int Size, string Str1) { alias scRType = typeof(coRoutine(string.init, int.init, string.int)); Run3 : process(&Testfun, Test); void process(T)(T function(string, string) coRoutine, string Test, int Size) { alias scRType = typeof(coRoutine(string.init)); PFresult.get = coRoutine(args); Some what like this auto Testfun (string FFs, int Size) { return tuple(FFs, Size); } void process(T ...)(T function(T args) coRoutine, T args) { This would mean that if you pass a function that returns for example an int, it must also - take an int as argument - and process has to accept another int e.g. process((int i) => i+1, 3); if T would be (int, string) you would have to pass something like process((int i, string s) { return AliasSeq!(int, string); }, // error: not even sure how to express this 3, "hello"); where I'm not sure how to express the return type of (int, string)... Does anybody know this? Would the Tuple Dip make this possible? (https://forum.dlang.org/post/p3bdp1$2b4e$1...@digitalmars.com) alias scRType = typeof(coRoutine(T.init)); PFresult.get = coRoutine(T); void main() { string Test ="C:\\Temp\\BACKUP1"; int Size = 1; process(&Testfun, Test, Size); } Code : Working import std.stdio: writeln; import std.container.array; import std.typecons: tuple; import std.parallelism: taskPool; auto Testfun (string FFs, int Size) { return tuple(FFs, Size); } void process(T)(T function(string, int) coRoutine, string Test, int Size) { alias scRType = typeof(coRoutine(string.init, int.init)); auto PFresult = taskPool.workerLocalStorage!scRType(); PFresult.get = coRoutine(Test, Size); foreach(i; PFresult.toRange) { writeln(i[][]); } } void main() { string Test ="C:\\Temp\\BACKUP1"; int Size = 1; process(&Testfun, Test, Size); } From, Vino.B I suggest taking a look at https://dlang.org/phobos/std_traits.html . E.g. ReturnType and Parameters: int func() { return 3; } assert(is(ReturnType!func == int)); void gunc(int i, double j) {} import std.meta : AliasSeq; assert(is(Parameters!gunc == AliasSeq!(int, double))); Perhaps you could tell us what your goal is. People here might come up with a nice solution. Why do you feel like having to use templated functions in the first place? That is, what is the generic goal of the functions you are trying to define?
Re: Passing function(whose parameter would be dynamic and the type is unknown) as a parameter to another function.
On Sunday, 8 July 2018 at 18:46:31 UTC, vino.B wrote: Request you help, in the below code we pass the function "Testfun" as a parameter to another function "process" in order for the function "process" to work we have to specify the type of the parameter that is passed to the function "(T function(string, int) coRoutine, string Test, int Size) ", so now how do we pass a function whose parameter would be dynamic and the type is unknown. What do you mean with a "function with dynamic parameters" and "unknown type"? But how about ´´´ void main() { process!fun(); } void process(alias coRoutine, T...)(T params) { coRoutine(params); } auto fun(T...)(T params) { } ´´´
Passing function(whose parameter would be dynamic and the type is unknown) as a parameter to another function.
Hi All, Request you help, in the below code we pass the function "Testfun" as a parameter to another function "process" in order for the function "process" to work we have to specify the type of the parameter that is passed to the function "(T function(string, int) coRoutine, string Test, int Size) ", so now how do we pass a function whose parameter would be dynamic and the type is unknown. void process(T)(T function(string, int) coRoutine, string Test, int Size) { alias scRType = typeof(coRoutine(string.init, int.init)); Eg: Run1 : process(&Testfun, Test, Size); void process(T ...)(T function(string, int) coRoutine, string Test) { alias scRType = typeof(coRoutine(string.init, int.init)); Run2 : process(&Testfun, Test, Size, Str1); void process(T)(T function(string, int, string) coRoutine, string Test, int Size, string Str1) { alias scRType = typeof(coRoutine(string.init, int.init, string.int)); Run3 : process(&Testfun, Test); void process(T)(T function(string, string) coRoutine, string Test, int Size) { alias scRType = typeof(coRoutine(string.init)); PFresult.get = coRoutine(args); Some what like this auto Testfun (string FFs, int Size) { return tuple(FFs, Size); } void process(T ...)(T function(T args) coRoutine, T args) { alias scRType = typeof(coRoutine(T.init)); PFresult.get = coRoutine(T); void main() { string Test ="C:\\Temp\\BACKUP1"; int Size = 1; process(&Testfun, Test, Size); } Code : Working import std.stdio: writeln; import std.container.array; import std.typecons: tuple; import std.parallelism: taskPool; auto Testfun (string FFs, int Size) { return tuple(FFs, Size); } void process(T)(T function(string, int) coRoutine, string Test, int Size) { alias scRType = typeof(coRoutine(string.init, int.init)); auto PFresult = taskPool.workerLocalStorage!scRType(); PFresult.get = coRoutine(Test, Size); foreach(i; PFresult.toRange) { writeln(i[][]); } } void main() { string Test ="C:\\Temp\\BACKUP1"; int Size = 1; process(&Testfun, Test, Size); } From, Vino.B
Re: ctRegex not matching correctly at word boundaries
On Sunday, 8 July 2018 at 10:58:24 UTC, rikki cattermole wrote: On 08/07/2018 10:37 PM, Ivo wrote: First of all, I'm not an expert about regular expression syntax, however I believe there might be some strange bug when using ctRegex. Consider the following code: void main() { import std.regex: ctRegex, matchFirst; auto expression = ctRegex!("\bis\b"); auto expression = ctRegex!(`\bis\b`); \b is bell escape code as per ASCII Thanks a lot. I forgot to put an r before the quotes. My silly mistake.
Re: ctRegex not matching correctly at word boundaries
On 08/07/2018 10:37 PM, Ivo wrote: First of all, I'm not an expert about regular expression syntax, however I believe there might be some strange bug when using ctRegex. Consider the following code: void main() { import std.regex: ctRegex, matchFirst; auto expression = ctRegex!("\bis\b"); auto expression = ctRegex!(`\bis\b`); \b is bell escape code as per ASCII
ctRegex not matching correctly at word boundaries
First of all, I'm not an expert about regular expression syntax, however I believe there might be some strange bug when using ctRegex. Consider the following code: void main() { import std.regex: ctRegex, matchFirst; auto expression = ctRegex!("\bis\b"); auto match = matchFirst("This island is beautiful", expression); if(match.empty) writeln("no match"); else writeln("match"); } when I compile and run I get the output "no match". Looking online I found this website https://www.regular-expressions.info/wordboundaries.html where they explain that the above regex should match that string. So I don't understand what's wrong with my code. P.S.: using regex instead of ctRegex doesn't solve the problem.