Re: A tutorial on D templates

2012-01-14 Thread Philippe Sigaud
 On 13/01/12 10:48 PM, DNewbie wrote:
  I can't understand it. Why would someone need template programming. What 
  problem does template solve?

Well read on and see :-)

Peter:
 Suppose you want to write a function to get the minimum of two integers.
 It's easy:

 Oh.. I see.
 Thank you everybody.

And that's the first, more visible part of templates, a bit like
generics in Java.
Template are incomplete pieces of code with left-empty 'slots' which
you can fill at compile-time to decide what code will be compiled.
You can generate entire functions / class hierarchies or nicely
crafted-for-your-need code / pre-computed-by-the-compiler code with
templates.

Philippe


Re: A tutorial on D templates

2012-01-14 Thread Philippe Sigaud
On Sat, Jan 14, 2012 at 01:08, Ali Çehreli acehr...@yahoo.com wrote:

 Here is another resource that tries to answer that question:

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

 Parts of the source code may be left to the compiler to be filled in until
 that part is actually used in the program.

Hi Ali, I discovered you had a chapter on templates just a few days
ago. I'll go and read it. I think I'll add a ressources/further
reading part in my doc and put a link to your chapter.

Philippe


Re: A tutorial on D templates

2012-01-14 Thread Ali Çehreli

On 01/14/2012 12:11 AM, Philippe Sigaud wrote:
 On Sat, Jan 14, 2012 at 01:08, Ali Çehreliacehr...@yahoo.com  wrote:

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

 Hi Ali, I discovered you had a chapter on templates just a few days
 ago.

That chapter is intentionally incomplete. I think function, struct, and 
class templates and their uses with type template parameters are the 
most common. (That's a C++ programmer talking. ;)) I've left the rest of 
templates to a later chapter.


 I'll go and read it.

Thank you. Please ignore the Inglish ;) mistakes for now. It is 
constantly being corrected by my editor (Ergin Güney) and I.


 I think I'll add a ressources/further
 reading part in my doc and put a link to your chapter.

Thank you. I will do the same. It will be easier if it gets a permanent 
home, in addition to its github page ( 
https://github.com/PhilippeSigaud/D-templates-tutorial ).



 Philippe

Ali



Re: A tutorial on D templates

2012-01-14 Thread Philippe Sigaud
On Sat, Jan 14, 2012 at 15:56, Ali Çehreli acehr...@yahoo.com wrote:
 On 01/14/2012 12:11 AM, Philippe Sigaud wrote:
 On Sat, Jan 14, 2012 at 01:08, Ali Çehreliacehr...@yahoo.com  wrote:

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


 Hi Ali, I discovered you had a chapter on templates just a few days
 ago.

 That chapter is intentionally incomplete. I think function, struct, and
 class templates and their uses with type template parameters are the most
 common. (That's a C++ programmer talking. ;)) I've left the rest of
 templates to a later chapter.

I agree. Your goal is not exactly the same as mine: you intend to give
a view on the entire language, for beginners, whereas I intend my doc
to be a deep plunge into templates, as complete as possible.



 I'll go and read it.

 Thank you. Please ignore the Inglish ;) mistakes for now. It is constantly
 being corrected by my editor (Ergin Güney) and I.

What I see is quite readable :-)


 I think I'll add a ressources/further
 reading part in my doc and put a link to your chapter.

 Thank you. I will do the same.

