Re: phobos: What type to use instead of File when doing I/O on streams?

2015-11-09 Thread Ali Çehreli via Digitalmars-d-learn

On 11/09/2015 01:48 AM, J.Frank wrote:

My question is now:
How can I make this work without using deprecated stuff?


import std.cstream;

void foo(InputStream in_stream, OutputStream out_stream)
{
//in_stream.seek(3); // compile error - good :)

 char[] line;
 while ((line = in_stream.readLine()) !is null)
 out_stream.writeLine(line);
}

void main(string[] args)
{
 foo(din, dout);
}



You don't need the template constraints but it is good practice:

import std.stdio;
import std.range;

void foo(I, O)(I in_stream, O out_stream)
if (isInputRange!I &&
isOutputRange!(O, ElementType!I)) {

// in_stream.seek(3); // compile error - good :)

foreach (element; in_stream) {
out_stream.put(element);
}
}

void main(string[] args)
{
// Also consider .byLine, which is faster and risky
foo(stdin.byLineCopy,
stdout.lockingTextWriter);
}

Ali



Providing custom formatting without importing half of phobos.

2015-11-09 Thread rumbu via Digitalmars-d-learn
Let's say that I'm a library provider and I intend to offer a 
completely new data type suitable for printing with format() or 
writef().


According to this tutorial 
(http://wiki.dlang.org/Defining_custom_print_format_specifiers), 
to achieve this, one must import at least std.format : FormatSpec.


The problem is that just importing FormatSpec will create exactly 
88 phobos dependencies.


Is there any way to provide formatting capabilities for a custom 
data type without importing the FormatSpec thing?


Split range that has no length or slice?

2015-11-09 Thread Spacen Jasset via Digitalmars-d-learn
I can't seem to find a way of splitting a range of characters 
into lines unless the range has a length or can be sliced? This 
presumably is because i is a range of chunks, which cannot have a 
length or a slice. I do not see how to get anything else from the 
File object at the moment.


import std.stdio;
import std.string;

void printFile(Range)(Range i)
{
foreach (l; lineSplitter(i)) {
writefln("%d\n", l);
}
}

auto range(File f)
{
const size_t someArbitrary = 4096;
return f.byChunk(someArbitrary);
}

void main()
{
File f = File("/etc/passwd");

printFile(f.range());
// std.string.lineSplitter(Flag keepTerm = KeepTerminator.no, 
Range)(Range r) if (hasSlicing!Range && hasLength!Range || 
isSomeString!Range)
// test.d(21): Error: template instance 
test.printFile!(ByChunk) error instantiating

}



Re: Split range that has no length or slice?

2015-11-09 Thread Adam D. Ruppe via Digitalmars-d-learn
File has a .byLine and .byLineCopy method you could use to just 
get lines from it.


std.experimental.allocator optlink error

2015-11-09 Thread ref2401 via Digitalmars-d-learn

Hello

I wrote a small hello world app and imported the 
`std.experimental.allocator` module.

I'm getting the following optlink error:
---
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
console-app.obj(console-app)
Error 42: Symbol Undefined 
_D3std12experimental9allocator12__ModuleInfoZ

--- errorlevel 1
---
os: Windows 8.1 Enterprise
dmd:2.069.0
build sript:dmd main.d -ofconsole-app.exe -debug -unittest -wi

Thank you.


Re: Associative arrays

2015-11-09 Thread TheFlyingFiddle via Digitalmars-d-learn

On Monday, 9 November 2015 at 04:52:37 UTC, rsw0x wrote:
On Monday, 9 November 2015 at 04:29:30 UTC, Rikki Cattermole 
wrote:
Fwiw, EMSI provides high quality containers backed by 
std.experimental.allocator.

https://github.com/economicmodeling/containers


I have a question regarding the implementation of the 
economicmodeling hashmap. Why must buckets be a power of two? Is 
it to be able to use the: hash & (buckets.length - 1) for index 
calculations or is there some other reason?


@property

