Skip to content

Instantly share code, notes, and snippets.

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

Алгоритм Хаффмана: реализация на «функциональном» JavaScript

И. Ю. Травкин ivan.travkin@live.com Май, 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]));
@itrav
itrav / let-in.js
Created April 18, 2018 09:12
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 | )(); |
// |____________________|__________________|
@itrav
itrav / y_combinator_memoization.js
Created April 17, 2018 01:35
Y-combinator (strict version, Z-combinator) with automatic memoization
// 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, {});
@itrav
itrav / hello.st
Created January 1, 2011 10:31
Smalltalk version of 'Hello, World!'
Transcript show: 'Hello, World!'; cr.