Created
April 18, 2018 09:12
-
-
Save itrav/595b5938bca32a163a7409c4ad584aca to your computer and use it in GitHub Desktop.
a la let-in (ML's operator) in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// _______________________________________ | |
// | let-in в ML | a la let-in в JS | | |
// |--------------------|------------------| | |
// | let | (( | | |
// | val a = 1 | a = 1, | | |
// | val b = 2 | b = 2 | | |
// | in | ) => | | |
// | a + b | a + b | | |
// | end | )(); | | |
// |____________________|__________________| | |
// | |
// среднее выборочное значение | |
const mean = xs => | |
((n = xs.length) => // а ля let-in | |
xs.reduce((m, x) => m + x / n, 0))(); | |
// выборочное стандартное отклонение | |
const sd = xs => | |
((n = xs.length, // а здесь сразу несколько | |
m = mean(xs)) => // определений в нашем а ля let-in | |
Math.sqrt(mean(xs.map(x => Math.pow(x - m, 2))) * n / (n - 1)))(); | |
// нормализация значений в массиве | |
const norm = xs => | |
((m = mean(xs), | |
s = sd(xs)) => | |
xs.map(x => (x - m) / s))(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment