Magic squares of doubly even order: Difference between revisions

m
→‎JS ES6: Tidied the test section.
(→‎JS ES6: Updated primitives, tidied.)
m (→‎JS ES6: Tidied the test section.)
Line 1,311:
const main = () =>
// Magic squares of orders 4, 8 and 12.
mapintercalate('\n => {\n')(
constmap(n => {
lines = doublyEvenMagicSquare(n),const
sums = lines.concat = doublyEvenMagicSquare(n),
transposesums = map(linessum)(
lines.concat(diagonals(lines))
transpose(lines)
.map(xs => xs.reduce((a, b) => a + b, 0 .concat(diagonals(lines)),
sum = sums[0]; )
return [ ),
"Order: " + n.toString(), total = sums[0];
"Summingreturn to: " + sum.toStringunlines(),[
"Row, column and diagonal sums checked"Order: " + str(n),
all "Summing to: " + str(eq(sum))(sumstotal),
.toString() + '\n' "Row, column and diagonal sums checked: " +
lines.map str(all(eq(total))(sums)) + '\n',
xs => xs.unlines(map(compose(
compose(justifyRight(3)intercalate(' '), str)
map(compose(justifyRight(3)(' '), str))
.join))(' 'lines))
.join('\n'])
].join})('\n'[4, 8, 12])
})([4, 8, 12]);
.join('\n\n');
 
 
Line 1,394 ⟶ 1,393:
// flatten :: NestedList a -> [a]
const flatten = nest => nest.flat(Infinity);
 
// intercalate :: String -> [String] -> String
const intercalate = s =>
xs => xs.join('\n\n's);
 
// isInt :: Int -> Bool
Line 1,427 ⟶ 1,430:
// str :: a -> String
const str = x => x.toString();
 
// sum :: [Num] -> Num
const sum = xs => xs.reduce((a, x) => a + x, 0);
 
// transpose :: [[a]] -> [[a]]
const transpose = xs =>
xs[0].map((_, iCol) => xs.map((row) => row[iCol]));
 
// unlines :: [String] -> String
const unlines = xs => xs.join('\n');
 
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
Line 1,438 ⟶ 1,447:
).map((x, i) => f(x)(ys[i]));
 
// MAIN ------------------------------------------------
return main();
})();</lang>
9,659

edits