2015-11-09 Thread Fyodor Ustinov via Digitalmars-d-learn

Hi!

Why do need "@proprety" if everything works without it?

WBR,
Fyodor.


Re: @property

2015-11-09 Thread Alex Parrill via Digitalmars-d-learn

On Monday, 9 November 2015 at 21:56:22 UTC, Fyodor Ustinov wrote:

Hi!

Why do need "@proprety" if everything works without it?

WBR,
Fyodor.


Check out the following code:


struct Test {
int foo() {
return 2;
}
int bar() @property {
return 2;
}
}

	pragma(msg, typeof(Test.foo)); // Prints int(); i.e. a 
zero-argument function that returns an int

pragma(msg, typeof(Test.bar)); // Prints int; i.e. a plain int

AFAIK when @property was introduced, it was expected that after a 
deprecation period, calling non-property functions without 
parenthesis would be invalid, but I don't think that panned out. 
It's still good for documentation.


Re: Split range that has no length or slice?

2015-11-09 Thread Spacen Jasset via Digitalmars-d-learn

On Monday, 9 November 2015 at 18:04:23 UTC, Alex Parrill wrote:

On Monday, 9 November 2015 at 17:56:14 UTC, Alex Parrill wrote:
On Monday, 9 November 2015 at 15:23:21 UTC, Spacen Jasset 
wrote:
On Monday, 9 November 2015 at 14:58:19 UTC, Adam D. Ruppe 
wrote:
File has a .byLine and .byLineCopy method you could use to 
just get lines from it.


Ah yes. but unfortunately I don't want to do that. I want to 
pass a range into my print function. i.e. Sometimes it may 
not be a File, it might be from a string.


Looks like it's not possible without adaptor code. I would 
suggest File should have a range function on it to return a 
range, for that matter byLine and byChunk probably shouldn't 
even be associated with file at all IMHO.


The `.byLine` and `.byLineCopy` functions return ranges. Use 
those.


Scratch that, just read your post.

Ideally, something like 
`file.byChunk(4096).joiner.splitter('\n')` would work. The 
issue is that the byChunk ranges aren't forward or sliceable 
ranges (which makes sense).


Theoretically, it should be possible to implement `splitter` 
with a plain input range, but it would require that the 
sub-range that `splitter.front` returns be invalidated when 
calling `splitter.popFront`.


I don't yet understand the D ranges unfortunately. I can raise a 
bug about this if people generally agree its currently not a good 
situation. I don't see that The `.byLine` and `.byLineCopy` 
functions should be on the File object either. They are surely 
part of range algorithms, or string functions like 
std.string.lineSplitter





Re: phobos: What type to use instead of File when doing I/O on streams?

2015-11-09 Thread Alex Parrill via Digitalmars-d-learn

On Monday, 9 November 2015 at 19:42:53 UTC, J.Frank wrote:

On Monday, 9 November 2015 at 18:44:00 UTC, Alex Parrill wrote:
Ranges are streams. file.byLine(Copy) and byChunk are 
effectively streams that are ranges.


I might be wrong, but from what I read so far I don't think 
that "ranges are streams":


- Can you read an arbitrary length of bytes from a range? 
(Reading by line in my code was just an example.) The only way 
I see is to make it a byte range. But then reading n bytes 
would result in n calls to popFront(), which is out of the 
question for performance reasons. Is this correct?


`myrange.take(array_size).array`

front/popFront are very good candidates for optimization; the 
compiler should be able to inline them, removing all of the 
overhead (GCD and LDC will likely be better at this than DMD, 
though).



- Can you flush() a range?

- Can you use select() on a range?



No, but you can't do either to anything other than file 
descriptors anyway (at least on Linux, dunno about Windows), so 
you may as well pass in a File.


Re: Associative arrays

2015-11-09 Thread Brian Schott via Digitalmars-d-learn

On Tuesday, 10 November 2015 at 01:29:11 UTC, Brian Schott wrote:
Yes. It's a hack that gives you a modulus without having to do 
a modulus. It only works on powers of two.


