Re: foreach with classes like associative array?

2012-01-20 Thread Ali Çehreli

On 01/20/2012 04:53 PM, Matt Soucy wrote:

So I was messing around with some code I've been writing today, and I
wanted to use a foreach for something, as if it were an associative
array. The problem is, I can't find any information on how to do that.
I can't use something like "alias this", because the class I'm writing
acts as a wrapper that lets me use string offsets for something that's
not string-indexed by default.
I don't see any sort of opApply or similar to do this, and the foreach
section of dlang.org doesn't help. Is there a way to do it?
Thank you,
-Matt Soucy


I have a chapter for this but it hasn't been translated yet:

  http://ddili.org/ders/d/foreach_opapply.html

Translating from there, when there is the following piece of code:

// What the programmer wrote:
foreach (/* loop variables */; object) {
// ... operations ...
}

The compiler uses the following behind the scenes:

// What the compiler uses:
object.opApply(delegate int(/* loop variables */) {
// ... operations ...
return termination_code;
});

You must terminate your loop if termination_code is non-zero. So all you 
need to do is to write an opApply overload that matches the loop variables:


class C
{
int[3] keys;
int[3] values;

int opApply(int delegate(ref int, ref int) operations) const
{
int termination_code;

for (size_t i = 0; i != keys.length; ++i) {
termination_code = operations(keys[i], values[i]);
if (termination_code) {
break;
}
}

return termination_code;
}
}

import std.stdio;

void main()
{
auto c = new C;

foreach(key, value; c) {
writefln("%s:%s", key, value);
}
}

Ali


Re: foreach with classes like associative array?

2012-01-20 Thread bearophile
Matt Soucy:

> I don't see any sort of opApply or similar to do this, and the foreach 
> section of dlang.org doesn't help. Is there a way to do it?

opApply sounds like the solution for your problem, if you don't need to define 
a Range. You are allowed to define two or more opApply with different 
type/number of arguments.

Bye,
bearophile


foreach with classes like associative array?

2012-01-20 Thread Matt Soucy
So I was messing around with some code I've been writing today, and I 
wanted to use a foreach for something, as if it were an associative 
array. The problem is, I can't find any information on how to do that.
I can't use something like "alias this", because the class I'm writing 
acts as a wrapper that lets me use string offsets for something that's 
not string-indexed by default.
I don't see any sort of opApply or similar to do this, and the foreach 
section of dlang.org doesn't help. Is there a way to do it?

Thank you,
-Matt Soucy


Re: tdlp: higher-order functions

2012-01-20 Thread Tobias Pankrath
> 
> Philippe Sigaud's template document covers everything about templates:
> 
>https://github.com/PhilippeSigaud/D-templates-tutorial
> 
> (Just download the pdf there.)

This should be on the website. 


Re: Changes for newer version...

2012-01-20 Thread Timon Gehr

On 01/19/2012 09:30 PM, Era Scarecrow wrote:

  Been a bit out of it for a while, but finding enough tools at my disposal I 
feel I can work on/convert a project now.

  I happen to have one of the first printing books (without the name on the 
cover), and although it may be worth millions in a few years, apparently 
there's been enough changes that certain features are not familiar in the 
discussions.

  So I need to ask since I don't see any obvious pages that describe it. What 
are the changes to D2, since the book's release? What has externally changed? 
(Internal implementation of features need not be mentioned if it's transparent 
to the programmer and user).


The most important changes are probably:

- inout type qualifier

The type qualifier inout can be used as a wildcard to stand for any of 
mutable, const, immutable. Its meaning is automatically deduced and is 
the same throughout a certain function definition or function call.


For example:

inout(int)[] identity(inout(int)[] x){return x;}
int[] x;
immutable(int)[] y;
const(int)[] z;
static assert(is(typeof(identity(x)) == int[]));
static assert(is(typeof(identity(y)) == immutable(int)[]));
static assert(is(typeof(identity(z)) == const(int)[]));



- function local imports
void main(){
import std.stdio;
writeln("hello world!");
}


- new function/delegate literal syntax (coming DMD 2.058)

identifier => expression
or
(id1, id2) => expression
or
(type id1, type id2) => expression

import std.stdio, std.range, std.algorithm;
void main(){
writeln("some odd squares:");
writeln(map!(x=>x^^2)(filter!(x=>x&1)(iota(1,1000;
}




Re: Merging two hashes

2012-01-20 Thread Andrej Mitrovic
Nice!

On 1/20/12, Steven Schveighoffer  wrote:
> On Fri, 20 Jan 2012 12:46:40 -0500, Andrej Mitrovic
>  wrote:
>
>> Is there a way to merge the keys from one hash to another (overwriting
>> any duplicates) without using a foreach loop? E.g.:
>>
>> void main()
>> {
>> int[int] a, b;
>> a[0] = 0;
>> b[1] = 1;
>>
>> b += a;  // ?
>> }
>>
>> It's not too hard to write this of course:
>> foreach (key, val; a)
>> b[key] = val;
>>
>> But I'm wondering if an enhancement request is in order, maybe an
>> operator overload, or maybe a special merge function.
>
> You can in dcollections :)
>
> http://www.dsource.org/projects/dcollections/browser/branches/d2/dcollections/model/Map.d#L40
>
> -Steve
>


Re: Changes for newer version...

2012-01-20 Thread Jesse Phillips

On Thursday, 19 January 2012 at 20:44:04 UTC, Era Scarecrow wrote:
Been a bit out of it for a while, but finding enough tools at 
my disposal I feel I can work on/convert a project now.


I happen to have one of the first printing books (without the 
name on the cover), and although it may be worth millions in a 
few years, apparently there's been enough changes that certain 
features are not familiar in the discussions.


So I need to ask since I don't see any obvious pages that 
describe it. What are the changes to D2, since the book's 
release? What has externally changed? (Internal implementation 
of features need not be mentioned if it's transparent to the 
programmer and user).


For the most part the language has been changing to match the 
book. There are still bugs in that regard.


http://d.puremagic.com/issues/buglist.cgi?quicksearch=[tdpl]

The book itself has its own bugs, which can be found on the 
errata page.


http://erdani.com/index.php?cID=109


Re: tdlp: higher-order functions

2012-01-20 Thread Ali Çehreli

On 01/20/2012 08:43 AM, Jerome BENOIT wrote:

> -
> T[] find(alias pred, T)(T[] input)
> if (is(typeof(pred(input[0])) == bool)) {
> for(; input.length > 0; input = input[1 .. $]) {
> if (pred(input[0])) break;
> }
> return input;
> }
> -

> Let assume that the predicate must have now two parameters in such a way
> that
> the first is of type T and the second of type TM.
>
> auto ff = find!((x, m) { return x % m != 0; })([1, 2, 3, 4, 5]);
>
> Here x would be of type T, and m of type TM.
>
>
> What does look the code for find then ?

We must decide on what 'm' is on the find() line above. Do you want to 
provide it as a template parameter or a function parameter? Here is how 
it could be provided as a function parameter (the last '2' in the 
parameter list):


auto ff = find!((x, m) { return x % m != 0; })([1, 2, 3, 4, 5], 2);

And here is a matching find() function template:

T[] find(alias pred, T, TM)(T[] input, TM m)
if (is(typeof(pred(input[0], m)) == bool))
{
for(; input.length > 0; input = input[1 .. $]) {
if (pred(input[0], m)) break;
}

return input;
}

void main()
{
auto ff = find!((x, m) { return x % m != 0; })([1, 2, 3, 4, 5], 2);
}

Notice that pred() now takes two parameters in find().

>
> Thanks in advance,
> Jerome
>
>>
>

Philippe Sigaud's template document covers everything about templates:

  https://github.com/PhilippeSigaud/D-templates-tutorial

(Just download the pdf there.)

I have a chapter about templates which intentionally covers little, 
leaving the rest of templates to a later chapter:


  http://ddili.org/ders/d.en/templates.html

The problem is, 'alias' template parameters are in that later chapter, 
which hasn't been translated yet.


Ali



Re: Merging two hashes

2012-01-20 Thread Steven Schveighoffer
On Fri, 20 Jan 2012 12:46:40 -0500, Andrej Mitrovic  
 wrote:



Is there a way to merge the keys from one hash to another (overwriting
any duplicates) without using a foreach loop? E.g.:

void main()
{
int[int] a, b;
a[0] = 0;
b[1] = 1;

b += a;  // ?
}

It's not too hard to write this of course:
foreach (key, val; a)
b[key] = val;

But I'm wondering if an enhancement request is in order, maybe an
operator overload, or maybe a special merge function.


You can in dcollections :)

http://www.dsource.org/projects/dcollections/browser/branches/d2/dcollections/model/Map.d#L40

-Steve


Re: Reading web pages

2012-01-20 Thread Bystroushaak

On 20.1.2012 18:42, Xan xan wrote:

Thank you very much. I should invite you to a beer ;-)


Write me if you will be in prag/czech republic :)


