Jump to content

Split a character string based on change of character: Difference between revisions

→‎JS ES6: updated groupBy primitive ( redrafted as a fold, rather than an explicit recursion with 'span' )
m (→‎{{header|Perl 6}}: Remove stray parenthesis)
(→‎JS ES6: updated groupBy primitive ( redrafted as a fold, rather than an explicit recursion with 'span' ))
Line 178:
// groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
const groupBy = (f, xs) => {
const lngdct = xs.length;slice(1)
.reduce((a, x) => {
const
h = a.active.length > 0 ? a.active[0] : undefined,
blnGroup = h !== undefined && f(h, x);
 
if (lng > 0) return {
const active: blnGroup ? a.active.concat(x) =: xs[0x],
lstSpanRest = span(y => f(x,sofar: y),blnGroup xs? a.slicesofar : a.sofar.concat(1)[a.active]),
ys = lstSpanRest[0],};
}, zs = lstSpanRest[1],{
group =active: [(ysxs.length > 0 ? [xxs[0]].concat(ys) : [x])];,
} else return sofar: [];
 
return zs.length ? group.concat(groupBy(f, zs})) : group;
return dct.sofar.concat(dct.active.length > 0 ? [dct.active] : []);
 
} else return [];
};
 
// Span of unbroken predicate matches at left, returned with remainder
// span :: (a -> Bool) -> [a] -> ([a],[a])
const span = (f, xs) => {
for (var i = 0, lng = xs.length;
(i < lng) && f(xs[i]); i++) {}
return [xs.slice(0, i), xs.slice(i)];
}
 
// intercalate :: String -> [a] -> String
const intercalate = (s, xs) => xs.join(s);
 
 
// TEST
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.