http://graphics.stanford.edu/~seander/bithacks.html#ModulusDivisionEasy


Re: why does this error out?

2015-11-09 Thread Cauterite via Digitalmars-d-learn

Here's the output I get (DMD v2.068.2):

[1, 3, 10, 12, 21, 30, 100, 102, 111, 120, 201, 210]
core.exception.AssertError@std\range\package.d(4603): Assertion 
failure


Re: why does this error out?

2015-11-09 Thread lobo via Digitalmars-d-learn

On Tuesday, 10 November 2015 at 04:34:22 UTC, Cauterite wrote:

Here's the output I get (DMD v2.068.2):

[1, 3, 10, 12, 21, 30, 100, 102, 111, 120, 201, 210]
core.exception.AssertError@std\range\package.d(4603): Assertion 
failure


iota.front() is complaining the range is empty from this line.

harshads.filter!(h => h > 1000).front.writeln;

your hardas has no values > 1000 so the filter is empty. hardwas 
was seeded with iota(1,256), so the maximum value will be 252.


bye,
lobo





Re: std.experimental.allocator optlink error

2015-11-09 Thread ref2401 via Digitalmars-d-learn

On Monday, 9 November 2015 at 14:42:01 UTC, ref2401 wrote:

Hello

I wrote a small hello world app and imported the 
`std.experimental.allocator` module.

I'm getting the following optlink error:
---
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
console-app.obj(console-app)
Error 42: Symbol Undefined 
_D3std12experimental9allocator12__ModuleInfoZ

--- errorlevel 1
---
os: Windows 8.1 Enterprise
dmd:2.069.0
build sript:dmd main.d -ofconsole-app.exe -debug -unittest -wi

Thank you.


I tested it with my home laptop(Win 8.1 Pro). Same error.
If I change `std.experimental.allocator` to 
`std.experimental.logger` then it builds fine.


Re: Providing custom formatting without importing half of phobos.