For the other hand,

I get this error:

[Excepció: std.conv.ConvException@/usr/include/d2/4.6/std/conv.d(1640):
Can't convert value `HTT' of type string to type uint]


This is very strange error, because on my computer it works well. Can 
you remove try..catch and post full error list and program parameters?


Re: Reading web pages

2012-01-20 Thread Xan xan
The same error with:
[...]
foreach (a; args[1..$]) {
|___|___|___|___write("[Longitud: ");
|___|___|___|___stdout.rawWrite(cast(ubyte[]) navegador.get(a));
|___|___|___|___writeln("]");
|___|___|___}
[...]

2012/1/20 Bystroushaak :
> rawWrite():
>
> stdout.rawWrite(cast(ubyte[]) navegador.get(a));
>
>
> On 20.1.2012 18:18, Xan xan wrote:
>>
>> Mmmm... I understand it. But is there any way of circumvent it?
>> Perhaps I could write to one file, isn't?
>>
>>
>>
>> 2012/1/20 Bystroushaak:
>>>
>>> Thats because you are trying writeln binary data, and that is impossible,
>>> because writeln IMHO checks UTF8 validity.
>>>
>>>
>>> On 20.1.2012 18:08, Xan xan wrote:


 Before and now, I get this error:

 $ ./spider http://static.arxiv.org/pdf/1109.4897.pdf
 [Excepció: std.conv.ConvException@/usr/include/d2/4.6/std/conv.d(1640):
 Can't convert value `HTT' of type string to type uint]

 The code:

 //D 2.0
 //gdmd-4.6    =>    surt el fitxer amb el mateix nom i .o
 //Usa https://github.com/Bystroushaak/DHTTPClient
 import std.stdio, std.string, std.conv, std.stream;
 import std.socket, std.socketstream;
 import dhttpclient;

 int main(string [] args)
 {
     if (args.length<    2) {
                writeln("Usage:");
                writeln("   ./spider {,, ...}");
                return 0;
        }
        else {
                try {
                        string[string] capcalera = dhttpclient.FFHeaders;
                        //capcalera["User-Agent"] = "arachnida yottiuma";
                        HTTPClient navegador = new HTTPClient();
                        navegador.setClientHeaders(capcalera);

                        foreach (a; args[1..$]) {
                                writeln("[Contingut: ", cast(ubyte[])
 navegador.get(a), "]");
                        }
                }
                catch (Exception e) {
                        writeln("[Excepció: ", e, "]");
                }
                return 0;
        }
 }



 What happens?


 2012/1/20 Bystroushaak:
>
>
> It is unlimited, you just have to cast output to ubyte[]:
>
> std.file.write("logo3w.png", cast(ubyte[])
> cl.get("http://www.google.cz/images/srpr/logo3w.png";));
>
>
>>>
>


Merging two hashes

2012-01-20 Thread Andrej Mitrovic
Is there a way to merge the keys from one hash to another (overwriting
any duplicates) without using a foreach loop? E.g.:

void main()
{
int[int] a, b;
a[0] = 0;
b[1] = 1;

b += a;  // ?
}

It's not too hard to write this of course:
foreach (key, val; a)
b[key] = val;

But I'm wondering if an enhancement request is in order, maybe an
operator overload, or maybe a special merge function.


Re: Reading web pages

2012-01-20 Thread Xan xan
Thank you very much. I should invite you to a beer ;-)

For the other hand,

I get this error:

[Excepció: std.conv.ConvException@/usr/include/d2/4.6/std/conv.d(1640):
Can't convert value `HTT' of type string to type uint]


if I only want the length:

//D 2.0
//gdmd-4.6  dhttpclient => surt el fitxer amb el mateix nom i .o
//Usa https://github.com/Bystroushaak/DHTTPClient
//versió 0.0.2
import std.stdio, std.string, std.conv, std.stream;
import std.socket, std.socketstream;
import dhttpclient;

int main(string [] args)
{
if (args.length < 2) {
writeln("Usage:");
writeln("   ./spider {, , ...}");
return 0;
}
else {
try {
string[string] capcalera = dhttpclient.FFHeaders;
HTTPClient navegador = new HTTPClient();
navegador.setClientHeaders(capcalera);

foreach (a; args[1..$]) {
auto tamany = cast(ubyte[]) navegador.get(a);
writeln("[Contingut: ", tamany.length, "]");
}
}
catch (Exception e) {
writeln("[Excepció: ", e, "]");
}
return 0;
}
}


In theory, tamany.length is completely defined.

Xan.

2012/1/20 Bystroushaak :
> rawWrite():
>
> stdout.rawWrite(cast(ubyte[]) navegador.get(a));
>
>
> On 20.1.2012 18:18, Xan xan wrote:
>>
>> Mmmm... I understand it. But is there any way of circumvent it?
>> Perhaps I could write to one file, isn't?
>>
>>
>>
>> 2012/1/20 Bystroushaak:
>>>
>>> Thats because you are trying writeln binary data, and that is impossible,
>>> because writeln IMHO checks UTF8 validity.
>>>
>>>
>>> On 20.1.2012 18:08, Xan xan wrote:


 Before and now, I get this error:

 $ ./spider http://static.arxiv.org/pdf/1109.4897.pdf
 [Excepció: std.conv.ConvException@/usr/include/d2/4.6/std/conv.d(1640):
 Can't convert value `HTT' of type string to type uint]

 The code:

 //D 2.0
 //gdmd-4.6    =>    surt el fitxer amb el mateix nom i .o
 //Usa https://github.com/Bystroushaak/DHTTPClient
 import std.stdio, std.string, std.conv, std.stream;
 import std.socket, std.socketstream;
 import dhttpclient;

 int main(string [] args)
 {
     if (args.length<    2) {
                writeln("Usage:");
                writeln("   ./spider {,, ...}");
                return 0;
        }
        else {
                try {
                        string[string] capcalera = dhttpclient.FFHeaders;
                        //capcalera["User-Agent"] = "arachnida yottiuma";
                        HTTPClient navegador = new HTTPClient();
                        navegador.setClientHeaders(capcalera);

                        foreach (a; args[1..$]) {
                                writeln("[Contingut: ", cast(ubyte[])
 navegador.get(a), "]");
                        }
                }
                catch (Exception e) {
                        writeln("[Excepció: ", e, "]");
                }
                return 0;
        }
 }



 What happens?


 2012/1/20 Bystroushaak:
