Re: A look inside "filter" function defintion

2022-08-01 Thread frame via Digitalmars-d-learn
On Monday, 1 August 2022 at 23:35:13 UTC, pascal111 wrote: This is the definition of "filter" function, and I think it called itself within its definition. I'm guessing how it works? It's a template that defines the function called "Eponymous Templates": https://dlang.org/spec/template.html#i

Re: How to check if a class has a specific overload for member x?

2022-08-01 Thread pascal111 via Digitalmars-d-learn
On Monday, 1 August 2022 at 00:23:21 UTC, Hipreme wrote: On Sunday, 31 July 2022 at 19:25:51 UTC, Hipreme wrote: [...] Seems that is how it should be checked: ```d enum hasOverload(T, string member, FuncType)() { bool ret; static foreach(overload; __traits(getVirt

A look inside "filter" function defintion

2022-08-01 Thread pascal111 via Digitalmars-d-learn
This is the definition of "filter" function, and I think it called itself within its definition. I'm guessing how it works? '''D template filter(alias predicate) if (is(typeof(unaryFun!predicate))) { /** Params: range = An $(REF_ALTTEXT input range, isInputRange, std,range,primi

Re: Converting JSONValue to AssociativeArray.

2022-08-01 Thread Ali Çehreli via Digitalmars-d-learn
On 8/1/22 15:47, Steven Schveighoffer wrote: You beat me to it. I used .object and .str: import std; void main() { JSONValue data = parseJSON(`{ "name": "Hype Editor", "hobby": "Programming" }`); writefln("%s", data); // This already sees the data as an AA but the type is JSONVa

Re: Converting JSONValue to AssociativeArray.

2022-08-01 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/1/22 2:00 PM, hype_editor wrote: I need to convert variable of type `JSONValue` to variable of type `string[string]` (AssociativeArray). ```d import std.json : JSONValue; import std.stdio : writefln; void main() {     JSONValue data = parseJSON(`{ "name": "Hype Editor", "hobby": "Progra

Re: Combining JSON arrays into a single JSON array -- better way than this?

2022-08-01 Thread ikelaiah via Digitalmars-d-learn
On Monday, 1 August 2022 at 17:02:10 UTC, Salih Dincer wrote: When printing the steps `import std.stdio : writeln;` use it. Thus, it does not conflict with `std.file`. SDB@79 Will do and thank you for this.

Re: Combining JSON arrays into a single JSON array -- better way than this?

2022-08-01 Thread ikelaiah via Digitalmars-d-learn
On Monday, 1 August 2022 at 16:11:52 UTC, frame wrote: ... because loading all in memory while not necessary is wasted memory if you have large files to process. Noted with thanks. Actually, some JSON input files are pretty large (>1Mb). I would just process each file and write it to the fi

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread pascal111 via Digitalmars-d-learn
On Monday, 1 August 2022 at 21:35:19 UTC, Ivan Kazmenko wrote: On Monday, 1 August 2022 at 20:36:12 UTC, pascal111 wrote: My complaint is about that a function is not a same as an expression that functions return values, but expressions being evaluated to provide values. An analogy. With a t

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread Ivan Kazmenko via Digitalmars-d-learn
On Monday, 1 August 2022 at 20:36:12 UTC, pascal111 wrote: My complaint is about that a function is not a same as an expression that functions return values, but expressions being evaluated to provide values. An analogy. With a ternary expression, we write: `x = (cond ? a : b);` The tradition

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread pascal111 via Digitalmars-d-learn
On Monday, 1 August 2022 at 19:32:41 UTC, Paul Backus wrote: On Monday, 1 August 2022 at 14:52:03 UTC, pascal111 wrote: [...] From [the relevant section of the language spec:][1] [...] In other words, a function literal is an expression that evaluates to either a delegate or a function po

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread Paul Backus via Digitalmars-d-learn
On Monday, 1 August 2022 at 14:52:03 UTC, pascal111 wrote: If `foo => bar` == `(foo) { return bar; }`, then `foo => bar` is a function. "=>" is not an operator, it's a special symbol for lambda "function". If A == B, so A's types is the same of B's type. How can it be withstanding `foo => ba

Re: Combining JSON arrays into a single JSON array -- better way than this?

2022-08-01 Thread kdevel via Digitalmars-d-learn
On Monday, 1 August 2022 at 07:35:34 UTC, Christian Köstlin wrote: [...] ``` An arguably shorter solution (that drops some of your logging) could be: ```d import std; void main() { dirEntries(".", "*.json", SpanMode.shallow) .filter!(f => !f.name.canFind("output")) .map!(r

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread pascal111 via Digitalmars-d-learn
On Monday, 1 August 2022 at 17:01:33 UTC, frame wrote: On Monday, 1 August 2022 at 14:15:31 UTC, pascal111 wrote: [...] In C ";" is a termination character, in D is more like to separate statements. The lexer wouldn't need ";" for most cases like JavaScript and the expression syntax withou

Converting JSONValue to AssociativeArray.

2022-08-01 Thread hype_editor via Digitalmars-d-learn
I need to convert variable of type `JSONValue` to variable of type `string[string]` (AssociativeArray). ```d import std.json : JSONValue; import std.stdio : writefln; void main() { JSONValue data = parseJSON(`{ "name": "Hype Editor", "hobby": "Programming" }`); writefln("%s", data);

Re: never seed this kind problem with localtime_r

2022-08-01 Thread frame via Digitalmars-d-learn
On Monday, 1 August 2022 at 13:31:15 UTC, test123 wrote: please help me give any suggestion how to handle this problem. errno = ?

Re: toString doesn't compile with -dip1000 switch

2022-08-01 Thread wjoe via Digitalmars-d-learn
On Monday, 1 August 2022 at 17:07:43 UTC, wjoe wrote: On Monday, 1 August 2022 at 13:09:01 UTC, Kagamin wrote: Bar.toString is typed `@system`. Even if I'd declare everything @safe: at module scope? I wrote that on my phone and it got a bit messy... ``` D module x; @safe: struct Foo() {

Re: toString doesn't compile with -dip1000 switch

2022-08-01 Thread wjoe via Digitalmars-d-learn
On Monday, 1 August 2022 at 13:09:01 UTC, Kagamin wrote: Bar.toString is typed `@system`. Even if I'd declare everything @safe: at module scope?

Re: Combining JSON arrays into a single JSON array -- better way than this?

2022-08-01 Thread Salih Dincer via Digitalmars-d-learn
On Monday, 1 August 2022 at 08:48:28 UTC, ikelaiah wrote: On Monday, 1 August 2022 at 07:35:34 UTC, Christian Köstlin wrote: An arguably shorter solution (that drops some of your logging) could be: ```d import std; void main() { dirEntries(".", "*.json", SpanMode.shallow) .filte

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread frame via Digitalmars-d-learn
On Monday, 1 August 2022 at 14:15:31 UTC, pascal111 wrote: We all know the strange syntax of lambda function within filter algorithm like "auto r = chain(a, b).filter!(a => a > 0);". My note is, don't we break D rules by leaving ";" after lambda function syntax?! Many of D rules are taken fro

Re: Combining JSON arrays into a single JSON array -- better way than this?

2022-08-01 Thread frame via Digitalmars-d-learn
On Monday, 1 August 2022 at 09:01:35 UTC, ikelaiah wrote: Based in your suggestion, the snippet is now more brief. While your string attempt wasn't that bad, because loading all in memory while not necessary is wasted memory if you have large files to process. I would just process each file

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread pascal111 via Digitalmars-d-learn
On Monday, 1 August 2022 at 16:00:50 UTC, ag0aep6g wrote: On Monday, 1 August 2022 at 15:52:34 UTC, pascal111 wrote: But how can we accept that both are functions and expressions at the same time and we know that expressions can be used to build functions themselves?!! I think they are not the

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 1 August 2022 at 15:52:34 UTC, pascal111 wrote: But how can we accept that both are functions and expressions at the same time and we know that expressions can be used to build functions themselves?!! I think they are not the same. This is getting ridiculous. I'm out.

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread pascal111 via Digitalmars-d-learn
On Monday, 1 August 2022 at 15:39:06 UTC, ag0aep6g wrote: On Monday, 1 August 2022 at 15:31:51 UTC, pascal111 wrote: Surely, because it seems that you are real man, your word must be taken. Isn't `(foo) { return bar; }` an anonymous function or am I a wrong?!! It IS a function, not an expressio

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 1 August 2022 at 15:31:51 UTC, pascal111 wrote: Surely, because it seems that you are real man, your word must be taken. Isn't `(foo) { return bar; }` an anonymous function or am I a wrong?!! It IS a function, not an expression. It's both an anonymous function and an expression. I d

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread pascal111 via Digitalmars-d-learn
On Monday, 1 August 2022 at 15:08:04 UTC, ag0aep6g wrote: On Monday, 1 August 2022 at 14:52:03 UTC, pascal111 wrote: If `foo => bar` == `(foo) { return bar; }`, then `foo => bar` is a function. "=>" is not an operator, it's a special symbol for lambda "function". If A == B, so A's types is t

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 1 August 2022 at 14:52:03 UTC, pascal111 wrote: If `foo => bar` == `(foo) { return bar; }`, then `foo => bar` is a function. "=>" is not an operator, it's a special symbol for lambda "function". If A == B, so A's types is the same of B's type. How can it be withstanding `foo => ba

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread pascal111 via Digitalmars-d-learn
On Monday, 1 August 2022 at 14:46:33 UTC, ag0aep6g wrote: On Monday, 1 August 2022 at 14:39:17 UTC, pascal111 wrote: On Monday, 1 August 2022 at 14:34:45 UTC, ag0aep6g wrote: [...] `a => a > 0` is not a statement. It's an expression. But it is still a "function", and functions make statement

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 1 August 2022 at 14:39:17 UTC, pascal111 wrote: On Monday, 1 August 2022 at 14:34:45 UTC, ag0aep6g wrote: [...] `a => a > 0` is not a statement. It's an expression. But it is still a "function", and functions make statements!! It's not a normal expression. It's a normal expressi

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread pascal111 via Digitalmars-d-learn
On Monday, 1 August 2022 at 14:34:45 UTC, ag0aep6g wrote: On Monday, 1 August 2022 at 14:15:31 UTC, pascal111 wrote: Many of D rules are taken from C, we know that, so a general basic rule is to put ";" after each statement, so the previous statement of filter should be "auto r = chain(a, b).fi

Re: Breaking ";" rule with lambda functions

2022-08-01 Thread ag0aep6g via Digitalmars-d-learn
On Monday, 1 August 2022 at 14:15:31 UTC, pascal111 wrote: Many of D rules are taken from C, we know that, so a general basic rule is to put ";" after each statement, so the previous statement of filter should be "auto r = chain(a, b).filter!(a => a > 0;);"? Why D leaves ";" in this case? `a

Breaking ";" rule with lambda functions

2022-08-01 Thread pascal111 via Digitalmars-d-learn
We all know the strange syntax of lambda function within filter algorithm like "auto r = chain(a, b).filter!(a => a > 0);". My note is, don't we break D rules by leaving ";" after lambda function syntax?! Many of D rules are taken from C, we know that, so a general basic rule is to put ";" af

never seed this kind problem with localtime_r

2022-08-01 Thread test123 via Digitalmars-d-learn
I have an app, when it start localtime_r return correct value. after some time, localtime_r return Thu Jan 1 0:00:00 1970 for same input value. please help me give any suggestion how to handle this problem.

Re: Exclamation symbol "!" within functions syntax

2022-08-01 Thread pascal111 via Digitalmars-d-learn
On Monday, 1 August 2022 at 12:58:27 UTC, Paul Backus wrote: On Monday, 1 August 2022 at 11:35:25 UTC, pascal111 wrote: I noticed that filter is using the concept of templates but this time it's with a lambda function, not with a data type, how can we explain this? isn't supposed to use a data

Re: toString doesn't compile with -dip1000 switch

2022-08-01 Thread Kagamin via Digitalmars-d-learn
Bar.toString is typed `@system`.

Re: Exclamation symbol "!" within functions syntax

2022-08-01 Thread Paul Backus via Digitalmars-d-learn
On Monday, 1 August 2022 at 11:35:25 UTC, pascal111 wrote: I noticed that filter is using the concept of templates but this time it's with a lambda function, not with a data type, how can we explain this? isn't supposed to use a data type after the exclamation mark: "auto r = chain(a, b).filter

Re: Exclamation symbol "!" within functions syntax

2022-08-01 Thread pascal111 via Digitalmars-d-learn
On Wednesday, 27 July 2022 at 11:09:19 UTC, Ali Çehreli wrote: On 7/27/22 04:00, pascal111 wrote: I noticed more than once that the exclamation "!" is used within functions typing, and it seems like an operator with new use, for example "to!int()", ".tee!(l => sum += l.length)", "enforce!Missi

toString doesn't compile with -dip1000 switch

2022-08-01 Thread wjoe via Digitalmars-d-learn
struct Foo() { import std.format: FormatSpec; const void toString( scope void delegate(const(char)[]) @safe sink, FormatSpec!char fmt) {} } struct Bar { import std.format: FormatSpec; const void toString( scope void delegate(const(char)[]) @safe sink, FormatSpec!char fmt

Re: Combining JSON arrays into a single JSON array -- better way than this?

2022-08-01 Thread ikelaiah via Digitalmars-d-learn
On Monday, 1 August 2022 at 05:52:36 UTC, frame wrote: If the JSON files are already parsed, why you stringify and reparse it? Because of ... 1. mental block and 2. I didn't know `jsonResult.array = [];` Many thanks for pointing this out. I tried the following, and didn't work, and henc

Re: Combining JSON arrays into a single JSON array -- better way than this?

2022-08-01 Thread ikelaiah via Digitalmars-d-learn
On Monday, 1 August 2022 at 07:35:34 UTC, Christian Köstlin wrote: An arguably shorter solution (that drops some of your logging) could be: ```d import std; void main() { dirEntries(".", "*.json", SpanMode.shallow) .filter!(f => !f.name.canFind("output")) .map!(readText)

Re: Combining JSON arrays into a single JSON array -- better way than this?

2022-08-01 Thread Christian Köstlin via Digitalmars-d-learn
On 2022-08-01 06:24, ikelaiah wrote: Hi, I've written a cli tool to merge JSON files (containing JSON array) in the current folder as a single JSON file. My algorithm: 1. Create a string to store the output JSON array as a string, 2. read each file 3. read each object in JSON array from inpu