www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - tdlp: higher-order functions

reply Jerome BENOIT <g6299304p rezozer.net> writes:
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
Jan 20 2012
parent reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
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
Jan 20 2012
next sibling parent Jerome BENOIT <g6299304p rezozer.net> writes:
Thanks.

Let go further.


On 20/01/12 15:58, Alex R=F8nne 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])) =3D=3D bool)) {
 for(; input.length > 0; input =3D 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 pre=
d,
 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 a= rgument as the first template parameter (function literals included) and = a type for the second parameter.
 You can call this function like so:

 auto ints =3D find!((x) { return x % 2 !=3D 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
Jan 20 2012
prev sibling parent reply Jerome BENOIT <g6299304p rezozer.net> writes:
Hello Again:

On 20/01/12 15:58, Alex R=F8nne 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])) =3D=3D bool)) {
 for(; input.length > 0; input =3D 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 pre=
d,
 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 a= rgument as the first template parameter (function literals included) and = a type for the second parameter.
 You can call this function like so:

 auto ints =3D find!((x) { return x % 2 !=3D 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 ge= t the point before to gig further D: In (alias pred, T), are both `pred' and `T' parameters for the function f= ind ? or is `T' some kind of parameter for `pred' ? Thanks in advance, Jerome =20

Jan 20 2012
parent reply =?ISO-8859-1?Q?Alex_R=F8nne_Petersen?= <xtzgzorex gmail.com> writes:
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
Jan 20 2012
parent reply Jerome BENOIT <g6299304p rezozer.net> writes:
On 20/01/12 17:23, Alex R=F8nne Petersen wrote:
 On 20-01-2012 17:14, Jerome BENOIT wrote:
 Hello Again:

 On 20/01/12 15:58, Alex R=F8nne Petersen wrote:
 On 20-01-2012 15:32, Jerome BENOIT wrote:
 Hello List:

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

 -----------------------------------------------------------------
 T[] find(alias pred, T)(T[] input)
 if (is(typeof(pred(input[0])) =3D=3D bool)) {
 for(; input.length > 0; input =3D 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 p=
red,
 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 =3D find!((x) { return x % 2 !=3D 0; })([1, 2, 3, 4, 5]);

 Here, the type parameter T gets deduced from the argument, which is a=
n
 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 functio=
n
 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 =3D find!((x) { return x % 2 !=3D 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 f=
unction literal and the type int is the template parameter list. The seco= nd 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 =3D find!((x, m) { return x % m !=3D 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

Jan 20 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Jan 20 2012
parent Tobias Pankrath <tobias pankrath.net> writes:
 
 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.
Jan 20 2012