>
>
> It is unlimited, you just have to cast output to ubyte[]:
>
> std.file.write("logo3w.png", cast(ubyte[])
> cl.get("http://www.google.cz/images/srpr/logo3w.png";));
>
>
>>>
>


Re: Reading web pages

2012-01-20 Thread Bystroushaak

rawWrite():

stdout.rawWrite(cast(ubyte[]) navegador.get(a));

On 20.1.2012 18:18, Xan xan wrote:

Mmmm... I understand it. But is there any way of circumvent it?
Perhaps I could write to one file, isn't?



2012/1/20 Bystroushaak:

Thats because you are trying writeln binary data, and that is impossible,
because writeln IMHO checks UTF8 validity.


On 20.1.2012 18:08, Xan xan wrote:


Before and now, I get this error:

$ ./spider http://static.arxiv.org/pdf/1109.4897.pdf
[Excepció: std.conv.ConvException@/usr/include/d2/4.6/std/conv.d(1640):
Can't convert value `HTT' of type string to type uint]

The code:

//D 2.0
//gdmd-4.6=>surt el fitxer amb el mateix nom i .o
//Usa https://github.com/Bystroushaak/DHTTPClient
import std.stdio, std.string, std.conv, std.stream;
import std.socket, std.socketstream;
import dhttpclient;

int main(string [] args)
{
 if (args.length<2) {
writeln("Usage:");
writeln("   ./spider {,, ...}");
return 0;
}
else {
try {
string[string] capcalera = dhttpclient.FFHeaders;
//capcalera["User-Agent"] = "arachnida yottiuma";
HTTPClient navegador = new HTTPClient();
navegador.setClientHeaders(capcalera);

foreach (a; args[1..$]) {
writeln("[Contingut: ", cast(ubyte[])
navegador.get(a), "]");
}
}
catch (Exception e) {
writeln("[Excepció: ", e, "]");
}
return 0;
}
}



What happens?


2012/1/20 Bystroushaak:


It is unlimited, you just have to cast output to ubyte[]:

std.file.write("logo3w.png", cast(ubyte[])
cl.get("http://www.google.cz/images/srpr/logo3w.png";));






Re: Reading web pages

2012-01-20 Thread Xan xan
Mmmm... I understand it. But is there any way of circumvent it?
Perhaps I could write to one file, isn't?



2012/1/20 Bystroushaak :
> Thats because you are trying writeln binary data, and that is impossible,
> because writeln IMHO checks UTF8 validity.
>
>
> On 20.1.2012 18:08, Xan xan wrote:
>>
>> Before and now, I get this error:
>>
>> $ ./spider http://static.arxiv.org/pdf/1109.4897.pdf
>> [Excepció: std.conv.ConvException@/usr/include/d2/4.6/std/conv.d(1640):
>> Can't convert value `HTT' of type string to type uint]
>>
>> The code:
>>
>> //D 2.0
>> //gdmd-4.6  =>  surt el fitxer amb el mateix nom i .o
>> //Usa https://github.com/Bystroushaak/DHTTPClient
>> import std.stdio, std.string, std.conv, std.stream;
>> import std.socket, std.socketstream;
>> import dhttpclient;
>>
>> int main(string [] args)
>> {
>>     if (args.length<  2) {
>>                writeln("Usage:");
>>                writeln("   ./spider {,, ...}");
>>                return 0;
>>        }
>>        else {
>>                try {
>>                        string[string] capcalera = dhttpclient.FFHeaders;
>>                        //capcalera["User-Agent"] = "arachnida yottiuma";
>>                        HTTPClient navegador = new HTTPClient();
>>                        navegador.setClientHeaders(capcalera);
>>
>>                        foreach (a; args[1..$]) {
>>                                writeln("[Contingut: ", cast(ubyte[])
>> navegador.get(a), "]");
>>                        }
>>                }
>>                catch (Exception e) {
>>                        writeln("[Excepció: ", e, "]");
>>                }
>>                return 0;
>>        }
>> }
>>
>>
>>
>> What happens?
>>
>>
>> 2012/1/20 Bystroushaak:
>>>
>>> It is unlimited, you just have to cast output to ubyte[]:
>>>
>>> std.file.write("logo3w.png", cast(ubyte[])
>>> cl.get("http://www.google.cz/images/srpr/logo3w.png";));
>>>
>>>
>


Re: Reading web pages

2012-01-20 Thread Bystroushaak
Thats because you are trying writeln binary data, and that is 
impossible, because writeln IMHO checks UTF8 validity.


On 20.1.2012 18:08, Xan xan wrote:

Before and now, I get this error:

$ ./spider http://static.arxiv.org/pdf/1109.4897.pdf
[Excepció: std.conv.ConvException@/usr/include/d2/4.6/std/conv.d(1640):
Can't convert value `HTT' of type string to type uint]

The code:

//D 2.0
//gdmd-4.6  =>  surt el fitxer amb el mateix nom i .o
//Usa https://github.com/Bystroushaak/DHTTPClient
import std.stdio, std.string, std.conv, std.stream;
import std.socket, std.socketstream;
import dhttpclient;

int main(string [] args)
{
 if (args.length<  2) {
writeln("Usage:");
writeln("   ./spider {,, ...}");
return 0;
}
else {
try {
string[string] capcalera = dhttpclient.FFHeaders;
//capcalera["User-Agent"] = "arachnida yottiuma";
HTTPClient navegador = new HTTPClient();
navegador.setClientHeaders(capcalera);

foreach (a; args[1..$]) {
writeln("[Contingut: ", cast(ubyte[]) navegador.get(a), 
"]");
}
}
catch (Exception e) {
writeln("[Excepció: ", e, "]");
}
return 0;
}
}



What happens?


2012/1/20 Bystroushaak:

It is unlimited, you just have to cast output to ubyte[]:

std.file.write("logo3w.png", cast(ubyte[])
cl.get("http://www.google.cz/images/srpr/logo3w.png";));




Re: Reading web pages

2012-01-20 Thread Xan xan
Thanks, but what fails that, because I downloaded as collection of
bytes. No matter if a file is a pdf, png or whatever if I downloaded
as bytes, isn't?

Thanks,


2012/1/20 Bystroushaak :
> If you want to know what type of file you just downloaded, look at
> .getResponseHeaders():
>
>
>  std.file.write("logo3w.png", cast(ubyte[])
> cl.get("http://www.google.cz/images/srpr/logo3w.png";));
>  writeln(cl.getResponseHeaders()["Content-Type"]);
>
> Which will print in this case: image/png
>
> Here is full example:
> https://github.com/Bystroushaak/DHTTPClient/blob/master/examples/download_binary_file.d
>
>
> On 20.1.2012 18:00, Bystroushaak wrote:
>>
>> It is unlimited, you just have to cast output to ubyte[]:
>>
>> std.file.write("logo3w.png", cast(ubyte[])
>> cl.get("http://www.google.cz/images/srpr/logo3w.png";));
>>
>> On 20.1.2012 17:53, Xan xan wrote:
>>>
>>> Thank you very much, Bystroushaak.
>>> I see you limite httpclient to xml/html documents. Is there
>>> possibility of download any files (and not only html or xml). Just
>>> like:
>>>
>>> HTTPClient navegador = new HTTPClient();
>>> auto file = navegador.download("http://www.google.com/myfile.pdf";)
>>>
>>> ?
>>>
>>> Thanks a lot,
>>>
>>>
>>>
>>> 2012/1/20 Bystroushaak:

 First version was buggy. I've updated code at github, so if you want
 to try
 it, pull new version (git pull). I've also added new example into
 examples/user_agent_change.d


 On 20.1.2012 16:08, Bystroushaak wrote:
