]>
git.scottworley.com Git - inverse-tax/blob - tax.js
2b5bd4a1a820a0299683a68fd2aba9d0cbbb8add
3 function parse_table(as_text
) {
4 function parse_line(line
) {
5 return line
.trim().split(' ').filter(x
=> x
!== '').map(x
=> parseFloat(x
));
7 return as_text
.trim().split('\n').map(parse_line
);
10 function parse_tax_table(as_text
) {
11 return parse_table(as_text
).map(([start
, rate
], i
, table
) =>
12 [start
, i
< table
.length
- 1 ? table
[i
+1][0] : Infinity
, rate
/ 100.0]);
16 return nums
.reduce((total
, num
) => total
+ num
, 0);
19 function tax(table
, income
) {
20 return sum(table
.map(([start
, end
, rate
]) =>
21 Math
.max(0, Math
.min(income
, end
) - start
) * rate
));
24 function apply_deductible(table
, deductible
) {
25 return table
.map(([start
, end
, rate
]) => [start
+ deductible
, end
+ deductible
, rate
]);
28 function merge_tax_tables(t1
, t2
) {
29 if (t1
.length
== 0) return t2
;
30 if (t2
.length
== 0) return t1
;
31 [start1
, end1
, rate1
] = t1
[0];
32 [start2
, end2
, rate2
] = t2
[0];
33 if (start1
== start2
) {
35 return [[start1
, end1
, rate1
+ rate2
]].concat(merge_tax_tables(t1
.slice(1), t2
.slice(1)));
38 return [[start1
, end1
, rate1
+ rate2
]].concat(merge_tax_tables(t1
.slice(1), [[end1
, end2
, rate2
]].concat(t2
.slice(1))));
40 return merge_tax_tables(t2
, t1
);
42 if (start1
< start2
) {
44 return [t1
[0]].concat(merge_tax_tables(t1
.slice(1), t2
));
46 return [[start1
, start2
, rate1
]].concat(merge_tax_tables([[start2
, end1
, rate1
]].concat(t1
.slice(1)), t2
));
48 return merge_tax_tables(t2
, t1
);