2015-11-09 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Nov 09, 2015 at 03:09:26PM +, rumbu via Digitalmars-d-learn wrote:
> Let's say that I'm a library provider and I intend to offer a
> completely new data type suitable for printing with format() or
> writef().
> 
> According to this tutorial
> (http://wiki.dlang.org/Defining_custom_print_format_specifiers), to
> achieve this, one must import at least std.format : FormatSpec.
> 
> The problem is that just importing FormatSpec will create exactly 88
> phobos dependencies.
> 
> Is there any way to provide formatting capabilities for a custom data
> type without importing the FormatSpec thing?

Do you need *custom* format specifiers? If all you need is to be able to
write: format("%s", myData), then the only thing you need to do is to
implement one of the toString methods recognized by format(), such as:

void toString(scope void delegate(const(char)[]) dg) {
...
}

and format() should be able to just pick it up.


T

-- 
You have to expect the unexpected. -- RL


Re: Providing custom formatting without importing half of phobos.

2015-11-09 Thread rumbu via Digitalmars-d-learn

On Monday, 9 November 2015 at 16:11:23 UTC, H. S. Teoh wrote:
On Mon, Nov 09, 2015 at 03:09:26PM +, rumbu via 
Digitalmars-d-learn wrote:

[...]


Do you need *custom* format specifiers? If all you need is to 
be able to write: format("%s", myData), then the only thing you 
need to do is to implement one of the toString methods 
recognized by format(), such as:


void toString(scope void delegate(const(char)[]) dg) {
...
}

and format() should be able to just pick it up.


T
I'm working to create a decimal data type library, therefore I 
need at least %f %e %a and %g, including other data like width, 
precision and so on.


Compile time digest error

2015-11-09 Thread tcak via Digitalmars-d-learn

[code]
private static import std.digest.md;

public enum HashValue = std.digest.digest.digest!( 
std.digest.md.MD5)( "" );


void main(){}
[/code]

[output]
dmd main.d
/usr/include/dmd/phobos/std/digest/md.d(202): Error: 
reinterpreting cast from uint[16] to ubyte* is not supported in 
CTFE
/usr/include/dmd/phobos/std/digest/md.d(320):called from 
here: this.transform(_buffer)
/usr/include/dmd/phobos/std/digest/md.d(381):called from 
here: this.put(cast(const(ubyte)[])bits)
/usr/include/dmd/phobos/std/digest/digest.d(458):called 
from here: hash.finish()

main.d(3):called from here: digest("")
[/output]


I would expect MD5 to work on compile time, but as the error 
indicates, the uint[16] to ubyte* conversion prevents that. I 
tried with "std.digest.md.md5Of" as well, and same result. Is 
there any MD5 function that returns a fixed length array like 
ubyte[16] maybe?


Re: phobos: What type to use instead of File when doing I/O on streams?

2015-11-09 Thread Alex Parrill via Digitalmars-d-learn

On Monday, 9 November 2015 at 18:18:19 UTC, J.Frank wrote:

On Monday, 9 November 2015 at 14:48:35 UTC, Ali Çehreli wrote:

import std.stdio;
import std.range;

void foo(I, O)(I in_stream, O out_stream)
if (isInputRange!I &&
isOutputRange!(O, ElementType!I)) {

// in_stream.seek(3); // compile error - good :)

foreach (element; in_stream) {
out_stream.put(element);
}
}

void main(string[] args)
{
// Also consider .byLine, which is faster and risky
foo(stdin.byLineCopy,
stdout.lockingTextWriter);
}

Ali


Uhm... no. That's not a solution to my problem. Ranges are not 
(I/O-)streams.


Ranges are streams. file.byLine(Copy) and byChunk are effectively 
streams that are ranges.


The only problem with implementing your code in ranges is that 
`splitter` requires a forward range, which file-backed ranges 
aren't. Otherwise you could do 
`file.byChunk(4096).joiner.drop(3).splitter('\n')` to get a 
"stream" of lines.


Re: phobos: What type to use instead of File when doing I/O on streams?

2015-11-09 Thread J.Frank via Digitalmars-d-learn

On Monday, 9 November 2015 at 14:48:35 UTC, Ali Çehreli wrote:

import std.stdio;
import std.range;

void foo(I, O)(I in_stream, O out_stream)
if (isInputRange!I &&
isOutputRange!(O, ElementType!I)) {

// in_stream.seek(3); // compile error - good :)

foreach (element; in_stream) {
out_stream.put(element);
}
}

void main(string[] args)
{
// Also consider .byLine, which is faster and risky
foo(stdin.byLineCopy,
stdout.lockingTextWriter);
}

Ali


Uhm... no. That's not a solution to my problem. Ranges are not 
(I/O-)streams.




Re: Split range that has no length or slice?

2015-11-09 Thread Alex Parrill via Digitalmars-d-learn

On Monday, 9 November 2015 at 15:23:21 UTC, Spacen Jasset wrote:

On Monday, 9 November 2015 at 14:58:19 UTC, Adam D. Ruppe wrote:
File has a .byLine and .byLineCopy method you could use to 
just get lines from it.


Ah yes. but unfortunately I don't want to do that. I want to 
pass a range into my print function. i.e. Sometimes it may not 
be a File, it might be from a string.


Looks like it's not possible without adaptor code. I would 
suggest File should have a range function on it to return a 
range, for that matter byLine and byChunk probably shouldn't 
even be associated with file at all IMHO.


The `.byLine` and `.byLineCopy` functions return ranges. Use 
those.


Re: Split range that has no length or slice?

2015-11-09 Thread Alex Parrill via Digitalmars-d-learn

On Monday, 9 November 2015 at 17:56:14 UTC, Alex Parrill wrote:

On Monday, 9 November 2015 at 15:23:21 UTC, Spacen Jasset wrote:
On Monday, 9 November 2015 at 14:58:19 UTC, Adam D. Ruppe 
wrote:
File has a .byLine and .byLineCopy method you could use to 
just get lines from it.


Ah yes. but unfortunately I don't want to do that. I want to 
pass a range into my print function. i.e. Sometimes it may not 
be a File, it might be from a string.


Looks like it's not possible without adaptor code. I would 
suggest File should have a range function on it to return a 
range, for that matter byLine and byChunk probably shouldn't 
even be associated with file at all IMHO.


The `.byLine` and `.byLineCopy` functions return ranges. Use 
those.


Scratch that, just read your post.

Ideally, something like 
`file.byChunk(4096).joiner.splitter('\n')` would work. The issue 
is that the byChunk ranges aren't forward or sliceable ranges 
(which makes sense).


Theoretically, it should be possible to implement `splitter` with 
a plain input range, but it would require that the sub-range that 
`splitter.front` returns be invalidated when calling 
`splitter.popFront`.


Re: Split range that has no length or slice?

2015-11-09 Thread Spacen Jasset via Digitalmars-d-learn

On Monday, 9 November 2015 at 14:58:19 UTC, Adam D. Ruppe wrote:
File has a .byLine and .byLineCopy method you could use to just 
get lines from it.


Ah yes. but unfortunately I don't want to do that. I want to pass 
a range into my print function. i.e. Sometimes it may not be a 
File, it might be from a string.


Looks like it's not possible without adaptor code. I would 
suggest File should have a range function on it to return a 
range, for that matter byLine and byChunk probably shouldn't even 
be associated with file at all IMHO.





Re: Providing custom formatting without importing half of phobos.

2015-11-09 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 9 November 2015 at 15:09:28 UTC, rumbu wrote:
The problem is that just importing FormatSpec will create 
exactly 88 phobos dependencies.


I would pull request that to move it into its own independent 
module and then make std.format import that. I know that's not an 
immediate solution but it should go through by the next release; 
i doubt such a PR would be controversial.


Re: phobos: What type to use instead of File when doing I/O on streams?

2015-11-09 Thread J.Frank via Digitalmars-d-learn

On Monday, 9 November 2015 at 19:38:06 UTC, Ali Çehreli wrote:
As far as I understand, the issue is to prevent the seek() call 
at compile time, right? If so, the range types that byLine() 
and byLineCopy() return does not have such a member function.


I think the code achieves that goal because we are not dealing 
with File objects anymore.


Yes and no. Again, seek() was just an example.

I'm looking for a replacement for the deprecated inferfaces 
InputStream and OutputStream.




Re: phobos: What type to use instead of File when doing I/O on streams?

2015-11-09 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 9 November 2015 at 19:49:15 UTC, J.Frank wrote:
I'm looking for a replacement for the deprecated inferfaces 
InputStream and OutputStream.


Just keep using them. The deprecated note isn't really 
important... they will still be in Phobos for another year and 
you can copy the file out yourself to keep using it beyond that.


As of right now, there is no exact official replacement.


Re: phobos: What type to use instead of File when doing I/O on streams?

2015-11-09 Thread J.Frank via Digitalmars-d-learn

On Monday, 9 November 2015 at 18:44:00 UTC, Alex Parrill wrote:
Ranges are streams. file.byLine(Copy) and byChunk are 
effectively streams that are ranges.


I might be wrong, but from what I read so far I don't think that 
"ranges are streams":


- Can you read an arbitrary length of bytes from a range? 
(Reading by line in my code was just an example.) The only way I 
see is to make it a byte range. But then reading n bytes would 
result in n calls to popFront(), which is out of the question for 
performance reasons. Is this correct?


- Can you flush() a range?

- Can you use select() on a range?

etc.

(BTW. Where do I find select()/poll() in phobos?)



Re: phobos: What type to use instead of File when doing I/O on streams?

2015-11-09 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 9 November 2015 at 19:42:53 UTC, J.Frank wrote:

(BTW. Where do I find select()/poll() in phobos?)


They are in `core.sys.posix.sys.select`

In general, OS headers for Posix in C like "sys/select.h" can be 
gotten by "import core.sys.posix.sys.select;"  is in 
"core.sys.posix.unistd"


Linux-specific ones like sys/epoll.h are in 
`core.sys.linux.sys.epoll`


and so on


Re: phobos: What type to use instead of File when doing I/O on streams?

2015-11-09 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 9 November 2015 at 08:49:08 UTC, J.Frank wrote:
Hm. "Maybe the stream is seekable, maybe it is not" is not 
really an option for a language that is supposed to be type 
safe.


It just isn't known at compile time. Files, especially on Unix, 
are interchangeable at compile time but offer different 
capabilities at run time.



Thank you for your solution, but this sounds more lika a hack.


Wrapper structs with alias this are basically how D does 
inheritance on a struct.


Re: phobos: What type to use instead of File when doing I/O on streams?

2015-11-09 Thread Ali Çehreli via Digitalmars-d-learn

On 11/09/2015 10:18 AM, J.Frank wrote:

On Monday, 9 November 2015 at 14:48:35 UTC, Ali Çehreli wrote:

import std.stdio;
import std.range;

void foo(I, O)(I in_stream, O out_stream)
if (isInputRange!I &&
isOutputRange!(O, ElementType!I)) {

// in_stream.seek(3); // compile error - good :)

foreach (element; in_stream) {
out_stream.put(element);
}
}

void main(string[] args)
{
// Also consider .byLine, which is faster and risky
foo(stdin.byLineCopy,
stdout.lockingTextWriter);
}

Ali


Uhm... no. That's not a solution to my problem. Ranges are not
(I/O-)streams.



As far as I understand, the issue is to prevent the seek() call at 
compile time, right? If so, the range types that byLine() and 
byLineCopy() return does not have such a member function.


I think the code achieves that goal because we are not dealing with File 
objects anymore.


Ali



why does this error out?

2015-11-09 Thread steven kladitis via Digitalmars-d-learn

void main() {
import std.stdio, std.algorithm, std.range, std.conv;

enum digSum = (int n) => n.text.map!(d => d - '0').sum;
//enum harshads = iota(1, int.max).filter!(n => n % digSum(n) 
== 0);

enum harshads = iota(1, 256).filter!(n => n % digSum(n) == 0);
harshads.take(20).writeln;
harshads.filter!(h => h > 1000).front.writeln;
}