>
>
> There are two ways:
>
> Change global variable for module:
>
> dhttpclient.DefaultHeaders = dhttpclient.IEHeaders; // or your own
>
> This will change headers for all clients.
>
> ---
>
> Change instance headers:
>
> string[string] my_headers = dhttpclient.FFHeaders; // there are more
> headers than just User-Agent and you have to copy it
> my_headers["User-Agent"] = "My own spider!";
>
> HTTPClient navegador = new HTTPClient();
> navegador.setClientHeaders(my_headers);
>
> ---
>
> Headers are defined as:
>
> public enum string[string] FFHeaders = [
> "User-Agent" : "Mozilla/5.0 (Windows; U; Windows NT 5.1; cs;
> rv:1.9.2.3)
> Gecko/20100401 Firefox/3.6.13",
> "Accept" :
>
>
> "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain",
>
>
> "Accept-Language" : "cs,en-us;q=0.7,en;q=0.3",
> "Accept-Charset" : "utf-8",
> "Keep-Alive" : "300",
> "Connection" : "keep-alive"
> ];
>
> /// Headers from firefox 3.6.13 on Linux
> public enum string[string] LFFHeaders = [
> "User-Agent" : "Mozilla/5.0 (X11; U; Linux i686; cs; rv:1.9.2.3)
> Gecko/20100401 Firefox/3.6.13",
> "Accept" :
>
>
> "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain",
>
>
> "Accept-Language" : "cs,en-us;q=0.7,en;q=0.3",
> "Accept-Charset" : "utf-8",
> "Keep-Alive" : "300",
> "Connection" : "keep-alive"
> ];
>
> Accept, Accept-Charset, Kepp-ALive and Connection are important and if
> you redefine it, module can stop work with some servers.
>
> On 20.1.2012 15:56, Xan xan wrote:
>>
>>
>> On the other hand, I see dhttpclient identifies as
>> "Mozilla/5.0 (Windows; U; Windows NT 5.1; cs; rv:1.9.2.3)
>> Gecko/20100401 Firefox/3.6.13"
>>
>> How can I change that?


Re: Reading web pages

2012-01-20 Thread Xan xan
Before and now, I get this error:

$ ./spider http://static.arxiv.org/pdf/1109.4897.pdf
[Excepció: std.conv.ConvException@/usr/include/d2/4.6/std/conv.d(1640):
Can't convert value `HTT' of type string to type uint]

The code:

//D 2.0
//gdmd-4.6  => surt el fitxer amb el mateix nom i .o
//Usa https://github.com/Bystroushaak/DHTTPClient
import std.stdio, std.string, std.conv, std.stream;
import std.socket, std.socketstream;
import dhttpclient;

int main(string [] args)
{
if (args.length < 2) {
writeln("Usage:");
writeln("   ./spider {, , ...}");
return 0;
}
else {
try {
string[string] capcalera = dhttpclient.FFHeaders;
//capcalera["User-Agent"] = "arachnida yottiuma";
HTTPClient navegador = new HTTPClient();
navegador.setClientHeaders(capcalera);

foreach (a; args[1..$]) {
writeln("[Contingut: ", cast(ubyte[]) 
navegador.get(a), "]");
}
}
catch (Exception e) {
writeln("[Excepció: ", e, "]");
}
return 0;
}
}



What happens?


2012/1/20 Bystroushaak :
> It is unlimited, you just have to cast output to ubyte[]:
>
> std.file.write("logo3w.png", cast(ubyte[])
> cl.get("http://www.google.cz/images/srpr/logo3w.png";));
>
>


Re: Reading web pages

2012-01-20 Thread Bystroushaak
If you want to know what type of file you just downloaded, look at 
.getResponseHeaders():


  std.file.write("logo3w.png", cast(ubyte[]) 
cl.get("http://www.google.cz/images/srpr/logo3w.png";));

  writeln(cl.getResponseHeaders()["Content-Type"]);

Which will print in this case: image/png

Here is full example: 
https://github.com/Bystroushaak/DHTTPClient/blob/master/examples/download_binary_file.d


On 20.1.2012 18:00, Bystroushaak wrote:

It is unlimited, you just have to cast output to ubyte[]:

std.file.write("logo3w.png", cast(ubyte[])
cl.get("http://www.google.cz/images/srpr/logo3w.png";));

On 20.1.2012 17:53, Xan xan wrote:

Thank you very much, Bystroushaak.
I see you limite httpclient to xml/html documents. Is there
possibility of download any files (and not only html or xml). Just
like:

HTTPClient navegador = new HTTPClient();
auto file = navegador.download("http://www.google.com/myfile.pdf";)

?

Thanks a lot,



2012/1/20 Bystroushaak:

First version was buggy. I've updated code at github, so if you want
to try
it, pull new version (git pull). I've also added new example into
examples/user_agent_change.d


On 20.1.2012 16:08, Bystroushaak wrote:


There are two ways:

Change global variable for module:

dhttpclient.DefaultHeaders = dhttpclient.IEHeaders; // or your own

This will change headers for all clients.

---

Change instance headers:

string[string] my_headers = dhttpclient.FFHeaders; // there are more
headers than just User-Agent and you have to copy it
my_headers["User-Agent"] = "My own spider!";

HTTPClient navegador = new HTTPClient();
navegador.setClientHeaders(my_headers);

---

Headers are defined as:

public enum string[string] FFHeaders = [
"User-Agent" : "Mozilla/5.0 (Windows; U; Windows NT 5.1; cs;
rv:1.9.2.3)
Gecko/20100401 Firefox/3.6.13",
"Accept" :

"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain",


"Accept-Language" : "cs,en-us;q=0.7,en;q=0.3",
"Accept-Charset" : "utf-8",
"Keep-Alive" : "300",
"Connection" : "keep-alive"
];

/// Headers from firefox 3.6.13 on Linux
public enum string[string] LFFHeaders = [
"User-Agent" : "Mozilla/5.0 (X11; U; Linux i686; cs; rv:1.9.2.3)
Gecko/20100401 Firefox/3.6.13",
"Accept" :

"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain",


"Accept-Language" : "cs,en-us;q=0.7,en;q=0.3",
"Accept-Charset" : "utf-8",
"Keep-Alive" : "300",
"Connection" : "keep-alive"
];

Accept, Accept-Charset, Kepp-ALive and Connection are important and if
you redefine it, module can stop work with some servers.

On 20.1.2012 15:56, Xan xan wrote:


On the other hand, I see dhttpclient identifies as
"Mozilla/5.0 (Windows; U; Windows NT 5.1; cs; rv:1.9.2.3)
Gecko/20100401 Firefox/3.6.13"

How can I change that?


Re: Reading web pages

2012-01-20 Thread Bystroushaak

It is unlimited, you just have to cast output to ubyte[]:

std.file.write("logo3w.png", cast(ubyte[]) 
cl.get("http://www.google.cz/images/srpr/logo3w.png";));


On 20.1.2012 17:53, Xan xan wrote:

Thank you very much, Bystroushaak.
I see you limite httpclient to xml/html documents. Is there
possibility of download any files (and not only html or xml). Just
like:

HTTPClient navegador = new HTTPClient();
auto file = navegador.download("http://www.google.com/myfile.pdf";)

?

Thanks a lot,



2012/1/20 Bystroushaak:

