Skip to content

Commit f8ef05a

Browse files
sneakers-the-ratlwasser
authored andcommitted
an aside on function composition
1 parent 3acacc2 commit f8ef05a

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

code-workflow-logic/python-functions-multi-parameters.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ def mean_mm_to_in(data_mm, axis_value):
189189

190190
Note that the function could be written to convert the values first and then calculate the mean. However, given that the function will complete both tasks and return the mean values in the desired units, it is more efficient to calculate the mean values first and then convert just those values, rather than converting all of the values in the input array.
191191

192+
````{tip}
193+
Typically functions should have a [single purpose](https://en.wikipedia.org/wiki/Separation_of_concerns), and be [composed](https://en.wikipedia.org/wiki/Function_composition_(computer_science)) to implement higher-order operations. The above function would normally be written without wrapping {func}`numpy.mean` like this:
194+
195+
```python
196+
mm_to_in(np.mean(data, axis=axis_value))
197+
```
198+
199+
The purpose of this lesson is to introduce function parameters, so let's focus on that for now and save design principles for another lesson :).
200+
````
201+
192202

193203
Last, include a docstring to provide the details about this function, including a brief description of the function (i.e. how it works, purpose) as well as identify the input parameters (i.e. type, description) and the returned output (i.e. type, description).
194204
<!-- #endregion -->

0 commit comments

Comments
 (0)