// this compiles but always prints 1 number and gets 
object.Error@(0): Access Violation


Re: Associative arrays

2015-11-09 Thread TheFlyingFiddle via Digitalmars-d-learn

On Monday, 9 November 2015 at 04:52:37 UTC, rsw0x wrote:
On Monday, 9 November 2015 at 04:29:30 UTC, Rikki Cattermole 
wrote:

On 09/11/15 4:57 PM, TheFlyingFiddle wrote:

[...]


Nope.


[...]


As far as I'm aware, you are stuck using e.g. structs to 
emulate AA behavior.
I have a VERY basic implementation here: 
https://github.com/rikkimax/alphaPhobos/blob/master/source/std/experimental/internal/containers/map.d

Feel free to steal.


Fwiw, EMSI provides high quality containers backed by 
std.experimental.allocator.

https://github.com/economicmodeling/containers


Thanks for the suggestions. I also made a hashmap using 
allocators some time ago that I use in-place of the built in 
hashmap for most of my purposes. The syntax of a custom hash map 
is somewhat lacking in comparison to the built in one however and 
I was hoping that I could either make the built in work with 
allocators or replace it with my own implementation.


In addition to this I am building a pointer patching binary 
serializer and I hoped that I could make it work with the built 
in aa without requiring to many gc allocations.