First version was buggy. I've updated code at github, so if you want to try
it, pull new version (git pull). I've also added new example into
examples/user_agent_change.d


On 20.1.2012 16:08, Bystroushaak wrote:


There are two ways:

Change global variable for module:

dhttpclient.DefaultHeaders = dhttpclient.IEHeaders; // or your own

This will change headers for all clients.

---

Change instance headers:

string[string] my_headers = dhttpclient.FFHeaders; // there are more
headers than just User-Agent and you have to copy it
my_headers["User-Agent"] = "My own spider!";

HTTPClient navegador = new HTTPClient();
navegador.setClientHeaders(my_headers);

---

Headers are defined as:

public enum string[string] FFHeaders = [
"User-Agent" : "Mozilla/5.0 (Windows; U; Windows NT 5.1; cs; rv:1.9.2.3)
Gecko/20100401 Firefox/3.6.13",
"Accept" :

"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain",

"Accept-Language" : "cs,en-us;q=0.7,en;q=0.3",
"Accept-Charset" : "utf-8",
"Keep-Alive" : "300",
"Connection" : "keep-alive"
];

/// Headers from firefox 3.6.13 on Linux
public enum string[string] LFFHeaders = [
"User-Agent" : "Mozilla/5.0 (X11; U; Linux i686; cs; rv:1.9.2.3)
Gecko/20100401 Firefox/3.6.13",
"Accept" :

"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain",

"Accept-Language" : "cs,en-us;q=0.7,en;q=0.3",
"Accept-Charset" : "utf-8",
"Keep-Alive" : "300",
"Connection" : "keep-alive"
];

Accept, Accept-Charset, Kepp-ALive and Connection are important and if
you redefine it, module can stop work with some servers.

On 20.1.2012 15:56, Xan xan wrote:


On the other hand, I see dhttpclient identifies as
"Mozilla/5.0 (Windows; U; Windows NT 5.1; cs; rv:1.9.2.3)
Gecko/20100401 Firefox/3.6.13"

How can I change that?


Re: Reading web pages

2012-01-20 Thread Xan xan
Thank you very much, Bystroushaak.
I see you limite httpclient to xml/html documents. Is there
possibility of download any files (and not only html or xml). Just
like:

HTTPClient navegador = new HTTPClient();
auto file = navegador.download("http://www.google.com/myfile.pdf";)

?

Thanks a lot,



2012/1/20 Bystroushaak :
> First version was buggy. I've updated code at github, so if you want to try
> it, pull new version (git pull). I've also added new example into
> examples/user_agent_change.d
>
>
> On 20.1.2012 16:08, Bystroushaak wrote:
>>
>> There are two ways:
>>
>> Change global variable for module:
>>
>> dhttpclient.DefaultHeaders = dhttpclient.IEHeaders; // or your own
>>
>> This will change headers for all clients.
>>
>> ---
>>
>> Change instance headers:
>>
>> string[string] my_headers = dhttpclient.FFHeaders; // there are more
>> headers than just User-Agent and you have to copy it
>> my_headers["User-Agent"] = "My own spider!";
>>
>> HTTPClient navegador = new HTTPClient();
>> navegador.setClientHeaders(my_headers);
>>
>> ---
>>
>> Headers are defined as:
>>
>> public enum string[string] FFHeaders = [
>> "User-Agent" : "Mozilla/5.0 (Windows; U; Windows NT 5.1; cs; rv:1.9.2.3)
>> Gecko/20100401 Firefox/3.6.13",
>> "Accept" :
>>
>> "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain",
>>
>> "Accept-Language" : "cs,en-us;q=0.7,en;q=0.3",
>> "Accept-Charset" : "utf-8",
>> "Keep-Alive" : "300",
>> "Connection" : "keep-alive"
>> ];
>>
>> /// Headers from firefox 3.6.13 on Linux
>> public enum string[string] LFFHeaders = [
>> "User-Agent" : "Mozilla/5.0 (X11; U; Linux i686; cs; rv:1.9.2.3)
>> Gecko/20100401 Firefox/3.6.13",
>> "Accept" :
>>
>> "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain",
>>
>> "Accept-Language" : "cs,en-us;q=0.7,en;q=0.3",
>> "Accept-Charset" : "utf-8",
>> "Keep-Alive" : "300",
>> "Connection" : "keep-alive"
>> ];
>>
>> Accept, Accept-Charset, Kepp-ALive and Connection are important and if
>> you redefine it, module can stop work with some servers.
>>
>> On 20.1.2012 15:56, Xan xan wrote:
>>>
>>> On the other hand, I see dhttpclient identifies as
>>> "Mozilla/5.0 (Windows; U; Windows NT 5.1; cs; rv:1.9.2.3)
>>> Gecko/20100401 Firefox/3.6.13"
>>>
>>> How can I change that?


Re: tdlp: higher-order functions

2012-01-20 Thread Jerome BENOIT



On 20/01/12 17:23, Alex Rønne Petersen wrote:

On 20-01-2012 17:14, Jerome BENOIT wrote:

Hello Again:

On 20/01/12 15:58, Alex Rønne Petersen wrote:

On 20-01-2012 15:32, Jerome BENOIT wrote:

Hello List:

In tDlp book in section 5.6 entitled `Higher-Order Functions. Function
Literals,
the first code example is:

-
T[] find(alias pred, T)(T[] input)
if (is(typeof(pred(input[0])) == bool)) {
for(; input.length > 0; input = input[1 .. $]) {
if (pred(input[0])) break;
}
return input;
}
-

I can play it. Nevertheless, it is not clear for me right now
what is the role played by `T' in the generic argument list (alias pred,
T).
(Its meaning is bypassed in this section via a `deduction' way.)

Any hint is welcome.

Thanks in advance,
Jerome



It's important to realize that D does not have generics; rather, it
has templates. What (alias pred, T) means is that it takes (virtually)
any argument as the first template parameter (function literals
included) and a type for the second parameter.

You can call this function like so:

auto ints = find!((x) { return x % 2 != 0; })([1, 2, 3, 4, 5]);

Here, the type parameter T gets deduced from the argument, which is an
array of ints.


I am certainly still very confused here, but I guess it is important I
get the point before to gig further D:
In (alias pred, T), are both `pred' and `T' parameters for the function
find ? or is `T' some kind of parameter for `pred' ?

Thanks in advance,
Jerome






Both pred and T are so-called template parameters.


Ok !

 That is, they are statically resolved at compile time.


You could as well have called the function like so:

auto ints = find!((x) { return x % 2 != 0; }, int)([1, 2, 3, 4, 5]);


It is getting clearer in my mind.



Notice how we have two parameter lists: The first one where we pass a function 
literal and the type int is the template parameter list. The second one is the 
actual runtime arguments to the function.


I got this part of the story: I guess that I was confused by the alias.

Let assume that the predicate  must have now two parameters in such a way that
the first is of type T and the second of type TM.

auto ff = find!((x, m) { return x % m != 0; })([1, 2, 3, 4, 5]);

Here x would be of type T, and m of type TM.


What does look the code for find then ?

Thanks in advance,
Jerome







Re: Passing arguments to a new thread

2012-01-20 Thread Mars

On Friday, 20 January 2012 at 15:33:34 UTC, Timon Gehr wrote:

On 01/20/2012 03:12 PM, Mars wrote:

Hello everybody.
As the title states, I want to run a function in a new thread, 
and pass
it some arguments. How would I do that? I guess I could make a 
class,
deriving from Thread, and work with it, but out of curiosity, 
I'd like

to know if it's possible with a simple function.

Mars


See std.concurrency.

auto tid = spawn(&function, arg1, arg2, arg3, ...);


Very interesting, thank you. I'm currently writing a little 
client<>server application, and wanted to use 1 thread per 
client, for simplicity, but with this it's probably not harder to 
just loop through non-blocking recvs, and pass something to a 
handler thread. Am I right?


Re: tdlp: higher-order functions

2012-01-20 Thread Alex Rønne Petersen

On 20-01-2012 17:14, Jerome BENOIT wrote:

Hello Again:

On 20/01/12 15:58, Alex Rønne Petersen wrote:

On 20-01-2012 15:32, Jerome BENOIT wrote:

Hello List:

In tDlp book in section 5.6 entitled `Higher-Order Functions. Function
Literals,
the first code example is:

-
T[] find(alias pred, T)(T[] input)
if (is(typeof(pred(input[0])) == bool)) {
for(; input.length > 0; input = input[1 .. $]) {
if (pred(input[0])) break;
}
return input;
}
-

I can play it. Nevertheless, it is not clear for me right now
what is the role played by `T' in the generic argument list (alias pred,
T).
(Its meaning is bypassed in this section via a `deduction' way.)

Any hint is welcome.

Thanks in advance,
Jerome



It's important to realize that D does not have generics; rather, it
has templates. What (alias pred, T) means is that it takes (virtually)
any argument as the first template parameter (function literals
included) and a type for the second parameter.

You can call this function like so:

auto ints = find!((x) { return x % 2 != 0; })([1, 2, 3, 4, 5]);

Here, the type parameter T gets deduced from the argument, which is an
array of ints.


I am certainly still very confused here, but I guess it is important I
get the point before to gig further D:
In (alias pred, T), are both `pred' and `T' parameters for the function
find ? or is `T' some kind of parameter for `pred' ?

Thanks in advance,
Jerome






Both pred and T are so-called template parameters. That is, they are 
statically resolved at compile time.


You could as well have called the function like so:

auto ints = find!((x) { return x % 2 != 0; }, int)([1, 2, 3, 4, 5]);

Notice how we have two parameter lists: The first one where we pass a 
function literal and the type int is the template parameter list. The 
second one is the actual runtime arguments to the function.


--
- Alex


Re: tdlp: higher-order functions

2012-01-20 Thread Jerome BENOIT

Hello Again:

On 20/01/12 15:58, Alex Rønne Petersen wrote:

On 20-01-2012 15:32, Jerome BENOIT wrote:

Hello List:

In tDlp book in section 5.6 entitled `Higher-Order Functions. Function
Literals,
the first code example is:

-
T[] find(alias pred, T)(T[] input)
if (is(typeof(pred(input[0])) == bool)) {
for(; input.length > 0; input = input[1 .. $]) {
if (pred(input[0])) break;
}
return input;
}
-

I can play it. Nevertheless, it is not clear for me right now
what is the role played by `T' in the generic argument list (alias pred,
T).
(Its meaning is bypassed in this section via a `deduction' way.)

Any hint is welcome.

Thanks in advance,
Jerome



It's important to realize that D does not have generics; rather, it has 
templates. What (alias pred, T) means is that it takes (virtually) any argument 
as the first template parameter (function literals included) and a type for the 
second parameter.

You can call this function like so:

auto ints = find!((x) { return x % 2 != 0; })([1, 2, 3, 4, 5]);

Here, the type parameter T gets deduced from the argument, which is an array of 
ints.


I am certainly still very confused here, but I guess it is important I get the 
point before to gig further D:
In (alias pred, T), are both `pred' and `T' parameters for the function find ? 
or is `T' some kind of parameter for `pred' ?

Thanks in advance,
Jerome  







Re: Reading web pages

2012-01-20 Thread Bystroushaak
First version was buggy. I've updated code at github, so if you want to 
try it, pull new version (git pull). I've also added new example into 
examples/user_agent_change.d


On 20.1.2012 16:08, Bystroushaak wrote:

There are two ways:

Change global variable for module:

dhttpclient.DefaultHeaders = dhttpclient.IEHeaders; // or your own

This will change headers for all clients.

---

Change instance headers:

string[string] my_headers = dhttpclient.FFHeaders; // there are more
headers than just User-Agent and you have to copy it
my_headers["User-Agent"] = "My own spider!";

HTTPClient navegador = new HTTPClient();
navegador.setClientHeaders(my_headers);

---

Headers are defined as:

public enum string[string] FFHeaders = [
"User-Agent" : "Mozilla/5.0 (Windows; U; Windows NT 5.1; cs; rv:1.9.2.3)
Gecko/20100401 Firefox/3.6.13",
"Accept" :
"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain",

"Accept-Language" : "cs,en-us;q=0.7,en;q=0.3",
"Accept-Charset" : "utf-8",
"Keep-Alive" : "300",
"Connection" : "keep-alive"
];

/// Headers from firefox 3.6.13 on Linux
public enum string[string] LFFHeaders = [
"User-Agent" : "Mozilla/5.0 (X11; U; Linux i686; cs; rv:1.9.2.3)
Gecko/20100401 Firefox/3.6.13",
"Accept" :
"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain",

"Accept-Language" : "cs,en-us;q=0.7,en;q=0.3",
"Accept-Charset" : "utf-8",
"Keep-Alive" : "300",
"Connection" : "keep-alive"
];

Accept, Accept-Charset, Kepp-ALive and Connection are important and if
you redefine it, module can stop work with some servers.

On 20.1.2012 15:56, Xan xan wrote:

On the other hand, I see dhttpclient identifies as
"Mozilla/5.0 (Windows; U; Windows NT 5.1; cs; rv:1.9.2.3)
Gecko/20100401 Firefox/3.6.13"

How can I change that?


Re: Passing arguments to a new thread

2012-01-20 Thread Timon Gehr

On 01/20/2012 03:12 PM, Mars wrote:

Hello everybody.
As the title states, I want to run a function in a new thread, and pass
it some arguments. How would I do that? I guess I could make a class,
deriving from Thread, and work with it, but out of curiosity, I'd like
to know if it's possible with a simple function.

Mars


See std.concurrency.

auto tid = spawn(&function, arg1, arg2, arg3, ...);


Re: tdlp: higher-order functions

2012-01-20 Thread Jerome BENOIT

Thanks.

Let go further.


On 20/01/12 15:58, Alex Rønne Petersen wrote:

On 20-01-2012 15:32, Jerome BENOIT wrote:

Hello List:

In tDlp book in section 5.6 entitled `Higher-Order Functions. Function
Literals,
the first code example is:

-
T[] find(alias pred, T)(T[] input)
if (is(typeof(pred(input[0])) == bool)) {
for(; input.length > 0; input = input[1 .. $]) {
if (pred(input[0])) break;
}
return input;
}
-

I can play it. Nevertheless, it is not clear for me right now
what is the role played by `T' in the generic argument list (alias pred,
T).
(Its meaning is bypassed in this section via a `deduction' way.)

Any hint is welcome.

Thanks in advance,
Jerome



It's important to realize that D does not have generics; rather, it has 
templates. What (alias pred, T) means is that it takes (virtually) any argument 
as the first template parameter (function literals included) and a type for the 
second parameter.

You can call this function like so:

auto ints = find!((x) { return x % 2 != 0; })([1, 2, 3, 4, 5]);

Here, the type parameter T gets deduced from the argument, which is an array of 
ints.



What would be the non-deducible version ?

Jerome




Re: Reading web pages

2012-01-20 Thread Bystroushaak

There are two ways:

Change global variable for module:

dhttpclient.DefaultHeaders = dhttpclient.IEHeaders; // or your own

This will change headers for all clients.

---

Change instance headers:

string[string] my_headers = dhttpclient.FFHeaders; // there are more 
headers than just User-Agent and you have to copy it

my_headers["User-Agent"] = "My own spider!";

HTTPClient navegador = new HTTPClient();
navegador.setClientHeaders(my_headers);

---

Headers are defined as:

public enum string[string] FFHeaders = [
  "User-Agent" : "Mozilla/5.0 (Windows; U; Windows NT 5.1; cs; 
rv:1.9.2.3) Gecko/20100401 Firefox/3.6.13",
  "Accept" : 
"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain",

  "Accept-Language" : "cs,en-us;q=0.7,en;q=0.3",
  "Accept-Charset" : "utf-8",
  "Keep-Alive" : "300",
  "Connection" : "keep-alive"
];

/// Headers from firefox 3.6.13 on Linux
public enum string[string] LFFHeaders = [
  "User-Agent" : "Mozilla/5.0 (X11; U; Linux i686; cs; rv:1.9.2.3) 
Gecko/20100401 Firefox/3.6.13",
  "Accept" : 
"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain",

  "Accept-Language" : "cs,en-us;q=0.7,en;q=0.3",
  "Accept-Charset" : "utf-8",
  "Keep-Alive" : "300",
  "Connection" : "keep-alive"
];

Accept, Accept-Charset, Kepp-ALive and Connection are important and if 
you redefine it, module can stop work with some servers.


On 20.1.2012 15:56, Xan xan wrote:

On the other hand, I see dhttpclient  identifies as
  "Mozilla/5.0 (Windows; U; Windows NT 5.1; cs; rv:1.9.2.3)
Gecko/20100401 Firefox/3.6.13"

How can I change that?


Re: Reading web pages

2012-01-20 Thread Bystroushaak
This module is very simple, only for HTTP protocol, but there is way how 
to add HTTPS:


public void setTcpSocketCreator(TcpSocket function(string domain, ushort 
port) fn)


You can add lambda function which return SSL socket, which will be 
called for every connection.


FTP is not supported - it is DHTTPCLient, not DFTPClient :)

On 20.1.2012 15:24, Xan xan wrote:

For the other hand, how to specify the protocol? It's not the same
http://foo  thanftp://foo


Re: tdlp: higher-order functions

2012-01-20 Thread Alex Rønne Petersen

On 20-01-2012 15:32, Jerome BENOIT wrote:

Hello List:

In tDlp book in section 5.6 entitled `Higher-Order Functions. Function
Literals,
the first code example is:

-
T[] find(alias pred, T)(T[] input)
if (is(typeof(pred(input[0])) == bool)) {
for(; input.length > 0; input = input[1 .. $]) {
if (pred(input[0])) break;
}
return input;
}
-

I can play it. Nevertheless, it is not clear for me right now
what is the role played by `T' in the generic argument list (alias pred,
T).
(Its meaning is bypassed in this section via a `deduction' way.)

Any hint is welcome.

Thanks in advance,
Jerome



It's important to realize that D does not have generics; rather, it has 
templates. What (alias pred, T) means is that it takes (virtually) any 
argument as the first template parameter (function literals included) 
and a type for the second parameter.


You can call this function like so:

auto ints = find!((x) { return x % 2 != 0; })([1, 2, 3, 4, 5]);

Here, the type parameter T gets deduced from the argument, which is an 
array of ints.


--
- Alex


Re: Reading web pages

2012-01-20 Thread Xan xan
Yes. I ddi not know that I have to compile the two d files, although
it has sense ;-)

