www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - A tutorial on D templates: updates

reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
Hello,

I posted there a few weeks ago about a tutorial on D templates I put in github:

https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/dtemplates.pdf

Since then, I received numerous mails, issues, advices and thanks. Thank to you
all!

Following the ideas found in TDPL, I wrote a D script to extract and test all
the samples
presented in the document. I'm proud to say that right now, all named (module
XXX;) samples
compile, which makes for more than 200 modules tested! Indeed, you could see
the entire document
as a huge package documentation :)

I also added explanations, new sections and a new appendix in D templates
resources.

As before, do not hesitate to read, comment, post and even send pull requests,
I'm all ears.

Bye,

Philippe
Jan 29 2012
next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 1/29/12 2:44 PM, Philippe Sigaud wrote:
 Hello,

 I posted there a few weeks ago about a tutorial on D templates I put in github:

 https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/dtemplates.pdf

 Since then, I received numerous mails, issues, advices and thanks. Thank to
you all!

 Following the ideas found in TDPL, I wrote a D script to extract and test all
the samples
 presented in the document. I'm proud to say that right now, all named (module
XXX;) samples
 compile, which makes for more than 200 modules tested! Indeed, you could see
the entire document
 as a huge package documentation :)

 I also added explanations, new sections and a new appendix in D templates
resources.

 As before, do not hesitate to read, comment, post and even send pull requests,
I'm all ears.

 Bye,

 Philippe
Great! Just give me the green light when you feel this is reddit-publishable. Andrei
Jan 29 2012
parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Mon, Jan 30, 2012 at 06:16, Andrei Alexandrescu
<SeeWebsiteForEmail erdani.org> wrote:
 Great! Just give me the green light when you feel this is
 reddit-publishable.
That should be OK now, thanks.
Jan 29 2012
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 1/29/12 11:56 PM, Philippe Sigaud wrote:
 On Mon, Jan 30, 2012 at 06:16, Andrei Alexandrescu
 <SeeWebsiteForEmail erdani.org>  wrote:
 Great! Just give me the green light when you feel this is
 reddit-publishable.
That should be OK now, thanks.
Let it ride! http://www.reddit.com/r/programming/comments/p2qfd/d_templates_a_tutorial_pdfgoogle_preview/ Andrei
Jan 29 2012
prev sibling next sibling parent "Mattbeui" <matheus_nab hotmail.com> writes:
On Sunday, 29 January 2012 at 20:44:00 UTC, Philippe Sigaud wrote:
 Hello,

 I posted there a few weeks ago about a tutorial on D templates 
 I put in github:

 https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/dtemplates.pdf
 .
 .
 .
 As before, do not hesitate to read, comment, post and even send 
 pull requests, I'm all ears.

 Bye,

 Philippe
Neat. I will start reading tonight. Anything else I'll wrote here.
Jan 30 2012
prev sibling parent reply bls <bizprac orange.fr> writes:
On 01/29/2012 12:44 PM, Philippe Sigaud wrote:
 Hello,

 I posted there a few weeks ago about a tutorial on D templates I put in github:

 https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/dtemplates.pdf

 Since then, I received numerous mails, issues, advices and thanks. Thank to
you all!

 Following the ideas found in TDPL, I wrote a D script to extract and test all
the samples
 presented in the document. I'm proud to say that right now, all named (module
XXX;) samples
 compile, which makes for more than 200 modules tested! Indeed, you could see
the entire document
 as a huge package documentation :)

 I also added explanations, new sections and a new appendix in D templates
resources.

 As before, do not hesitate to read, comment, post and even send pull requests,
I'm all ears.

 Bye,

 Philippe
First of all thank you so much for this wonderful book! I would like to ask for a little enhancement regarding mixin templates. see snippet. The second mixin template "RandomAccessList" is slightly OT 'cause Ranges are also touched, but I think this could be very interesting stuff. A third mixin template example which comes in mind is the Publisher subscriber pattern template. snip. import std.stdio; import std.cstream; void main(string[] args) { // showing mixin template scope. auto bar = new Bar(); writeln(bar.onClick()); din.getc(); } mixin template FooMixin() { void init() { } string onClick() { return "Clicked"; } } class Foo { mixin FooMixin; } class Bar : Foo { } // Something more interesting : Mixin templates and ranges mixin template RandomAccessList() { alias typeof(this) Me; //Either static private Me[] MeArray; //OR private Me next, prev; } interface IRandomAccessRange {} class Contacts : IRandomAccessRange // Contacts not Contact ! { mixin RandomAccessList; }
Jan 30 2012
parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Mon, Jan 30, 2012 at 13:07, bls <bizprac orange.fr> wrote:
 First of all thank you so much for this wonderful book!
