2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -0,0 +1,39 @@
(function (strTest) {
'use strict';
// s -> [n]
function expansion(strExpr) {
// concat map yields flattened output list
return [].concat.apply([], strExpr.split(',')
.map(function (x) {
return x.split('-')
.reduce(function (a, s, i, l) {
// negative (after item 0) if preceded by an empty string
// (i.e. a hyphen-split artefact, otherwise ignored)
return s.length ? i ? a.concat(
parseInt(l[i - 1].length ? s :
'-' + s, 10)
) : [+s] : a;
}, []);
// two-number lists are interpreted as ranges
})
.map(function (r) {
return r.length > 1 ? range.apply(null, r) : r;
}));
}
// [m..n]
function range(m, n) {
return Array.apply(null, Array(n - m + 1))
.map(function (x, i) {
return m + i;
});
}
return expansion(strTest);
})('-6,-3--1,3-5,7-11,14,15,17-20');

View file

@ -0,0 +1 @@
[-6, -3, -2, -1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]

View file

@ -0,0 +1,37 @@
(strTest => {
// expansion :: String -> [Int]
let expansion = strExpr =>
// concat map yields flattened output list
[].concat.apply([], strExpr.split(',')
.map(x => x.split('-')
.reduce((a, s, i, l) =>
// negative (after item 0) if preceded by an empty string
// (i.e. a hyphen-split artefact, otherwise ignored)
s.length ? i ? a.concat(
parseInt(l[i - 1].length ? s :
'-' + s, 10)
) : [+s] : a, [])
// two-number lists are interpreted as ranges
)
.map(r => r.length > 1 ? range.apply(null, r) : r)),
// range :: Int -> Int -> Maybe Int -> [Int]
range = (m, n, step) => {
let d = (step || 1) * (n >= m ? 1 : -1);
return Array.from({
length: Math.floor((n - m) / d) + 1
}, (_, i) => m + (i * d));
};
return expansion(strTest);
})('-6,-3--1,3-5,7-11,14,15,17-20');

View file

@ -0,0 +1 @@
[-6, -3, -2, -1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]