Perfect.

On the other hand, I see dhttpclient  identifies as
 "Mozilla/5.0 (Windows; U; Windows NT 5.1; cs; rv:1.9.2.3)
Gecko/20100401 Firefox/3.6.13"

How can I change that?




2012/1/20 Bystroushaak :
> With dmd 2.057 on my linux machine:
>
> bystrousak:DHTTPClient,0$ dmd spider.d dhttpclient.d
> bystrousak:DHTTPClient,0$ ./spider http://kitakitsune.org
> [Contingut:  "http://www.w3.org/TR/html4/loose.dtd";>
>
> 
> .
>
>
>
> On 20.1.2012 15:37, Xan xan wrote:
>>
>> I get errors:
>>
>> xan@gerret:~/yottium/@codi/aranya-d2.0$ gdmd-4.6 spider.d
>> spider.o: In function `_Dmain':
>> spider.d:(.text+0x4d): undefined reference to
>> `_D11dhttpclient10HTTPClient7__ClassZ'
>> spider.d:(.text+0x5a): undefined reference to
>> `_D11dhttpclient10HTTPClient6__ctorMFZC11dhttpclient10HTTPClient'
>> spider.o:(.data+0x24): undefined reference to
>> `_D11dhttpclient12__ModuleInfoZ'
>> collect2: ld returned 1 exit status
>>
>>
>> with the file spider.d:
>>
>> //D 2.0
>> //gdmd-4.6  =>  surt el fitxer amb el mateix nom i .o
>> //Usa https://github.com/Bystroushaak/DHTTPClient
>> import std.stdio, std.string, std.conv, std.stream;
>> import std.socket, std.socketstream;
>> import dhttpclient;
>>
>> int main(string [] args)
>> {
>>     if (args.length<  2) {
>>                writeln("Usage:");
>>                writeln("   ./spider {,, ...}");
>>                return 0;
>>        }
>>        else {
>>                try {
>>                        HTTPClient navegador = new HTTPClient();
>>                        foreach (a; args[1..$]) {
>>                                writeln("[Contingut: ", navegador.get(a),
>> "]");
>>                        }
>>                }
>>                catch (Exception e) {
>>                        writeln("[Excepció: ", e, "]");
>>                }
>>                return 0;
>>        }
>> }
>>
>>
>>
>> What happens now?
>>
>> Thanks a lot,
>> Xan.
>>
>> 2012/1/20 Bystroushaak:
>>>
>>> You can always use my module:
>>>  https://github.com/Bystroushaak/DHTTPClient
>>>
>>>
>


Re: Reading web pages

2012-01-20 Thread Bystroushaak

With dmd 2.057 on my linux machine:

bystrousak:DHTTPClient,0$ dmd spider.d dhttpclient.d
bystrousak:DHTTPClient,0$ ./spider http://kitakitsune.org
[Contingut: Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd";>



.


On 20.1.2012 15:37, Xan xan wrote:

I get errors:

xan@gerret:~/yottium/@codi/aranya-d2.0$ gdmd-4.6 spider.d
spider.o: In function `_Dmain':
spider.d:(.text+0x4d): undefined reference to
`_D11dhttpclient10HTTPClient7__ClassZ'
spider.d:(.text+0x5a): undefined reference to
`_D11dhttpclient10HTTPClient6__ctorMFZC11dhttpclient10HTTPClient'
spider.o:(.data+0x24): undefined reference to `_D11dhttpclient12__ModuleInfoZ'
collect2: ld returned 1 exit status


with the file spider.d:

//D 2.0
//gdmd-4.6  =>  surt el fitxer amb el mateix nom i .o
//Usa https://github.com/Bystroushaak/DHTTPClient
import std.stdio, std.string, std.conv, std.stream;
import std.socket, std.socketstream;
import dhttpclient;

int main(string [] args)
{
 if (args.length<  2) {
writeln("Usage:");
writeln("   ./spider {,, ...}");
return 0;
}
else {
try {
HTTPClient navegador = new HTTPClient();
foreach (a; args[1..$]) {
writeln("[Contingut: ", navegador.get(a), "]");
}
}
catch (Exception e) {
writeln("[Excepció: ", e, "]");
}
return 0;
}
}



What happens now?

Thanks a lot,
Xan.

2012/1/20 Bystroushaak:

You can always use my module:
  https://github.com/Bystroushaak/DHTTPClient




Re: Reading web pages

2012-01-20 Thread Xan xan
I get errors:

xan@gerret:~/yottium/@codi/aranya-d2.0$ gdmd-4.6 spider.d
spider.o: In function `_Dmain':
spider.d:(.text+0x4d): undefined reference to
`_D11dhttpclient10HTTPClient7__ClassZ'
spider.d:(.text+0x5a): undefined reference to
`_D11dhttpclient10HTTPClient6__ctorMFZC11dhttpclient10HTTPClient'
spider.o:(.data+0x24): undefined reference to `_D11dhttpclient12__ModuleInfoZ'
collect2: ld returned 1 exit status


with the file spider.d:

//D 2.0
//gdmd-4.6  => surt el fitxer amb el mateix nom i .o
//Usa https://github.com/Bystroushaak/DHTTPClient
import std.stdio, std.string, std.conv, std.stream;
import std.socket, std.socketstream;
import dhttpclient;

int main(string [] args)
{
if (args.length < 2) {
writeln("Usage:");
writeln("   ./spider {, , ...}");
return 0;
}
else {
try {
HTTPClient navegador = new HTTPClient();
foreach (a; args[1..$]) {
writeln("[Contingut: ", navegador.get(a), "]");
}
}
catch (Exception e) {
writeln("[Excepció: ", e, "]");
}
return 0;
}
}



What happens now?

Thanks a lot,
Xan.

2012/1/20 Bystroushaak :
> You can always use my module:
>  https://github.com/Bystroushaak/DHTTPClient
>
>


tdlp: higher-order functions

2012-01-20 Thread Jerome BENOIT

Hello List:

In tDlp book in section 5.6 entitled `Higher-Order Functions. Function Literals,
the first code example is:

-
T[] find(alias pred, T)(T[] input)
if (is(typeof(pred(input[0])) == bool)) {
for(; input.length > 0; input = input[1 .. $]) {
if (pred(input[0])) break;
}
return input;
}
-

I can play it. Nevertheless, it is not clear for me right now
what is the role played by `T' in the generic argument list (alias pred, T).
(Its meaning is bypassed in this section via a `deduction' way.)

Any hint is welcome.

Thanks in advance,
Jerome



Re: Reading web pages

2012-01-20 Thread Xan xan
Thanks for that. The standard library would include it. It will easy
the things high level, please.

For the other hand, how to specify the protocol? It's not the same
http://foo than ftp://foo

Thanks,
Xan.

2012/1/20 Bystroushaak :
> You can always use my module:
>  https://github.com/Bystroushaak/DHTTPClient
>
>
> On 19.1.2012 20:24, Timon Gehr wrote:
>>
>> On 01/19/2012 04:30 PM, Xan xan wrote:
>>>
>>> Hi,
>>>
>>> I want to simply code a script to get the url as string in D 2.0.
>>> I have this code:
>>>
>>> //D 2.0
>>> //gdmd-4.6
>>> import std.stdio, std.string, std.conv, std.stream;
>>> import std.socket, std.socketstream;
>>>
>>> int main(string [] args)
>>> {
>>> if (args.length< 2) {
>>> writeln("Usage:");
>>> writeln(" ./aranya {,, ...}");
>>> return 0;
>>> }
>>> else {
>>> foreach (a; args[1..$]) {
>>> Socket sock = new TcpSocket(new InternetAddress(a, 80));
>>> scope(exit) sock.close();
>>> Stream ss = new SocketStream(sock);
>>> ss.writeString("GET" ~ a ~ " HTTP/1.1\r\n");
>>> writeln(ss);
>>> }
>>> return 0;
>>> }
>>> }
>>>
>>>
>>> but when I use it, I receive:
>>> $ ./aranya http://www.google.com
>>> std.socket.AddressException@../../../src/libphobos/std/socket.d(697):
>>> Unable to resolve host 'http://www.google.com'
>>>
>>> What fails?
>>>
>>> Thanks in advance,
>>> Xan.
>>
>>
>> The protocol specification is part of the get request.
>>
>> ./aranaya www.google.com
>>
>> seems to actually connect to google. (it still does not work fully, I
>> get back 400 Bad Request, but maybe you can figure it out)


Re: Reading web pages

2012-01-20 Thread Xan xan
Nope:

xan@gerret:~/yottium/@codi/aranya-d2.0$ gdmd-4.6 aranya.d
xan@gerret:~/yottium/@codi/aranya-d2.0$ ./aranya www.google.com
std.socket.TcpSocket


What fails?

2012/1/19 Timon Gehr :
> On 01/19/2012 04:30 PM, Xan xan wrote:
>>
>> Hi,
>>
>> I want to simply code a script to get the url as string in D 2.0.
>> I have this code:
>>
>> //D 2.0
>> //gdmd-4.6
>> import std.stdio, std.string, std.conv, std.stream;
>> import std.socket, std.socketstream;
>>
>> int main(string [] args)
>> {
>>     if (args.length<  2) {
>>                writeln("Usage:");
>>                writeln("   ./aranya {,, ...}");
>>                return 0;
>>        }
>>        else {
>>                foreach (a; args[1..$]) {
>>                        Socket sock = new TcpSocket(new InternetAddress(a,
>> 80));
>>                        scope(exit) sock.close();
>>                        Stream ss = new SocketStream(sock);
>>                        ss.writeString("GET" ~ a ~ " HTTP/1.1\r\n");
>>                        writeln(ss);
>>                }
>>                return 0;
>>        }
>> }
>>
>>
>> but when I use it, I receive:
>> $ ./aranya http://www.google.com
>> std.socket.AddressException@../../../src/libphobos/std/socket.d(697):
>> Unable to resolve host 'http://www.google.com'
>>
>> What fails?
>>
>> Thanks in advance,
>> Xan.
>
>
> The protocol specification is part of the get request.
>
> ./aranaya www.google.com
>
> seems to actually connect to google. (it still does not work fully, I get
> back 400 Bad Request, but maybe you can figure it out)


Passing arguments to a new thread

2012-01-20 Thread Mars

Hello everybody.
As the title states, I want to run a function in a new thread, 
and pass it some arguments. How would I do that? I guess I could 
make a class, deriving from Thread, and work with it, but out of 
curiosity, I'd like to know if it's possible with a simple 
function.


Mars


Re: sameness

2012-01-20 Thread bearophile
sclytrack:

>   letters are different yet the same
> ...
>   letters are different

It's a Zen thing.
Templates are very strict in the type you give them, while function arguments 
perform some silent type conversions. I think this will not change, because 
it's hard to design C++/D-style templates in a different way. In C++ happens 
something similar.

Bye,
bearophile


sameness

2012-01-20 Thread sclytrack


---
letters are different yet the same

immutable(char) [] letter1;
const(char) [] letter2;
char [] letter3;

void proc1( const(char) [] letter) {}

---
letters are different

struct Container(T)
{
T letter;
}

Container!(const(char)) letter1;
Container!(immutable(char)) letter2
Container!(char) letter3;

void proc2(Container!(const(char)) letter) {}

---


Strict aliasing in D

2012-01-20 Thread Denis Shelomovskij

Is there a strict aliasing rule in D?

I just saw https://bitbucket.org/goshawk/gdc/changeset/b44331053062