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

Feature: add ES6 #68

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 105 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,46 @@ For simplicity, many of the code examples here operate on floating point values

# contents

- [variable name conventions](#variable-name-conventions)
- [equals `=` `≈` `≠` `:=`](#equals-symbols)
- [square root and complex numbers `√` *`i`*](#square-root-and-complex-numbers)
- [dot & cross `·` `×` `∘`](#dot--cross)
- [scalar multiplication](#scalar-multiplication)
- [vector multiplication](#vector-multiplication)
- [dot product](#dot-product)
- [cross product](#cross-product)
- [sigma `Σ`](#sigma) - *summation*
- [capital Pi `Π`](#capital-pi) - *products of sequences*
- [pipes `||`](#pipes)
- [absolute value](#absolute-value)
- [Euclidean norm](#euclidean-norm)
- [determinant](#determinant)
- [hat **`â`**](#hat) - *unit vector*
- ["element of" `∈` `∉`](#element)
- [common number sets `ℝ` `ℤ` `ℚ` `ℕ`](#common-number-sets)
- [function `ƒ`](#function)
- [piecewise function](#piecewise-function)
- [common functions](#common-functions)
- [function notation `↦` `→`](#function-notation)
- [prime `′`](#prime)
- [floor & ceiling `⌊` `⌉`](#floor--ceiling)
- [arrows](#arrows)
- [material implication `⇒` `→`](#material-implication)
- [equality `<` `≥` `≫`](#equality)
- [conjunction & disjunction `∧` `∨`](#conjunction--disjunction)
- [logical negation `¬` `~` `!`](#logical-negation)
- [intervals](#intervals)
- [more...](#more)
- [math-as-code](#math-as-code)
- [foreword](#foreword)
- [contents](#contents)
- [variable name conventions](#variable-name-conventions)
- [equals symbols](#equals-symbols)
- [square root and complex numbers](#square-root-and-complex-numbers)
- [dot & cross](#dot--cross)
- [scalar multiplication](#scalar-multiplication)
- [vector multiplication](#vector-multiplication)
- [dot product](#dot-product)
- [cross product](#cross-product)
- [sigma](#sigma)
- [capital Pi](#capital-pi)
- [pipes](#pipes)
- [absolute value](#absolute-value)
- [Euclidean norm](#euclidean-norm)
- [determinant](#determinant)
- [hat](#hat)
- [element](#element)
- [common number sets](#common-number-sets)
- [`ℝ` real numbers](#%E2%84%9D-real-numbers)
- [`ℚ` rational numbers](#%E2%84%9A-rational-numbers)
- [`ℤ` integers](#%E2%84%A4-integers)
- [`ℕ` natural numbers](#%E2%84%95-natural-numbers)
- [`ℂ` complex numbers](#%E2%84%82-complex-numbers)
- [function](#function)
- [piecewise function](#piecewise-function)
- [common functions](#common-functions)
- [function notation](#function-notation)
- [prime](#prime)
- [floor & ceiling](#floor--ceiling)
- [arrows](#arrows)
- [material implication](#material-implication)
- [equality](#equality)
- [conjunction & disjunction](#conjunction--disjunction)
- [logical negation](#logical-negation)
- [intervals](#intervals)
- [more...](#more)
- [Contributing](#contributing)
- [License](#license)

## variable name conventions

Expand Down Expand Up @@ -324,6 +334,17 @@ var sum = 0
for (var i = 1; i <= 100; i++) {
sum += i
}

//ES6
const sum = (start, end) =>
Array(end - start + 1)
.fill()
.map((_, index) => start + index)
.reduce((accumulator, current) =>
accumulator + current
);

console.log(sum(1, 100));
```

The result of `sum` is `5050`.
Expand All @@ -348,6 +369,18 @@ var sum = 0
for (var i = 1; i <= 100; i++) {
sum += (2 * i + 1)
}

//ES6
const sum = (start, end) =>
Array(end - start + 1)
.fill()
.map((_, index) => start + index)
.reduce((accumulator, current) =>
accumulator + 2 * current + 1,
0
);

console.log(sum(1, 100));
```

The result of `sum` is `10200`.
Expand Down Expand Up @@ -388,6 +421,17 @@ var value = 1
for (var i = 1; i <= 6; i++) {
value *= i
}

//ES6
const value = (start, end) =>
Array(end - start + 1)
.fill()
.map((_, index) => start + index)
.reduce((accumulator, current) =>
accumulator * current
);

console.log(value(1, 6));
```

Where `value` will evaluate to `720`.
Expand Down Expand Up @@ -443,6 +487,13 @@ function length (vec) {
var z = vec[2]
return Math.sqrt(x * x + y * y + z * z)
}

//ES6
const length = vec =>
Math.sqrt(vec.reduce(
(accumulator, current) =>
accumulator += current* current)
);
```

Other implementations:
Expand Down Expand Up @@ -510,6 +561,22 @@ function normalize(vec) {
}
return vec
}

//ES6
const normalize = vec => {
var squaredLength = vec.reduce(
(accumulator, current) =>
accumulator + current * current,
0
);

if (squaredLength > 0) {
var length = Math.sqrt(squaredLength);
vec.forEach((_, index) =>
vec[index] /= length);
}
return vec;
}
```

Other implementations:
Expand Down Expand Up @@ -721,6 +788,14 @@ function sgn (x) {
if (x > 0) return 1
return 0
}

//ES6
const sgn = x =>
x < 0
? -1
: x > 0
? 1
: 0;
```

See [signum](https://github.com/scijs/signum) for this function as a module.
Expand Down