Discussion:
Mapi function
Rupert Bates
2009-05-19 09:19:37 UTC
Permalink
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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to mochikit+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---
Morten Barklund
2009-05-19 13:38:24 UTC
Permalink
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
Post by Rupert Bates
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



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to mochikit+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---
Bob Ippolito
2009-05-19 15:13:09 UTC
Permalink
Typically this is done with count or cycle and izip.

izip(cycle(["odd", "even"]), someArray)
Post by Rupert Bates
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to mochikit+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---
Rupert Bates
2009-05-20 14:32:27 UTC
Permalink
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
Post by Bob Ippolito
Typically this is done with count or cycle and izip.
izip(cycle(["odd", "even"]), someArray)
Post by Rupert Bates
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to mochikit+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---
Bob Ippolito
2009-05-20 15:53:13 UTC
Permalink
Did you read the sentence that I wrote?

izip(count(), someArray) does the indexes.
Post by Rupert Bates
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
Post by Bob Ippolito
Typically this is done with count or cycle and izip.
izip(cycle(["odd", "even"]), someArray)
Post by Rupert Bates
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "MochiKit" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to mochikit+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/mochikit?hl=en
-~----------~----~----~----~------~----~------~--~---

Loading...