Skip to content

Instantly share code, notes, and snippets.

@itrav
Created April 18, 2018 09:12
Show Gist options
  • Save itrav/595b5938bca32a163a7409c4ad584aca to your computer and use it in GitHub Desktop.
Save itrav/595b5938bca32a163a7409c4ad584aca to your computer and use it in GitHub Desktop.
a la let-in (ML's operator) in JavaScript
// _______________________________________
// | 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