Re: std.datetime & timzone specifier: 2018-11-06T16:52:03+01:00

2020-03-08 Thread tchaloupka via Digitalmars-d-learn

On Sunday, 8 March 2020 at 17:28:33 UTC, Robert M. Münch wrote:

On 2020-03-07 12:10:27 +, Jonathan M Davis said:

DateTime dt = 
DateTime.fromISOExtString(split("2018-11-06T16:52:03+01:00", 
regex("\\+"))[0]);


IMO such a string should be feedable directly to the function.


You just need to use SysTime.fromISO*String functions for that, 
as DateTime does't work with timezones, SysTime do.


Re: std.datetime & timzone specifier: 2018-11-06T16:52:03+01:00

2020-03-08 Thread kdevel via Digitalmars-d-learn

On Sunday, 8 March 2020 at 17:28:33 UTC, Robert M. Münch wrote:
[...]

But I have to do:

DateTime dt = 
DateTime.fromISOExtString(split("2018-11-06T16:52:03+01:00", 
regex("\\+"))[0]);


You don't need a regex. split (..., '+') seems to suffice here.


IMO such a string should be feedable directly to the function.


It took me less than an hour to figure out how to provide a local 
version of std.datetime named local.datetime:


```local/datetime.d
module local.datetime;
public import std.datetime;
struct DateTime {
   std.datetime.DateTime dt;
   alias dt this;
   static DateTime fromISOExtString (string s)
   {
  import std.array;
  auto arr = split (s, '+');
  import std.exception;
  enforce (arr.length > 0);
  return DateTime (dt.fromISOExtString (arr[0]));
   }
}
```

usage: import local.datetime instead of import std.datetime.


Re: Converting Lua source to D

2020-03-08 Thread Jesse Phillips via Digitalmars-d-learn

On Saturday, 7 March 2020 at 01:14:14 UTC, Jesse Phillips wrote:


Now I should look at getting the CI up and Test failure fixed.


Test failures were my local system and related to the stack 
overflow tests.


I have the build pipeline up and running but hit a couple of 
snags.

https://github.com/JesseKPhillips/lua/runs/493866555?check_suite_focus=true

* Couldn't use dpp in the build because I couldn't install 
libclang-dev on the runner (ubuntu repository issue)

* The compiler couldn't locate libphobos

I think I'll be able to make use of dpp locally though.


Re: static foreach / How to construct concatenated string?

2020-03-08 Thread Robert M. Münch via Digitalmars-d-learn

On 2020-03-07 16:41:47 +, MoonlightSentinel said:


You can use an anonymous lambda to build the string in CTFE:

--

struct S {
 int a;
 bool b;
}

import std;

enum string sql = {
 string s = "CREATE TABLE data(";

 static foreach(f; FieldNameTuple!S) {
s ~= f ~ ",";
 }

s ~= ");";
 return s;
} ();

pragma(msg, sql);

--

This prints "CREATE TABLE data(a, b);"


You can get rid of the enum und the static and it will work too.

--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



Re: How to use sets in D?

2020-03-08 Thread Jesse Phillips via Digitalmars-d-learn

On Friday, 7 February 2020 at 19:37:08 UTC, mark wrote:
I am porting code from other languages to D as part of learning 
D, and I find I've used sets quite a lot. AFAIK D doesn't have 
a built-in set type or one in the std. lib.


However, I've been perfectly successfully using int[E] where E 
is my ElementType, and adding with set[element] = 0. I mostly 
only need add, remove, iteration, and in, with uniqueness what 
I care most about.


I know I could use bool[E] and set[element] = false, or I 
suppose container.rbtree. Would either of these--or something 
else built-in or in the std. lib.--be better?


I think I've usually used the associative arrays, but I also 
think I tend to avoid using this approach but couldn't quite 
remember what I do instead.


I believe I have started just using an array.

arr ~= addMyData;
arr.sort.uniq

Then I make use of the algorithms here.

https://dlang.org/phobos/std_algorithm_setops.html


Re: Just a Reminder...

2020-03-08 Thread Brakeran via Digitalmars-d-learn

On Monday, 24 February 2020 at 17:47:37 UTC, Ron Tarrant wrote:
In case you thought the GtkDcoding blog announcements have 
stopped, I just want to let you know that starting with #0101, 
they'll be made in the Announce sub-forum from now on. This is 
where they were originally supposed to be made, but because I 
got the wrong end of the stick last year, I ended up making 
them here in Learn.


[...]


I love your blog posts, thank you again.


Re: Can't compile dlangui

2020-03-08 Thread Brakeran via Digitalmars-d-learn

On Friday, 7 February 2020 at 14:25:05 UTC, Jan Hönig wrote:

On Friday, 7 February 2020 at 12:04:10 UTC, A.Perea wrote:

[...]


I am afraid that dlangui and dlangide is currently not 
maintained, since i can reproduce the error as well.


If you are looking for a good editor for D: I am using 
VisualStudioCode with the code-d plugin.
If you don't like Microsoft's calling home features, you can go 
for Codium: https://vscodium.com/


Thank you for that vscodium tip! Ditched visual studio code just 
now and replaced it with vscodium.


Re: A small D/GtkD example game

2020-03-08 Thread Brakeran via Digitalmars-d-learn

On Monday, 24 February 2020 at 19:18:24 UTC, mark wrote:

I've just completed a small D/GtkD game.

It might be useful for others trying to learn GtkD since it is 
only just over 1000 lines, yet shows how to create a 
dialog-style app with a modal dialog and a modeless dialog, and 
a custom drawn widget, as well as keyboard and mouse handling.


The source (and a 64-bit Windows binary) is here:
https://github.com/mark-summerfield/gravitate-d

I'm posting to the Learn forum because I'm just a D beginner, 
so felt that using the announce list would be a bit 
presumptuous.


