splitter and matcher combined regex

2019-06-16 Thread Amex via Digitalmars-d-learn
Having to split and match seems slow(50%). Surely the regex 
splitter and matcher can be combined? Sometimes we just need to 
extract out and remove information simultaneously.


I propose a new function called extractor that returns the 
matchAll and splitter's results but is optimized.


CT/RT annoyance

2019-06-16 Thread Bart via Digitalmars-d-learn

Consider

void foo(string A = "")(string B = "")
{
static if (A != "")
   do(A);
else
   do(B);
}

foo!("x");

foo("x");


This is necessary because D's templating and meta programming 
system is frail.


While CTFE should take care of such things, it does not, consider 
import(file) vs readText(file).


If do loads such a file then one must use tricks as above:


auto load(string n = "")(string n2 = "")
{
static if (n != "")
enum x = import(n);
else
auto x = readText(n2);
}

auto load2(string n)
{
   enum x = import(n);
   string y = import(n);
}


load2 fails to compile SIMPLY because n is a RT parameter.

Even if ctfe were to kick in, such as 
load2("LiteralFileName.txt"), it is irrelevant because we can't 
compile the code.


So tricks as in load are used, but it is very hacky and verbose 
to do something very simple and natural.


This is precisely because readText cannot be used at compile time 
since it dispatches to the OS's reading routines rather than 
using import. This is not necessarily bad since we generally do 
not want to import files in to the application at compile time, 
but when we do it creates somewhat of a mess to unify code under 
ctfe.


For example, if I create a function that loads a file and 
processes it at RT then that code will not compile when used in 
CTFE. If I modify it to import the text at CT then it does not 
work at RT... even though the single point of breakage is the 
import/readText line.



auto importOrReadText(string n)
{
if (__ctfe)
{
auto x = import(n);
} else
{
auto x = readText(n);
}
}

Fails for the same reasons above.

There is no way to get out of this conundrum(and therefor it is a 
contradiction). It thin requires kicking the can down the street 
and not read files at all but take text.


My suggestion is for someone to fix this. I'm not sure of the 
solution.



maybe something like

auto importOrReadText(string n)
{
if (__ctfe)
{
auto x = import(ctfeFix(n));
} else
{
auto x = readText(n);
}
}

ctfeFix would be a special compiler semantic that takes a RT-like 
variable and converts it to CT. It does this because it actually 
can. It knows that n is ctfe'able and therefore n is actually a 
string literal and will do some "magic" trick the compiler in to 
seeing n for what it really is.


importOrReadText(rtFilename); // uses readText
importOrReadText("x.txt"); // uses import


Unfortunately __ctfe doesn't seem to actually work well... so I 
suggest further


auto importOrReadText(string n)
{
static if (isKACT(n))
{
auto x = import(n);
// or auto x = import(ctfeFix(n));
} else
{
auto x = readText(n);
}
}

isKACT determines if n is actually known at compile time and 
marks it n as essentially a CT constant as if it were passed as a 
template parameter(internally it would probably just convert it 
to a template parameter as if it were passed as such).



There may be other solutions.







Re: Range violation error when reading from a file

2019-06-16 Thread Norm via Digitalmars-d-learn

On Monday, 17 June 2019 at 00:22:23 UTC, Samir wrote:

On Sunday, 16 June 2019 at 23:55:41 UTC, lithium iodate wrote:
There is *very* likely to be a terminating new-line at the end 
of the file (many editors add one without asking!). If that 
the case, the last line seen by the loop will be empty and you 
must not attempt to access any elements.


On Monday, 17 June 2019 at 00:02:37 UTC, aliak wrote:
The fail bit is only set after reading fails. So after you 
read the last line, your eof will still return true, and hence 
your range violation.


H...maybe you and lithium iodate were onto something.

Here is what the file looks like in vim:
> line 1
line 2
line 3
> line 4
line 5
~
~
~

The "5" in the last line is the last character I can put my 
cursor on.


Also, if I run the program below with the same file, I don't 
get any range violation errors:


import std.stdio;
import std.string;

void main() {
File file = File("test.txt");
string line;

while (!file.eof()) {
line = file.readln().strip;
//if (line[0] == '>') { // line 10
//writeln(line[1..$]);
//}
//else {
writeln(line);
//}
}
}

HOWEVER, the output is interesting.  There IS a blank line 
between the last line and the prompt:


$ dmd -run readfile.d
> line 1
line 2
line 3
> line 4
line 5

$

Any suggestions on how to rectify?


You could change the IF to

`if(line.length > 0 && line[0] == '>')`

or use strip itself;

`File("test.txt", "r").byLine.map!(line => 
line.strip(">")).writeln;`


For "> line 1" your code and this above will generate " line 1" 
(note the leading space). To remove that change the string passed 
to `strip` to include a space, e.g.;


`.strip("> ")).writeln;`

bye,
Norm



Re: Range violation error when reading from a file

