И. Ю. Травкин [email protected] Май, 2018
Просто захотелось.
let data = [2, 4, 6, 8]; | |
mean(data); // => 5.0 | |
// common way | |
const mean = function (xs) { | |
let n = xs.length; | |
return xs.reduce((m, x) => m + x / n, 0); | |
}; | |
// using ES6 default parameters and IIFE to emulate |
И. Ю. Травкин [email protected] Май, 2018
Просто захотелось.
let raw = `M,0.455,0.365,0.095,0.514,0.2245,0.101,0.15,15 | |
M,0.35,0.265,0.09,0.2255,0.0995,0.0485,0.07,7 | |
... | |
F,0.53,0.42,0.135,0.677,0.2565,0.1415,0.21,9`; | |
let parsed = raw.split("\n").map(line => line.split(",").map((v, i) => i > 0 ? parseFloat(v): v == "M" ? 1 : -1)); | |
let X = parsed.map(row => row.slice(0, row.length - 1)); | |
let y = parsed.map(row => row[row.length - 1]); |
// Console API | |
const print = o => console.log(JSON.stringify(o)); | |
const printm = A => console.log(A.map(row => row.join("\t")).join("\n")); | |
// Matrix API | |
//1 | |
const trans = A => A[0].map((_, j) => A.map(row => row[j])); |
// _______________________________________ | |
// | 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 | )(); | | |
// |____________________|__________________| |
// Without memoization | |
const U = f => f(f); | |
const Z_ = t => (f => f(n => t(t)(f)(n))); | |
const Z = U(Z_); | |
// With memoization | |
const U_mem = f => f(f, {}); |
Transcript show: 'Hello, World!'; cr. |