Skip to content

Commit 284e033

Browse files
committed
Code style according to ours eslint rules
1 parent dedd21b commit 284e033

File tree

1 file changed

+44
-36
lines changed

1 file changed

+44
-36
lines changed

JavaScript/2-functional-pure.js

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,81 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
15
/* fp */
2-
const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x)
3-
const curry = fn => (...args) => fn.bind(null, ...args)
4-
const map = curry((fn, arr) => arr.map(fn))
5-
const join = curry((str, arr) => arr.join(str))
6-
const split = curry((splitOn, str) => str.split(splitOn))
7-
const sort = curry((compareFn, arr) => arr.sort(compareFn))
8-
const filter = curry((filterFn, arr) => arr.filter(filterFn))
9-
/* utility */
10-
const first = arr => arr[0]
11-
const skipFirst = arr => arr.slice(1)
12-
const hasValue = val => !!val
13-
const toStr = val => val + ''
14-
const appendCell = (row, value) => row.concat(value)
15-
const cellPad = (index, str, width) => index ? str.padStart(width) : str.padEnd(width)
6+
const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x);
7+
const curry = fn => (...args) => fn.bind(null, ...args);
8+
const map = curry((fn, arr) => arr.map(fn));
9+
const join = curry((str, arr) => arr.join(str));
10+
const split = curry((splitOn, str) => str.split(splitOn));
11+
const sort = curry((compareFn, arr) => arr.sort(compareFn));
12+
const filter = curry((filterFn, arr) => arr.filter(filterFn));
1613

17-
const cellWidth = index => [18, 10, 8, 8, 18, 6][index]
14+
/* utility */
15+
const first = arr => arr[0];
16+
const skipFirst = arr => arr.slice(1);
17+
const hasValue = val => !!val;
18+
const toStr = val => val + '';
19+
const appendCell = (row, value) => row.concat(value);
20+
const cellPad = (index, str, width) => (
21+
index ? str.padStart(width) : str.padEnd(width)
22+
);
23+
const cellWidth = index => [18, 10, 8, 8, 18, 6][index];
1824

19-
const renderCell = (cell, index) => cellPad(index, toStr(cell), cellWidth(index))
20-
const renderRow = pipe(
21-
map(renderCell),
22-
join('')
23-
)
24-
const renderTable = pipe(
25-
map(renderRow),
26-
join('\n')
27-
)
25+
const renderCell = (cell, index) => (
26+
cellPad(index, toStr(cell), cellWidth(index))
27+
);
28+
const renderRow = pipe(map(renderCell), join(''));
29+
const renderTable = pipe(map(renderRow), join('\n'));
2830

29-
const getDensityCell = row => parseInt(row[3], 10)
30-
const proportion = (max, val) => Math.round(parseInt(val, 10) * 100 / max)
31-
const sortRowsByDensity = sort((row1, row2) => getDensityCell(row2) - getDensityCell(row1))
31+
const getDensityCell = row => parseInt(row[3], 10);
32+
const proportion = (max, val) => Math.round(parseInt(val, 10) * 100 / max);
33+
const sortRowsByDensity = sort(
34+
(row1, row2) => getDensityCell(row2) - getDensityCell(row1)
35+
);
3236

3337
const calcMaxDensity = pipe(
3438
sortRowsByDensity,
3539
first,
3640
getDensityCell
37-
)
41+
);
3842

3943
const calcRowsProportionToMax = rows => max => rows.map(pipe(
4044
getDensityCell,
4145
densityCell => proportion(max, densityCell)
42-
))
46+
));
47+
4348
const appendProportionCell = rows => proportions => rows.map(
4449
(row, index) => appendCell(row, proportions[index])
45-
)
50+
);
51+
4652
const appendTableProportionCol = rows => pipe(
4753
calcMaxDensity,
4854
calcRowsProportionToMax(rows),
4955
appendProportionCell(rows)
50-
)(rows)
56+
)(rows);
57+
58+
const parseTableLines = map(split(','));
5159

52-
const parseTableLines = map(split(','))
5360
const toLines = pipe(
5461
split('\n'),
5562
skipFirst,
5663
filter(hasValue)
57-
)
64+
);
5865

59-
const readFile = file => fs.readFileSync(file).toString()
66+
const readFile = file => fs.readFileSync(file).toString();
6067

6168
const getDataset = pipe(
6269
readFile,
6370
toLines,
6471
parseTableLines
65-
)
72+
);
6673

6774
const main = pipe(
6875
getDataset,
6976
sortRowsByDensity,
7077
appendTableProportionCol,
7178
renderTable
72-
)
79+
);
7380

81+
console.log(main('./cities.dat'));

0 commit comments

Comments
 (0)