2019-06-16 Thread Samir via Digitalmars-d-learn

On Sunday, 16 June 2019 at 23:55:41 UTC, lithium iodate wrote:
There is *very* likely to be a terminating new-line at the end 
of the file (many editors add one without asking!). If that the 
case, the last line seen by the loop will be empty and you must 
not attempt to access any elements.


On Monday, 17 June 2019 at 00:02:37 UTC, aliak wrote:
The fail bit is only set after reading fails. So after you read 
the last line, your eof will still return true, and hence your 
range violation.


H...maybe you and lithium iodate were onto something.

Here is what the file looks like in vim:
> line 1
line 2
line 3
> line 4
line 5
~
~
~

The "5" in the last line is the last character I can put my 
cursor on.


Also, if I run the program below with the same file, I don't get 
any range violation errors:


import std.stdio;
import std.string;

void main() {
File file = File("test.txt");
string line;

while (!file.eof()) {
line = file.readln().strip;
//if (line[0] == '>') { // line 10
//writeln(line[1..$]);
//}
//else {
writeln(line);
//}
}
}

HOWEVER, the output is interesting.  There IS a blank line 
between the last line and the prompt:


$ dmd -run readfile.d
> line 1
line 2
line 3
> line 4
line 5

$

Any suggestions on how to rectify?



Re: Range violation error when reading from a file

2019-06-16 Thread aliak via Digitalmars-d-learn

On Sunday, 16 June 2019 at 23:44:49 UTC, Samir wrote:

On Sunday, 16 June 2019 at 23:03:04 UTC, aliak wrote:

stripping the last line could result in an empty line if it 
just has strippable characters?


The last line of the file is just text but without a newline 
(\n) character or any other whitespace character at the end.  I 
get the same error when I remove the strip function from the 
readln line.


http://www.cplusplus.com/reference/ios/ios/eof/

The fail bit is only set after reading fails. So after you read 
the last line, your eof will still return true, and hence your 
range violation.


Re: Range violation error when reading from a file

2019-06-16 Thread lithium iodate via Digitalmars-d-learn

On Sunday, 16 June 2019 at 23:44:49 UTC, Samir wrote:

On Sunday, 16 June 2019 at 23:03:04 UTC, aliak wrote:

stripping the last line could result in an empty line if it 
just has strippable characters?


The last line of the file is just text but without a newline 
(\n) character or any other whitespace character at the end.  I 
get the same error when I remove the strip function from the 
readln line.


There is *very* likely to be a terminating new-line at the end of 
the file (many editors add one without asking!). If that the 
case, the last line seen by the loop will be empty and you must 
not attempt to access any elements.


Re: Range violation error when reading from a file

2019-06-16 Thread Samir via Digitalmars-d-learn

On Sunday, 16 June 2019 at 23:03:04 UTC, aliak wrote:

stripping the last line could result in an empty line if it 
just has strippable characters?


The last line of the file is just text but without a newline (\n) 
character or any other whitespace character at the end.  I get 
the same error when I remove the strip function from the readln 
line.


Re: Range violation error when reading from a file

2019-06-16 Thread Samir via Digitalmars-d-learn

On Sunday, 16 June 2019 at 23:03:04 UTC, aliak wrote:
stripping the last line could result in an empty line if it 
just has strippable characters?


The last line is just the text of the last line.  There is no 
newline character at the end.  I also get the same error if I 
remove the strip function from the readln line.


Re: Range violation error when reading from a file

2019-06-16 Thread aliak via Digitalmars-d-learn

On Sunday, 16 June 2019 at 22:47:14 UTC, Samir wrote:

I am trying to read from a text file using the following code:

import std.stdio;
import std.string;

void main() {
File file = File("test.txt");
string line;

while (!file.eof()) {
line = strip(file.readln());
if (line[0] == '>') { // line 10
writeln(line[1..$]);
}
else {
writeln(line);
}
}
}

and I get the following error AFTER the last line is processed:

core.exception.RangeError@readfile.d(10): Range violation

??:? _d_arrayboundsp [0x448efa]
??:? _Dmain [0x4459f7]

Any idea what I am doing wrong?  When processing the if 
statement or writing the slice, am I inadvertently trying to 
read a non-existent line in the file?


Thanks in advance
Samir


stripping the last line could result in an empty line if it just 
has strippable characters?


Range violation error when reading from a file

2019-06-16 Thread Samir via Digitalmars-d-learn

I am trying to read from a text file using the following code:

import std.stdio;
import std.string;

void main() {
File file = File("test.txt");
string line;

while (!file.eof()) {
line = strip(file.readln());
if (line[0] == '>') { // line 10
writeln(line[1..$]);
}
else {
writeln(line);
}
}
}

and I get the following error AFTER the last line is processed:

core.exception.RangeError@readfile.d(10): Range violation

??:? _d_arrayboundsp [0x448efa]
??:? _Dmain [0x4459f7]