The economicmodeling one seems interesting ill try it out and see 
if it's better then the one I am currently using.





AliasSeq + isExpression type specialization behavior

2015-11-09 Thread Brian Schott via Digitalmars-d-learn

Given the following code:
```
import std.meta;

static assert(is(char : dchar));
static assert(is(AliasSeq!(int, char) : AliasSeq!(int, char)));
static assert(is(AliasSeq!(int, char) : AliasSeq!(int, dchar)));
```

The third static assert fails. Should it, given that the first 
and second pass?


Re: @property

2015-11-09 Thread Fyodor Ustinov via Digitalmars-d-learn

On Monday, 9 November 2015 at 22:06:22 UTC, Alex Parrill wrote:

Check out the following code:


struct Test {
int foo() {
return 2;
}
int bar() @property {
return 2;
}
}

	pragma(msg, typeof(Test.foo)); // Prints int(); i.e. a 
zero-argument function that returns an int

pragma(msg, typeof(Test.bar)); // Prints int; i.e. a plain int

But the compiler is lying!

void main() {
auto a = Test();
auto aa =  // if type of a.bar is int
writeln(*aa); // why: Error: can only * a pointer, not a 'int 
delegate() @property'

}

AFAIK when @property was introduced, it was expected that after 
a deprecation period, calling non-property functions without 
parenthesis would be invalid, but I don't think that panned 
out. It's still good for documentation.