Thanks!
 I would like to ask for a little enhancement regarding mixin templates. see
 snippet.
Yes, this section is still bit short. But, what enhancement are you talking about? The FooMixin template?
 The second mixin template "RandomAccessList" is slightly OT 'cause Ranges
 are also touched, but I think this could be very interesting stuff.
Yes indeed. Can I ask you for a small explanatory text? (And your real name, if you want to be cited under it)
 A third mixin template example which comes in mind is the Publisher
 subscriber pattern template.
I'll add it also. Again, I'd welcome any explanatory text.
Jan 30 2012
parent reply bls <bizprac orange.fr> writes:
Hi Philippe,
Ok, something more interesting it combines suggestion 2)  and 3). Still 
a quick hack, not much tested, but I think the intention is clear.

The snippets show how a publisher subscriber pattern can be mixed in. 
Further it shows how a simple class could become a stack, queue, list etc.
The FooMixin was just to show (due to a question on D.Learn), how we can 


import std.stdio;
import std.cstream;
import std.functional;

void main(string[] args)
{
	auto  p = new PersonStack();
	
	p.add("Hans", 42);// uses MIStack push(), MISubScriber notify()
	din.getc();
}

mixin template MIPublisher()
{
	alias void delegate(Object sender, string event) CallBack;
	CallBack[] callBacks;

	public void register(CallBack callBack)
	{
		callBacks ~= callBack;
	}

	// There is for sure a smarter solution to remove
	public void unRegister(CallBack callBack)
	{
		for ( int i=0 ; !i<callBacks.length ; i++ )
		{
			if (callBacks[i] == callBack)
			{
				callBacks = callBacks[0..i] ~ callBacks[i+1..callBacks.length];
				--i;
			}
		}
	}
	// Notify ALL Subscribers
	public void notify(string evt)
	{
		
		foreach ( CallBack callBack ; callBacks )
		{
			callBack( this, evt );
		}
	}
}

mixin template MIStack()
{
	alias typeof(this) Me;
	//alias Me[] Us;
	static Me[] stack;
	
	
	public:
	
	bool empty()
	{
		return stack.length == 0;
	}
	
	int count()
	{
		return stack.length;
	}
	
	void push(Me element)
	{
		stack ~= element;
	}
	
	Me pop()
	{
		Me element = peek();
		stack.length = stack.length - 1;
		return element;
	}
	
	Me peek()
	{
		if ( stack.length == 0 )
		{
			// throw error
		}
		Me element = stack[stack.length-1];
		return element;
	}
}


	
class PersonStack
{
	private string name;
	private int age;
	
	// Our Mixins
	mixin MIStack;
	mixin MIPublisher;
		
	this()
	{
		// Register some subscribers (MIPublisher register function)
		// I have used free functions to show the useful toDelegate()
		register( toDelegate(&DrawPersonBarChart) ); 	
		register( toDelegate(&DrawPersonPieChart) ); 	
	}
	
	// Push
	void add(string name, int age)
	{
		this.name = name;
		this.age = age;
		// Push Person (MIStack push function.)
		push(this);
		
		// Notify all subscribers		
		notify("Push");
			
	}
	// remove()
}

//Subscriber free functions
void DrawPersonBarChart(Object sender, string msg)
{
	writeln("Bar " ~ msg);
}
void DrawPersonPieChart(Object sender, string msg)
{
	writeln("Pie " ~ msg);
}
Jan 30 2012
parent bls <bizprac orange.fr> writes:
On 01/30/2012 01:14 PM, bls wrote:
 Hi Philippe,
slightly better .. class PersonStack { private string name; private int age; // Our Mixins mixin MIStack; mixin MIPublisher; this() { // Register some subscribers (MIPublisher register function) // I have used free functions to show the useful toDelegate() register( toDelegate(&DrawPersonBarChart) ); register( toDelegate(&DrawPersonPieChart) ); } // Push void add(string name, int age) { auto p = new PersonStack(); p.name = name; p.age = age; // Add Person (MIStack push function.) push(p); // Notify all subscribers notify("Push"); } void remove() { auto el = pop(); notify("Pop " ~ el.name ); } }
Jan 30 2012