Greatest subsequential sum: Difference between revisions
m
→JS Functional: (Adjusted type signature of main function)
m (→{{header|AppleScript}}: Updated primitives – max and gt) |
m (→JS Functional: (Adjusted type signature of main function)) |
||
Line 1,597:
===Functional===
Linear approach,
<lang JavaScript>(() => {
// maxSubseq :: [Int] -> (Int, [Int])
const maxSubseq = xs =>
snd(xs.reduce((tpl, x) => {
const [m1, m2] = Array.from(fst(tpl
high = max(
Tuple(0, []),
Tuple(m1 + x, m2.concat(x))
);
return Tuple(high, max(snd(tpl
}, Tuple(Tuple(0, []), Tuple(0, [])))
// TEST -----------------------------------------------
// main :: IO ()
const main = () => {
showLog(
}
▲ maxSubseq([-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1])
// [3,5,6,-2,-1,4] -> 15
Line 1,626 ⟶ 1,624:
// GENERIC FUNCTIONS ----------------------------------
//
const
// gt :: Ord a => a -> a -> Bool
Line 1,637 ⟶ 1,632:
x[0] > y[0]
) : (x > y);
// max :: Ord a => a -> a -> a
Line 1,652 ⟶ 1,644:
);
//
const
// Tuple (,) :: a -> b -> (a, b)
|