Any idea what I am doing wrong?  When processing the if statement 
or writing the slice, am I inadvertently trying to read a 
non-existent line in the file?


Thanks in advance
Samir


Re: Suggest aesthetic way to Naming a module or a package with illegal lexical D lang keywords

2019-06-16 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, June 16, 2019 5:53:30 AM MDT BoQsc via Digitalmars-d-learn wrote:
> On Sunday, 16 June 2019 at 11:38:27 UTC, rikki cattermole wrote:
> > The style guide has an opinion about this (you don't have to
> > follow it).
> >
> > https://dlang.org/dstyle.html#naming_keywords
>
> So if I follow dstyle guidelines on keywords, this would be a
>
> correct non-conflicting result:
> >module _auto._alias._abstract;
> >
> > void main(){
> >
> >}
>
> The underscore way, I kind of liking it, thanks for sourcing the
> idea.

Well, technically, per the style guide, the underscores would go at the end
of the names, not the front, but that's only if you're trying to follow the
style guide rather than be inspired by it.

- Jonathan M Davis





How does this template work?

2019-06-16 Thread Robert M. Münch via Digitalmars-d-learn
How does the observerObject Template and function work? I'm struggling 
because both use the same name and how is the template parameter R 
deduced/where is it coming from? Looks like it's somehow implicitly 
deduced.



class ObserverObject(R, E...){...}

template observerObject(E)
{
   ObserverObject!(R, E) observerObject(R)(R range)
   {
   return new ObserverObject!(R, E)(range);
   }
}

struct TestObserver {...}

auto observer = observerObject!int(TestObserver());


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



Re: How does this template work?

2019-06-16 Thread rikki cattermole via Digitalmars-d-learn

On 17/06/2019 3:11 AM, Robert M. Münch wrote:
How does the observerObject Template and function work? I'm struggling 
because both use the same name and how is the template parameter R 
deduced/where is it coming from? Looks like it's somehow implicitly 
deduced.



class ObserverObject(R, E...){...}

template observerObject(E)
{
    ObserverObject!(R, E) observerObject(R)(R range)
    {
    return new ObserverObject!(R, E)(range);
    }
}

struct TestObserver {...}

auto observer = observerObject!int(TestObserver());


observerObject is an eponymous template.

What this means (in essence) is the symbol inside the template block == 
template block.


Yes R is being inferred by the argument.


Re: Strange closure behaviour

2019-06-16 Thread Rémy Mouëza via Digitalmars-d-learn

On Sunday, 16 June 2019 at 01:36:38 UTC, Timon Gehr wrote:
It's a bug. It's memory corruption. Different objects with 
overlapping

 lifetimes use the same memory location.

Okay. Seen that way, it is clear to me why it's a bug.


...
No, it's not the same. Python has no sensible notion of 
variable scope.


>>> for i in range(3): pass
...
>>> print(i)
2

Yuck.


I got confused by this Python behavior:

ls = []
for i in range(0, 5):
   ls.append(lambda x: x + i)
for fun in ls:
   print(fun(0))

This prints:
4
4
4
4
4


Re: Suggest aesthetic way to Naming a module or a package with illegal lexical D lang keywords

2019-06-16 Thread BoQsc via Digitalmars-d-learn

On Sunday, 16 June 2019 at 11:38:27 UTC, rikki cattermole wrote:
The style guide has an opinion about this (you don't have to 
follow it).


https://dlang.org/dstyle.html#naming_keywords


So if I follow dstyle guidelines on keywords, this would be a 
correct non-conflicting result:



module _auto._alias._abstract;
void main(){


}


The underscore way, I kind of liking it, thanks for sourcing the 
idea.


Re: Suggest aesthetic way to Naming a module or a package with illegal lexical D lang keywords

2019-06-16 Thread rikki cattermole via Digitalmars-d-learn

The style guide has an opinion about this (you don't have to follow it).

https://dlang.org/dstyle.html#naming_keywords


Suggest aesthetic way to Naming a module or a package with illegal lexical D lang keywords

2019-06-16 Thread BoQsc via Digitalmars-d-learn
Do not ask why I want to do that, you can however suggest 
alternative variations.


As you all might know,

2. The Identifiers preceding the rightmost are the Packages 
that the module is in. The packages correspond to directory 
names in the source file path. Package and module names cannot 
be Keywords.

https://dlang.org/spec/module.html#module_declaration

The D Spec literally says that we cannot use these words in the 
Module Names:

https://dlang.org/spec/lex.html#Keyword

Example, we can't Name Modules or Packages like that:


module auto.alias.abstract;
void main(){


}





I would like to know if there is any way to make it possible to 
name Modules/Packages via preoccupied lexical words, or at least 
receive some suggestions on some aesthetic naming for a 
module/package that includes these lexical d words.



Examples:


module dauto.dalias.dabstract;
void main(){


}



module CustomAuto.CustomAlias.CustomAbstract;
void main(){


}