That's cool, because the D community is small enough and still
dispersed enough that we should link one another to bring it all
together.

 It will be easier if it gets a permanent
 home, in addition to its github page (
 https://github.com/PhilippeSigaud/D-templates-tutorial ).

I have no permanent home for my D projects, nor do I intend to do. You
can link to the Github project, that's what people do. Or directly to
the pdf, if you wish.


Re: A tutorial on D templates

2012-01-14 Thread DNewbie
On Sat, Jan 14, 2012, at 09:07 AM, Philippe Sigaud wrote:
  On 13/01/12 10:48 PM, DNewbie wrote:
   I can't understand it. Why would someone need template programming. What 
   problem does template solve?
 
 Well read on and see :-)
 
 Peter:
  Suppose you want to write a function to get the minimum of two integers.
  It's easy:
 
  Oh.. I see.
  Thank you everybody.
 
 And that's the first, more visible part of templates, a bit like
 generics in Java.
 Template are incomplete pieces of code with left-empty 'slots' which
 you can fill at compile-time to decide what code will be compiled.
 You can generate entire functions / class hierarchies or nicely
 crafted-for-your-need code / pre-computed-by-the-compiler code with
 templates.
 
 Philippe
 

OK... I won't play with templates for now, but please keep up the good work.


Re: import std.c.windows.windows;

2012-01-14 Thread DNewbie


On Wed, Jan 11, 2012, at 01:16 PM, Mike Parker wrote:
 On 1/10/2012 10:57 PM, DNewbie wrote:
  On Tue, Jan 10, 2012, at 10:37 PM, Mike Parker wrote:
 
  Those samples use a binding to the Win32 API[1] that does not ship with
  DMD. So in your case, std.c.windows.windows is probably what you want
  for now.
 
 
  [1]http://dsource.org/projects/bindings/wiki/WindowsApi
 
 
  Ok.. I still don't understand why such a 'binding' is needed.
  Is std.c.windows.windows not enough for everyone?
 
 
 Unfortunately, no. It is not a complete binding of the Win32 API. It has 
 a lot of stuff, but if you need more you need to look elsewhere. Andrej 
 hit on some of the deficiencies in his post.
 

Understood. I think I'll go with the binding. Thnk you.


Constant function/delegate literal

2012-01-14 Thread Vladimir Matveev
Hi,

Is there a reason why I cannot compile the following code:

module test;

struct Test {
int delegate(int) f;
}

Test s = Test((int x) { return x + 1; });

void main(string[] args) {
return;
}

dmd 2.057 says:

test.d(7): Error: non-constant expression cast(int delegate(int))delegate pure
nothrow @safe int(int x)
{
return x + 1;
}

?

This is simple example; what I want to do is to create a global variable
containing a structure with some ad-hoc defined functions. The compiler
complains that it cannot evaluate  at compile time. I think this could
be solved by defining a function returning needed structure, but I think this
is cumbersome and inconvenient.

Best regards,
Vladimir Matveev.


Re: A tutorial on D templates

2012-01-14 Thread Joel

Good work Philippe, looks good!



Re: Absolute beginner

2012-01-14 Thread Matej Nanut
I've never noticed std.conv.parse takes a radix argument, silly me.
And will take
a look at readf from std.stream, definitely.

Thanks!

On 14 January 2012 01:20, Justin Whear jus...@economicmodeling.com wrote:
 On Fri, 13 Jan 2012 23:05:19 +0100, Matej Nanut wrote:

 While we're at it: what's the best way to parse in a formatted manner?
 For example, if I want to get 5 hexadecimal digits converted into an
 uint? And I want to simultaneously advance the string?

 sscanf seems fiddly and unsafe.


 Check out readf: http://d-programming-language.org/phobos/
 std_stream.html#readf

 You'll need to wrap the string in a Stream interface (MemoryStream?), but
 since you're looking for advance-on-read, you want stream semantics
 anyhow.

 Justin


Re: Constant function/delegate literal

2012-01-14 Thread Andrej Mitrovic
I guess these are CTFE (compile-time function evaluation) issues,
someone else might know more. A workaround is to use a module
constructor which will run before main():

struct Test {
   int delegate(int) f;
}

Test s;

static this() {
s = Test((int x) { return x + 1; });
}

Note that 's' is thread-local, to make it shared across threads but
without implicit synchronization you can use:

__gshared Test s;

shared static this() {
s = Test((int x) { return x + 1; });
}

Note the use of *shared* in the module ctor. Without 'shared' it would
run every time a new thread was spawned.


