Function Repository Resource:

MapSelectCounted

Source Notebook

Map a function over selected elements of a list, also passing in a running count of such elements

Contributed by: Michael Sollami

ResourceFunction["MapSelectCounted"][f,list,test]

returns a list of f[xi,j] where j is a running total incremented when test[xi] is True.

ResourceFunction["MapSelectCounted"][f,list]

returns a list of f[xi,i] for all elements xiin list.

Details and Options

Oftentimes you only need the first part of the second slot of MapIndexed

Examples

Basic Examples (2) 

Map while keeping the count:

In[1]:=
ResourceFunction["MapSelectCounted"][Subscript[#1, #2] &, Alphabet[]]
Out[1]=

MapSelectCounted over a list conditionally:

In[2]:=
ResourceFunction["MapSelectCounted"][Subscript[#1, #2] &, Range[100], PrimeQ]
Out[2]=

Properties and Relations (1) 

Oftentimes you only need the first part of the second slot of MapIndexed

In[3]:=
lst = {1, 2, 3, 5, 6, 11, 18};
{(f |-> Labeled[
      TableForm[f[{#1, FullForm@#2} &, lst], TableHeadings -> {{}, {"Slot 1", "Slot 2"}}], Text@Style[f, Bold], Top]) /@ {ResourceFunction[
    "MapSelectCounted"], MapIndexed}} // TextGrid
Out[4]=

Possible Issues (1) 

MapSelectCounted only works at the first level, use MapIndexed to specify deeper levels than the first:

In[5]:=
MapIndexed[f, {{"a"}, {"b"}}, {2}]
Out[5]=
In[6]:=
ResourceFunction["MapSelectCounted"][f, {{"a"}, {"b"}}]
Out[6]=

Publisher

Michael Sollami

Version History

  • 1.0.0 – 14 February 2020

Related Resources

License Information