trouble with associative Arrays

2024-01-20 Thread atzensepp via Digitalmars-d-learn
Hello, I am new with D and want to convert a c program for a csv file manipulation with exhaustive dynamic memory mechanics to D . When reading a CSV-file line by line I would like to create an associative array to get the row values by the value in the second column. Although I save the row

Re: trouble with associative Arrays

2024-01-20 Thread atzensepp via Digitalmars-d-learn
Thank you T for your hint. This worked perfectly On Saturday, 20 January 2024 at 14:44:49 UTC, H. S. Teoh wrote: Because .byLine reuses its line buffer. You want .byLineCopy instead. The section looks now simpler although I guess that there are more appropriate mechanisms available (csvreader

Partial function application (Currying)

2024-01-20 Thread atzensepp via Digitalmars-d-learn
Hello, D-Language allows for anonymous functions. Is there a way of elegant partial function application such as in other (functional) languages? A function "doemar" accepts a function with one parameter, Another function g defined within mail accepts two parameters and should be applied parti

Re: Partial function application (Currying)

2024-01-20 Thread atzensepp via Digitalmars-d-learn
On Saturday, 20 January 2024 at 17:45:36 UTC, Christian Köstlin wrote: Would https://dlang.org/library/std/functional/curry.html help you? kind regards, Christian Hello Christian, thank for the link. I looks good however in my installation (gdc 8.4.0) it says: ```d curry.d:1:8: error: modul

Re: Partial function application (Currying)

2024-01-20 Thread atzensepp via Digitalmars-d-learn
On Saturday, 20 January 2024 at 17:47:29 UTC, Richard (Rikki) Andrew Cattermole wrote: Not really any other way to do it, create context (i.e. struct, or stack) and have a delegate point to both it and a patch function. It'll be how partial is implemented. https://dlang.org/phobos/std_functio

Re: Partial function application (Currying)

2024-01-20 Thread atzensepp via Digitalmars-d-learn
On Saturday, 20 January 2024 at 20:58:49 UTC, Richard (Rikki) Andrew Cattermole wrote: On 21/01/2024 9:55 AM, atzensepp wrote: import std.stdio; // Overloads are resolved when the partially applied function is called // with the remaining arguments. struct S { static char fun(int i, string s) {

Re: vector crash

2024-01-21 Thread atzensepp via Digitalmars-d-learn
On Sunday, 21 January 2024 at 03:54:35 UTC, zjh wrote: On Thursday, 18 January 2024 at 03:07:13 UTC, zjh wrote: ```d void toV(string a,ref vector!string b){ auto f=File(a,"r"); while(!f.eof()){ string l=strip(f.readln());push(b,l); } Qwk(b); }// ``` There is an issue wi

Re: Delegates and values captured inside loops

2024-01-21 Thread atzensepp via Digitalmars-d-learn
On Sunday, 21 January 2024 at 20:13:38 UTC, An Pham wrote: On Saturday, 20 January 2024 at 15:59:59 UTC, Anonymouse wrote: I remember reading this was an issue and now I ran into it myself. ```d import std.stdio; void main() { auto names = [ "foo", "bar", "baz" ]; void delegate()[] dg

Re: Delegates and values captured inside loops

2024-01-21 Thread atzensepp via Digitalmars-d-learn
In Ocaml this would look as: ```d let names =["foo";"bar";"baz"] in let funmap = List.map ( fun name -> ( fun () -> print_endline name)) names in List.iter ( fun f -> f () ) funmap ;; ~ ``` I think in the D-example it is basically one function and not 3.

Effective String to Date conversion?

2024-01-22 Thread atzensepp via Digitalmars-d-learn
Dear D-gurus, being new to D I am trying my first steps and the language is quite intuitive and appealing. When reading a file and creating a hash for the reocrds I want to get only the most recent ones. For this I need to convert Date/Time-Strings to comparable DateTime-Objects. The code belo

Function Composition

2024-01-24 Thread atzensepp via Digitalmars-d-learn
How is it possible to compose functions? I came up with following solution that is not satisfactory for two reasons: 1. the compose function should be argument agnostic here it is explicitly coded for (int) -> (int) 2. the composition itself requires additional lambda exp

Re: Function Composition

2024-01-24 Thread atzensepp via Digitalmars-d-learn
Some progress: compose function needs to know type but templates help to create for different types. ```d import std.stdio; import std.container.array; // Function composition: int f(int x) { return x*2;} ; int g(int x) { return x+2;} ; double ff(double x) { return x*x;} ; double gg(double x

Re: Function Composition

2024-01-25 Thread atzensepp via Digitalmars-d-learn
On Wednesday, 24 January 2024 at 21:34:26 UTC, user1234 wrote: On Wednesday, 24 January 2024 at 21:30:23 UTC, user1234 wrote: On Wednesday, 24 January 2024 at 21:12:20 UTC, atzensepp wrote: [...] what a bummer! Have you tried https://dlang.org/phobos/std_functional.html#compose ? Well this

Re: Function Composition

2024-01-25 Thread atzensepp via Digitalmars-d-learn
On Thursday, 25 January 2024 at 12:19:47 UTC, Paul Backus wrote: On Thursday, 25 January 2024 at 08:25:02 UTC, atzensepp wrote: ```d int function(int) t = compose!(f,g,g,f,g,g,f,g,g,f); ``` This leads to: ``` gdc lambda4.d lambda4.d:28:25: error: template compose(E)(E a) has no value int f

Re: Function Composition

2024-01-31 Thread atzensepp via Digitalmars-d-learn
On Monday, 29 January 2024 at 19:24:51 UTC, Inkrementator wrote: On Thursday, 25 January 2024 at 18:44:26 UTC, atzensepp wrote: However this works: ```d int delegate (int) td = (x) => compose!(f,g,g,f,g,g,f,g,g,f)(x); ``` While not a real function pointer, this might already fit your nee