Function Repository Resource:

# AllSameBy

Check if all the elements of a list give the same value after applying a function

Contributed by: Sander Huisman
 ResourceFunction["AllSameBy"][list,f] gives True when all elements of the list returns the same value when f is applied, and False otherwise.

## Details

ResourceFunction["AllSameBy"] applies f sequentially from the first element to the last until the result is different from the ones before, i.e. it short-circuits whenever possible.
When list has length 0 or 1, True is automatically returned without f being applied.

## Examples

### Basic Examples (1)

Test whether all numbers have the same parity:

 In[1]:=
 Out[1]=

### Scope (1)

Test if all the numbers have the same magnitude:

 In[2]:=
 Out[2]=

### Applications (1)

Check if numbers are all prime or all composite:

 In[3]:=
 Out[3]=

### Properties and Relations (4)

AllSameBy can be implemented by applying f to all elements and then checking whether the results are all the same:

 In[4]:=
 Out[4]=

Compare with the original function:

 In[5]:=
 Out[5]=

The difference becomes apparent when there are side effects:

 In[6]:=
 Out[6]=

Compared to the original function, the last items are untouched:

 In[7]:=
 Out[7]=

SHuisman

## Version History

• 1.0.0 – 10 May 2021