Mapi function

0 views
Skip to first unread message

Rupert Bates

unread,
May 19, 2009, 5:19:37 AM5/19/09
to MochiKit
Hi there, how about adding a mapi function to Mochikit.Iter which
passes the index of the item being processed to the specified
function? It would be really handy, for instance for specifying
altenate rows on a table.
Rupert

Morten Barklund

unread,
May 19, 2009, 9:38:24 AM5/19/09
to rupert...@gmail.com, moch...@googlegroups.com
Hi Rupert,

You can do that using the MochiKit.Base.items() function:

var foo=[{b:1},{b:2},{b:3}];
var indexed_foo = items(foo); // an array like
[[0,{b:1}],[1,{b:2}],[2,{b:3}]];

Then you can map over this using arg[0] and arg[1] as index and value
respectively.

:)

Regards,
B
--
Morten Barklund


Bob Ippolito

unread,
May 19, 2009, 11:13:09 AM5/19/09
to Rupert Bates, MochiKit
Typically this is done with count or cycle and izip.

izip(cycle(["odd", "even"]), someArray)

Rupert Bates

unread,
May 20, 2009, 10:32:27 AM5/20/09
to MochiKit
Thanks for the reply, so that answers the alternate rows case, but
there are plenty of other cases where you would want to know the index
of the item. The items() solution feels like a bit of a workaround and
presumably has performance implications. Would a mapi function not be
more efficient and elegant? Sorry if I'm missing something.
Rupert

On May 19, 4:13 pm, Bob Ippolito <b...@redivi.com> wrote:
> Typically this is done with count or cycle and izip.
>
> izip(cycle(["odd", "even"]), someArray)
>

Bob Ippolito

unread,
May 20, 2009, 11:53:13 AM5/20/09
to Rupert Bates, MochiKit
Did you read the sentence that I wrote?

izip(count(), someArray) does the indexes.
Reply all
Reply to author
Forward
0 new messages