Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #2086 (closed enhancement: fixed)

Opened 13 years ago

Last modified 13 years ago

[patch] Add map() and filter() to tango.core.Array

Reported by: llucax Assigned to: community
Priority: normal Milestone: 1.0
Component: Tango Version: 0.99.9 Kai
Keywords: Cc: leandro.lucarella@sociomantic.com

Description

I think map() and filter() are one of the most basic functions for arrays, so they deserve a place in tango.core.Array.

Attachments

0001-core.Array-Add-map-and-filter-functions.patch (5.3 kB) - added by llucax on 10/18/11 17:37:41.
0001-Fix-documentation.patch (0.9 kB) - added by llucax on 10/18/11 17:40:55.
Small typo fix ;)

Change History

10/18/11 17:37:41 changed by llucax

  • attachment 0001-core.Array-Add-map-and-filter-functions.patch added.

10/18/11 17:40:55 changed by llucax

  • attachment 0001-Fix-documentation.patch added.

Small typo fix ;)

10/19/11 09:58:15 changed by llucax

BTW, I kept the double template thing because it was there, but I don't understand really its purpose. Also, I noticed that using the double template the functions can't accept static arrays as arguments and not using the double template thing make it work. So, is there any good reason to do that?

10/19/11 16:40:57 changed by doob

Shouldn't these functions take an optional buffer as its last argument, like many of the other array functions in Tango do.

10/20/11 11:24:42 changed by llucax

Doesn't look like the case for tango.core.Array (for example intersectionOf(), missingFrom(), unionOf()). I think just for the sake of consistency in the module and simplicity of implementation I wouldn't add an optional buffer to these functions either. If that option want to be added in the future, all functions in this module can be modified together to keep consistency.

10/24/11 11:06:27 changed by llucax

I could only come up with this implementation of map() to allow the function to return a different type than it takes:

        typeof(Fun(ElemTypeOf!(Buf)))[] map( alias Fun, Buf )( Buf buf )
        {
            typeof(Fun(ElemTypeOf!(Buf)))[] r;
            r.length = buf.length;
            for( size_t pos = 0, len = buf.length; pos < len; ++pos )
            {
                r[pos] = Fun( buf[pos] );
            }
            return r;
        }   

The usage though it's pretty different from the other functions in tango.core.Array, as the delegate has to be passed as a template argument: map!(dg)(collection);

Also this lacks the "double template" that I don't really know what's good for.

Anyway, comments and suggestions are welcome.

10/24/11 16:05:01 changed by dhasenan

The following seems to work:

template map(TOut, TIn)
{
 TOut[] map(TOut function(TIn) func, TIn[] array)
 {
    TOut[] arr;
    arr.length = array.length;
    foreach (i, a; array) arr[i] = func(a);
    return arr;
 }
}

auto dg = function(int i) { return [cast(char)('0' + i + 1)] };
Stdout.formatln("{}", map(dg, [1, 2, 3, 4])); // [2, 3, 4, 5]

I'm not sure whether we want the function before the array, but I'd generally say "map a function onto an array", so...

Anyway, I'll take care of this today. Yell at me if it isn't done when you wake up tomorrow.

10/24/11 16:10:49 changed by llucax

I think a weakness of that implementation is that the input has to be an array, but in my version it could be anything with indexing support and the callable has to be a function (not even a delegate can be used with your version I think), and in my version it could be any callable object (which I think is more in sync with the other functions in the module).

10/25/11 03:47:47 changed by dhasenan

Check out the implementation I committed. It addresses your concerns.

There are at least some functions in that module that assume arrays. They should be fixed eventually.

10/25/11 12:34:31 changed by llucax

OK, looks good to me, but I would reorder the arguments of map() and reduce() to take the array argument first, as mostly all other functions in tango.core.Array do.

Thanks.

10/25/11 16:30:35 changed by dhasenan

  • status changed from new to closed.
  • resolution set to fixed.