D templates + IFTI + Tuples + delegate literals = holy bovine!

Daniel Keep daniel.keep.lists at gmail.com
Mon Feb 26 13:00:22 PST 2007



Vassily Gavrilyak wrote:
> Cool stuff, almost what I searched for. filter works like a charm.
> However not so for map and reduce. The return type of map and reduce is different
> from the type of item in collection.
> I'm just started to look at D, so don't know the right way to write those functions.
> Declaration in Nemerle looks like this
> public Map['a, 'b] (l : list ['a], f : 'a -> 'b) : list ['b] {
>       $[ f(x) | x in l ]
> }
> So we have 2 types here, type of collection and type of result
> And reduce(fold) has 2 types too.
> So basically the following code:
> 
> struct Person{   int id;  char[] name;}
> static Person[] people= [{1, "Joe"}, {2, "Bill"}];
> int[] ids = map(persons, delegate int(Person p){return p.id;});
> 
> should produce [1,2]
> 
> Is that possible in D?

Wow; it's been a while :P  I think this is the first reply I got to that
post.

Having a quick squizz at the code; you're right.  The way I've derived
the return type of map is completely wrong.  I'll put that on my list of
things to fix :P

So in answer to your question, yes it should be entirely possible.  The
trick, in this case, is the following:

  static if( is( tOp tReturnType == return ) )

Which will derive the return type of the function.

  -- Daniel

Incidentally, Nemerle is looking less and less like C# every time I see
it :P

-- 
Unlike Knuth, I have neither proven or tried the above; it may not even
make sense.

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list