Equilibrium index: Difference between revisions

→‎ES6 Functional: Tidied, updated primitives and output format.
(Added Lua language for this task)
(→‎ES6 Functional: Tidied, updated primitives and output format.)
Line 1,401:
 
<lang javascript>(() => {
'"use strict'";
 
// ---------------- EQUILIBRIUM INDEX ----------------
 
// equilibriumIndices :: [Int] -> [Int]
const equilibriumIndices = xs =>
zip(scanl1(add)(xs))(
const v = fscanl1(xadd)(a[0]xs);
});(
scanr1(add)(xs)
).reduceRight((a, xy, i) =>
xy[0] === xy[1] ? .reduceRight(
(a, xy, i) => xy[0] === xy[1] ? (
[i, ...a]
) : a, []);
b => ({);
 
 
// ------------------------ TEST- -----------------------
const main = () => {[
console.log(JSON.stringify([
[-7, 1, 5, 2, -4, 3, 0],
[2, 4, 6],
Line 1,421 ⟶ 1,427:
[1],
[]
].map(equilibriumIndices)));compose(
console.log( JSON.stringify([,
// -> [[3, 6], [], [1], [0, 1, 2, 3, 4, 5, 6], [0], []]
equilibriumIndices
};
'0': a,))
.join("\n");
// -> [[3, 6], [], [1], [0, 1, 2, 3, 4, 5, 6], [0], []]
 
 
// ----------------- GENERIC FUNCTIONS-- ----------------
 
// Tuple (,) :: a -> b -> (a, b)
const Tuple = a =>
b => ({
type: 'Tuple',
'0': a,
'1': b,
length: 2
});
 
// add (+) :: Num a => a -> a -> a
Line 1,441:
// Curried addition.
b => a + b;
 
 
// Tuplecompose (,<<<) :: a(b -> bc) -> (a, -> b) -> a -> c
const compose = (...fs) =>
// values,A buildingfunction fromdefined by the right.-to-left
// composition of all the functions in fs.
fs.reduce(
(f, g) => x => f(g(x)),
type:x 'Tuple',=> x
} );
 
 
// scanl :: (b -> a -> b) -> b -> [a] -> [b]
const scanl = f => startValue => xs =>
// scanlThe isseries likeof foldlinterim orvalues reduce, butarising
// returnsfrom a successioncatamorphism. ofParallel intermediateto foldl.
// valuesxs.reduce((a, buildingx) from=> the left.{
v => xs => xs.reduce((a, x) => {
const v = f(a[0])(x);
return Tuple(v)(a[1].concat(v));
}, Tuple(v)([v]))[1];
 
// scanr :: (b -> a -> b) ->return b ->[v, a[a1] -> [b.concat(v)];
}, [startValue, [startValue]])[1];
const scanr = f =>
 
// scanr is like foldr or reduceRight, but
// returns a succession of intermediate
// values, building from the right.
v => xs => xs.reduceRight((a, x) => {
const v = f(x)(a[0]);
return Tuple(v)([v].concat(a[1]));
}, Tuple(v)([v]))[1];
 
// scanl1 :: (a -> a -> a) -> [a] -> [a]
const scanl1 = f =>
// scanl1 is a variant of scanl that has no
// seed-starting value argument, and assumes that.
// xs is not empty.
xs => xs.length > 0 ? (
scanl(f)(
Line 1,472 ⟶ 1,473:
)(xs.slice(1))
) : [];
 
 
// scanr :: (a -> b -> b) -> b -> [a] -> [b]
const Tuplescanr = af =>
vstartValue => xs => xs.reducereduceRight((a, x) => {
'1': b(a, x) => {
const v = f(x)(a[0]);
 
return Tuple([v)(a, [1v].concat(v)a[1])];
}, [startValue, [startValue]]
}, Tuple(v)([v]))[1];
 
 
// scanr1 :: (a -> a -> a) -> [a] -> [a]
Line 1,483 ⟶ 1,496:
)(xs.slice(0, -1))
) : [];
 
 
// zip :: [a] -> [b] -> [(a, b)]
const zip = xs =>
// The paired members of xs and ys, up to
// the length of the shorter of the two lists.
ys => Array.from({
length: Math.min(xs.length, ys.length)
}, (_, i) => Tuple([xs[i])(, ys[i])]);
 
// MAIN ---
Line 1,494 ⟶ 1,510:
})();</lang>
{{Out}}
<pre>[[3,6],[],[1],[0,1,2,3,4,5,6],[0],[]]</pre>
[]
[1]
[0,1,2,3,4,5,6]
[0]
[]</pre>
 
=={{header|jq}}==
9,655

edits