Re: Fixed matrix rows joining

2012-01-14 Thread Andrej Mitrovic
A rectangular array is really just one array, is it not? From a syntax
point it looks like a multidimensional array but really it's just a
single linear piece of memory, so just cast it:

void main()
{
int[2][4] table;
table[0][] = 0;
table[1][] = 1;
table[2][] = 2;
table[3][] = 3;
auto x = cast(int[])table;
assert(x == [0, 0, 1, 1, 2, 2, 3, 3]);
}


Re: Fixed matrix rows joining

2012-01-14 Thread Andrej Mitrovic
I guess join() could be specialized for static arrays and then just do
a dup and a cast? Would that work ok?


Re: std.algorithm.startsWith with maximal matching

2012-01-14 Thread H. S. Teoh
On Fri, Jan 13, 2012 at 09:30:35PM -0800, Jonathan M Davis wrote:
 On Friday, January 13, 2012 18:47:19 H. S. Teoh wrote:
[...]
  But what I really want to accomplish is to parse a string containing
  multiple words; at each point I have a list of permitted words that
  need to be matched against the string; substring matches don't
  count. I already have a way of skipping over spaces; so for medial
  words, I can simulate this by appending a space to the end of the
  word list passed to startsWith(). However, this doesn't work when
  the word being matched is at the very end of the string, or if it is
  followed by punctuation.
  
  Is there another library function that can do this, or do I just
  have to roll my own?
 
 Use std.array.split. It will split a string into an array of strings
 using whitespace as the delimiter.
[...]

What about punctuation?


T

-- 
Don't modify spaghetti code unless you can eat the consequences.


Re: std.algorithm.startsWith with maximal matching

2012-01-14 Thread Jonathan M Davis
On Saturday, January 14, 2012 19:13:02 H. S. Teoh wrote:
 On Fri, Jan 13, 2012 at 09:30:35PM -0800, Jonathan M Davis wrote:
  On Friday, January 13, 2012 18:47:19 H. S. Teoh wrote:
 [...]
 
   But what I really want to accomplish is to parse a string containing
   multiple words; at each point I have a list of permitted words that
   need to be matched against the string; substring matches don't
   count. I already have a way of skipping over spaces; so for medial
   words, I can simulate this by appending a space to the end of the
   word list passed to startsWith(). However, this doesn't work when
   the word being matched is at the very end of the string, or if it is
   followed by punctuation.
   
   Is there another library function that can do this, or do I just
   have to roll my own?
  
  Use std.array.split. It will split a string into an array of strings
  using whitespace as the delimiter.
 
 [...]
 
 What about punctuation?

If you have to worry about punctuation, then == isn't going to work. You'll 
need to use some other combination of functions to strip the punctuation from 
one or both ends of the word. One possible solution would be something like

foreach(word; splitter!(std.uni.isWhite)(str))
{
auto found = find!(not!(std.uni.isPunctuation))(word);
if(found.startsWith(listOfWords))
{
//...
}
}

- Jonathan M Davis


Re: std.algorithm.startsWith with maximal matching

2012-01-14 Thread Jonathan M Davis
On Saturday, January 14, 2012 19:45:55 Jonathan M Davis wrote:
 If you have to worry about punctuation, then == isn't going to work. You'll
 need to use some other combination of functions to strip the punctuation
 from one or both ends of the word. One possible solution would be something
 like
 
 foreach(word; splitter!(std.uni.isWhite)(str))
 {
 auto found = find!(not!(std.uni.isPunctuation))(word);
 if(found.startsWith(listOfWords))
 {
 //...
 }
 }

Actually, if the word has to match exactly, then startsWith isn't going to cut 
it. What you need to do is outright strip the punctuation from both ends. 
You'd need something more like

word = find!(not!(std.uni.isPunctuation))(word);
word = array(until!(std.uni.isPunctuation)(word));

if(canFind(wordList, word))
{
//...
}

- Jonathan M Davis