McNuggets problem: Difference between revisions

→‎{{header|JavaScript}}: dropWhile in lieu of filter
(→‎{{header|JavaScript}}: Added a JS draft)
(→‎{{header|JavaScript}}: dropWhile in lieu of filter)
Line 138:
)
),
gapsxs = filterdropWhile(
x => !nuggets.has(x),
enumFromThenTo(100, 99, 1)
);
 
return 0 < gapsxs.length ? (
gapsxs[0]
) : 'No unreachable quantities found in this range';
};
Line 153:
const bindList = (xs, mf) => [].concat.apply([], xs.map(mf));
 
// filterdropWhile :: (a -> Bool) -> [a] -> [a]
const filterdropWhile = (fp, xs) => xs.filter(f);{
const lng = xs.length;
return 0 < lng ? xs.slice(
until(
i => i === lng || !p(xs[i]),
i => 1 + i,
0
)
) : [];
};
 
// enumFromThenTo :: Int -> Int -> Int -> [Int]
Line 167 ⟶ 178:
length: 1 + n - m
}, (_, i) => m + i);
 
// filter :: (a -> Bool) -> [a] -> [a]
const filter = (f, xs) => xs.filter(f);
 
// quot :: Int -> Int -> Int
Line 176 ⟶ 184:
// sum :: [Num] -> Num
const sum = xs => xs.reduce((a, x) => a + x, 0);
 
// until :: (a -> Bool) -> (a -> a) -> a -> a
const until = (p, f, x) => {
let v = x;
while (!p(v)) v = f(v);
return v;
};
 
// MAIN ---
9,659

edits