Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function composition section #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Expand Up @@ -34,6 +34,7 @@ For simplicity, many of the code examples here operate on floating point values
- [hat **`â`**](#hat) - *unit vector*
- ["element of" `∈` `∉`](#element)
- [function `ƒ`](#function)
- [function composition](#function-composition)
- [piecewise function](#piecewise-function)
- [prime `′`](#prime)
- [more...](#more)
Expand Down Expand Up @@ -524,6 +525,38 @@ function length (x, y) {
}
```

### function composition

Functions take certain inputs and produce exactly one output. Function composition is when a function's input is a function as well.

For example, suppose we have these functions

![fncs](http://latex.codecogs.com/svg.latex?f%28x%29%20%3D%20x%5E2%2C%5C%3A%20%5C%3A%20g%28x%29%3Dx+1)

We can compose f with g, denoted `(f o g)(x)` by placing `g(x)` inside `f(x)`

![fog](http://latex.codecogs.com/svg.latex?%28f%5Ccirc%20g%29%28x%29%20%3D%20f%28g%28x%29%29%20%3Df%28x+1%29%20%3D%20%28x+1%29%5E2)

In code this would look like
```js
function f(x) {
return Math.pow(x, 2);
}

function g(x) {
return x + 1;
}

function fComposedWithg(f, g) {
return function(x) {
return f(g(x));
}
}
```
The order of composition matters as g composed with f would produce a different function

![gof](http://latex.codecogs.com/svg.latex?%28g%5Ccirc%20f%29%28x%29%20%3D%20g%28f%28x%29%29%20%3Dg%28x%5E2%29%20%3D%20x%5E2+1)

### piecewise function

Some functions will use different relationships depending on the input value, *x*.
Expand Down