Functional tools for D
Daniel Keep
daniel.keep.lists at gmail.com
Wed Feb 28 16:50:40 PST 2007
map should now be fixed to return the actual type, and I've uploaded the
file to Wiki4D. I actually couldn't find the source for this, so when I
downloaded it off the DM news archive (Thunderbird doesn't have my
original message :P) I had to go rebuild all the indenting. Fun, fun, fun!
http://www.prowiki.org/wiki4d/wiki.cgi?DanielKeep/functools
Your original example should look like this:
auto ids = map(persons, (Person p) { return p.id; });
Have fun, and hope it's all actually working this time :P
-- Daniel
Daniel Keep wrote:
>
> 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