Middle three digits: Difference between revisions

Content added Content deleted
m (→‎{{header|JavaScript}}: (simpler expression for max numeric string length))
Line 2,674: Line 2,674:
-100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0
-100, 100, -12345, 1, 2, -1, -10, 2002, -2002, 0
],
],
w = length(
w = maximum(map(x => x.toString().length, xs));
maximumBy(
comparing(length),
map(x => x.toString(), xs)
)
);
return (
return (
unlines(map(
unlines(map(
Line 2,709: Line 2,704:
// abs :: Num -> Num
// abs :: Num -> Num
const abs = Math.abs;
const abs = Math.abs;

// comparing :: (a -> b) -> (a -> a -> Ordering)
const comparing = f =>
(x, y) => {
const
a = f(x),
b = f(y);
return a < b ? -1 : (a > b ? 1 : 0);
};


// drop :: Int -> [a] -> [a]
// drop :: Int -> [a] -> [a]
Line 2,737: Line 2,723:
// even :: Int -> Bool
// even :: Int -> Bool
const even = n => 0 === n % 2;
const even = n => 0 === n % 2;

// foldl1 :: (a -> a -> a) -> [a] -> a
const foldl1 = (f, xs) =>
1 < xs.length ? xs.slice(1)
.reduce(f, xs[0]) : xs[0];


// id :: a -> a
// id :: a -> a
Line 2,757: Line 2,748:
) : Infinity;
) : Infinity;


// maximumBy :: (a -> a -> Ordering) -> [a] -> a
// maximum :: Ord a => [a] -> a
const maximumBy = (f, xs) =>
const maximum = xs =>
0 < xs.length ? (
0 < xs.length ? (
xs.slice(1)
foldl1((a, x) => x > a ? x : a, xs)
.reduce((a, x) => 0 < f(x, a) ? x : a, xs[0])
) : undefined;
) : undefined;