Skip to content

Commit bc061e0

Browse files
committed
Add alternative fp implementation
1 parent d19e066 commit bc061e0

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
5+
const proportion = (max, val) => Math.round(val * 100 / max);
6+
const compose = (...funcs) => x => funcs.reduce((x, fn) => fn(x), x);
7+
8+
const densityCol = 3;
9+
const cellWidth = [18, 10, 8, 8, 18, 6];
10+
11+
const renderTable = table => table
12+
.map(row => row
13+
.map((cell, i) => cell
14+
.toString()[(i ? 'padStart' : 'padEnd')](cellWidth[i]))
15+
.join(''))
16+
.join('\n');
17+
18+
const calcProportion = table => (
19+
table.sort((row1, row2) => (row2[densityCol] - row1[densityCol])),
20+
table.map(row => (
21+
row.push(proportion(table[0][densityCol], row[densityCol])), row
22+
))
23+
);
24+
25+
const getDataset = file => fs.readFileSync(file).toString()
26+
.split('\n')
27+
.filter((s, i) => i && s)
28+
.map(line => line.split(','));
29+
30+
const main = compose(getDataset, calcProportion, renderTable);
31+
32+
console.log(main('./cities.dat'));

0 commit comments

Comments
 (0)