If this feature will be removed, it will be very lacking code, 
like:


writeln = "Hello, world!";

:)
WBR,
Fyodor.



Re: Parse d source file by using compiler

2015-11-09 Thread ZombineDev via Digitalmars-d-learn

On Monday, 9 November 2015 at 05:49:25 UTC, tcak wrote:
I checked for a flag in this page 
http://dlang.org/dmd-linux.html , but couldn't have found any 
for this purpose.


Is there a way to parse a d source file so it generates a tree 
in JSON, XML, or something-that-can-be-processed-easily file 
format?


---

My real purpose:

I need to generate hash code (e.g. MD5) for a part of source 
code (let's say a class, struct, or a function). So whether the 
codes are changed or not can be detected. As you will guess, 
comments, text formatting etc. shouldn't affect the hash result.



Use-Case:

I am writing a code generator/back up system. It will check the 
last available code file. If important changes are done in a 
specific part of code, it will increase version number by 1.


You're use case is really interesting! AFAIK, currently, the 
lexer part of the DMD frontend is the only part that can be 
easily used standalone. Daniel Murphy (who drove a lot of the 
work towards DDMD) published the lexer on DUB: 
http://code.dlang.org/packages/ddmd. However the package is 
outdated, because it is based on DMD v2.067.
As the whole frontend is now in D, you should be able to import 
any of the D modules and work with them, but it may not be as 
easy, as their API is geared only towards the DMD driver.
I personally want to help refactor DDMD to be usable as a 
library, so you can use for all sorts of cool things (like 
runtime JIT, IDE support, REPL, and so on), but I'm quite busy at 
the moment :(




Re: phobos: What type to use instead of File when doing I/O on streams?

2015-11-09 Thread J.Frank via Digitalmars-d-learn

On Sunday, 8 November 2015 at 23:50:58 UTC, Adam D. Ruppe wrote:
You don't, in general. stdin is sometimes seekable and the 
compiler doesn't know if it is or not until you try at runtime.


Hm. "Maybe the stream is seekable, maybe it is not" is not really 
an option for a language that is supposed to be type safe.


I just found std.stream which looks very good. But it is 
deprecated??


You could write a wrapper struct for File though that @disables 
the seek function. It would have a File member, alias file 
this;, a constructor that forwards to it (optionally, you could 
also construct it as a File and assign it), and then a 
@disabled function with the same signature as the seek call in 
File. Then, it will no longer compile as long as you use your 
wrapper.


Thank you for your solution, but this sounds more lika a hack.



Re: phobos: What type to use instead of File when doing I/O on streams?

2015-11-09 Thread J.Frank via Digitalmars-d-learn

My question is now:
How can I make this work without using deprecated stuff?


import std.cstream;

void foo(InputStream in_stream, OutputStream out_stream)
{
//  in_stream.seek(3); // compile error - good :)

char[] line;
while ((line = in_stream.readLine()) !is null)
out_stream.writeLine(line);
}

void main(string[] args)
{
foo(din, dout);
}



Re: std.experimental.allocator optlink error

2015-11-09 Thread uiop via Digitalmars-d-learn

On Monday, 9 November 2015 at 18:07:39 UTC, ref2401 wrote:

On Monday, 9 November 2015 at 14:42:01 UTC, ref2401 wrote:

Hello

I wrote a small hello world app and imported the 
`std.experimental.allocator` module.

I'm getting the following optlink error:
---
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
console-app.obj(console-app)
Error 42: Symbol Undefined 
_D3std12experimental9allocator12__ModuleInfoZ

--- errorlevel 1
---
os: Windows 8.1 Enterprise
dmd:2.069.0
build sript: 	dmd main.d -ofconsole-app.exe -debug -unittest 
-wi


Thank you.


I tested it with my home laptop(Win 8.1 Pro). Same error.
If I change `std.experimental.allocator` to 
`std.experimental.logger` then it builds fine.


Can you find the sources in your setup ? Any chance that that 
phobos.lib is still the the one distributed with dmd 2.068 ? When 
you setup dmd 2.069, did you use the 7z or installer ?


Re: Parse d source file by using compiler

2015-11-09 Thread Brian Schott via Digitalmars-d-learn

On Monday, 9 November 2015 at 05:49:25 UTC, tcak wrote:
I checked for a flag in this page 
http://dlang.org/dmd-linux.html , but couldn't have found any 
for this purpose.


Is there a way to parse a d source file so it generates a tree 
in JSON, XML, or something-that-can-be-processed-easily file 
format?


---

My real purpose:

I need to generate hash code (e.g. MD5) for a part of source 
code (let's say a class, struct, or a function). So whether the 
codes are changed or not can be detected. As you will guess, 
comments, text formatting etc. shouldn't affect the hash result.



Use-Case:

I am writing a code generator/back up system. It will check the 
last available code file. If important changes are done in a 
specific part of code, it will increase version number by 1.


dscanner --ast path/to/file.d \
| xmllint --xpath 
"//classDeclaration[name='ClassYouCareAbout']" - \

| md5sum

https://github.com/Hackerpilot/Dscanner

The only problem here is that D-Scanner's XML output includes 
 tags. You should be able to strip those out with sed or 
something.


Re: Associative arrays

2015-11-09 Thread rsw0x via Digitalmars-d-learn

On Monday, 9 November 2015 at 21:33:09 UTC, TheFlyingFiddle wrote:

On Monday, 9 November 2015 at 04:52:37 UTC, rsw0x wrote:
On Monday, 9 November 2015 at 04:29:30 UTC, Rikki Cattermole 
wrote:
Fwiw, EMSI provides high quality containers backed by 
std.experimental.allocator.

https://github.com/economicmodeling/containers


I have a question regarding the implementation of the 
economicmodeling hashmap. Why must buckets be a power of two? 
Is it to be able to use the: hash & (buckets.length - 1) for 
index calculations or is there some other reason?


I have no idea, sorry.
Schott wrote them AFAIK, he might be able to respond if he sees 
this.


Re: Associative arrays

2015-11-09 Thread Brian Schott via Digitalmars-d-learn

On Monday, 9 November 2015 at 21:33:09 UTC, TheFlyingFiddle wrote:

On Monday, 9 November 2015 at 04:52:37 UTC, rsw0x wrote:
On Monday, 9 November 2015 at 04:29:30 UTC, Rikki Cattermole 
wrote:
Fwiw, EMSI provides high quality containers backed by 
std.experimental.allocator.

https://github.com/economicmodeling/containers


I have a question regarding the implementation of the 
economicmodeling hashmap. Why must buckets be a power of two? 
Is it to be able to use the: hash & (buckets.length - 1) for 
index calculations or is there some other reason?


Yes. It's a hack that gives you a modulus without having to do a 
modulus. It only works on powers of two.