Thanks!
Much appreciated, perfect for a beginner as me.



Re: std.datetime & timzone specifier: 2018-11-06T16:52:03+01:00

2020-03-08 Thread Robert M. Münch via Digitalmars-d-learn

On 2020-03-07 12:10:27 +, Jonathan M Davis said:


I take it that you're asking why you don't get the time zone as part of the
string when you call one of the to*String functions?


The problem is, the from* functions give an error, that this is not an 
ISO date.


I get this in an XML response and extract the datetime:




But I have to do:

DateTime dt = 
DateTime.fromISOExtString(split("2018-11-06T16:52:03+01:00", 
regex("\\+"))[0]);


IMO such a string should be feedable directly to the function.

--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



Re: static foreach / How to construct concatenated string?

2020-03-08 Thread Robert M. Münch via Digitalmars-d-learn

On 2020-03-07 16:41:47 +, MoonlightSentinel said:


On Saturday, 7 March 2020 at 16:30:59 UTC, Robert M. Münch wrote:

Is this possible at all?


You can use an anonymous lambda to build the string in CTFE:

--

struct S {
 int a;
 bool b;
}

import std;

enum string sql = {
 string s = "CREATE TABLE data(";

 static foreach(f; FieldNameTuple!S) {
s ~= f ~ ",";
 }

s ~= ");";
 return s;
} ();

pragma(msg, sql);

--

This prints "CREATE TABLE data(a, b);"


Nice... is the enum a so called "manifest constant" for which the 
initializer is evaluated at compile time?


OT: The pragma seems to print the string twice... at least here on my side.

OT2: Looks like I have to read through the language spec again... and 
most likely over and over again, to have all these tricks at my 
finger-tips.


--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



Re: How to use sets in D?

2020-03-08 Thread Johan via Digitalmars-d-learn

On Sunday, 8 March 2020 at 08:43:10 UTC, mark wrote:


Here are some timings ...

[...]

#!/usr/bin/env rdmd


Please remember that performance testing is not trivial.
At the very least, you should be testing optimized code (-O) and 
preferably with LDC or GDC because they have a much stronger 
optimizer than DMD.
Also, `assert` is not a good way to check something in 
performance testing code. See 
https://forum.dlang.org/thread/bpbyhmrsfzirfqggn...@forum.dlang.org


-Johan



Re: How to use sets in D?

2020-03-08 Thread mark via Digitalmars-d-learn
I use sets a lot and am now working on a program that will need 
to hold sets of 65,000+ items, so I thought I do some timings for 
the different approaches.


Here are some timings (uset uses the AA Unit approach, tset uses 
an rbtree, and aset uses an AA with bool values):


$ ./sets.d
size 50,000
uset 1 ms, 340 μs, and 8 hnsecs
tset 4 ms, 637 μs, and 1 hnsec
aset 1 ms, 402 μs, and 6 hnsecs
$ ./sets.d
size 100,000
uset 2 ms, 338 μs, and 4 hnsecs
tset 12 ms, 262 μs, and 6 hnsecs
aset 2 ms and 991 μs
$ ./sets.d
size 200,000
uset 5 ms, 971 μs, and 5 hnsecs
tset 30 ms, 675 μs, and 5 hnsecs
aset 6 ms, 74 μs, and 6 hnsecs
$ ./sets.d
size 400,000
uset 11 ms, 823 μs, and 4 hnsecs
tset 74 ms, 146 μs, and 2 hnsecs
aset 12 ms, 560 μs, and 5 hnsecs

What seems pretty clear is that for my purposes (checking 
presence or absence of membership in a set), AAs are much faster 
than rbtrees. (This is to be expected since if an AA uses a hash 
is should be O(1) vs O(lg n) for an rbtree).


Here is the test code I used:

#!/usr/bin/env rdmd

enum SIZE = 400_000;
enum SUB_SIZE = SIZE / 10;
enum MIN_LEN = 10;
enum MAX_LEN = 50;
enum AZ = "abcdefghijklmnopqrstuvwxyz";

void main() {
import std.stdio: writefln;

auto data = Data.populate();
auto sets = Sets(data.words);

writefln("size %,d", SIZE);
check(sets.uset, data.present, data.absent, "uset");
check(sets.tset, data.present, data.absent, "tset");
check(sets.aset, data.present, data.absent, "aset");
}

struct Sets {
import std.container.rbtree: RedBlackTree;

alias Unit = void[0];
enum unit = Unit.init;

Unit[string] uset;
RedBlackTree!string tset;
bool[string] aset;

this(string[] words) {
tset = new RedBlackTree!string;
foreach (word; words) {
uset[word] = unit;
tset.insert(word);
aset[word] = false;
}
}
}

struct Data {
string[] words;
string[] present;
string[] absent;

static Data populate() {
Data data;
for (int i = 0; i < SIZE; i++) {
auto word = makeWord;
data.words ~= word;
if (data.present.length < SUB_SIZE)
data.present ~= word;
if (data.absent.length < SUB_SIZE)
data.absent ~= word ~ "9";
}
return data;
}
}

string makeWord() {
import std.random: randomCover, uniform;
import std.range: take;
import std.string: join, split;

enum AZS = (AZ ~ AZ ~ AZ ~ AZ ~ AZ ~ AZ).split("");
return randomCover(AZS).take(uniform(MIN_LEN, 
MAX_LEN)).join("");

}

void check(T)(T set, string[] present, string[] absent, string 
name) {

import std.datetime.stopwatch: AutoStart, StopWatch;
import std.stdio: writeln;

auto timer = StopWatch(AutoStart.yes);
foreach (string p; present)
assert(p in set);
foreach (string a; absent)
assert(a !in set);
writeln(name, " ", timer.peek);
}