Oh my... Mertz, listen to me, you don't need a parameter. You only need a key 
function to pass to `sorted()`

If you use this key function:
https://mail.python.org/archives/list/python-ideas@python.org/message/M3DEOZLA63Z5OIF6H6ZCEXK36GQMLVVA/

in my median() function:
https://mail.python.org/archives/list/python-ideas@python.org/message/KN6BSMJRVCPSQW32DTWQHTGZ5E3E5KK2/

you can simply do:

```
median(iterable, key=iliadSort)
```

and you have "poisoned" your data.

If you want to use the sorted iterable again later in the code, you can just do:

```
sorted_it = sorted(iterable, key=iliadSort)
median(sorted_it, sort_fn=None)
```

I prefer this approach, since this way you can avoid potentially re-sorting of 
your data.

For the same reason, if you want to remove the NaNs, it's better to create an 
iterable apart instead of creating it on the fly, because you can reutilize it:

```
filtered_it = [x for x in iterable if not math.isnan(x)]
median(filtered_it)
```

If you want to raise an error, you can use another key function:

```
class NanError(ValueError):
    pass

def alertNan(x):
    if math.isnan(x):
        raise NanError("There a NaN in my dish!")
    
    return x

```

and then use it:

```
median(iterable, key=alertNan)
```

But if you  absolutely want a nan parameter, you can create a wrapper for 
`sorted`:

```
def nansorted(iterable, on_nan="poison", **kwargs):
    if on_nan == "poison":
        return sorted(iterable, key=iliadSort, **kwargs)
    
    if on_nan == "remove":
        new_iterable = [x for x in iterable if not math.isnan(x)]
        return sorted(new_iterable, **kwargs)
    
    if on_nan == "raise":
        return sorted(iterable, key=alertNan, **kwargs)
    
    raise ValueError(f"Unsupported on_nan parameter value: {on_nan}")
```

and then use it in my `median()`:

```
median(iterable, sort_fn=nansorted, on_nan="raise")
```

or, as before

```
sorted_it = nansorted(iterable, on_nan="poison")
median(sorted_it, sort_fn=None)
```
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/OBNFSEUQE5K6PN74L7NNZ5V36XDDFR7M/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to