Spiral matrix: Difference between revisions
→JS ES6: Updated primitives and output.
m (→{{header|Phix}}: syntax coloured) |
(→JS ES6: Updated primitives and output.) |
||
Line 2,536:
{{Trans|Haskell}}
<lang JavaScript>(() => {
unlines(▼
);▼
// spiral :: Int -> [[Int]]
const spiral = n => {
const go = (rows, cols, start) =>
enumFromTo(start
...
start
)
).map(reverse)
] : [
[]
];
return go(n, n, 0);
};
▲ // GENERIC FUNCTIONS ----------------------------------
// ---------------------- TEST -----------------------
const main
▲ );
// --------------------- GENERIC ---------------------
})() : [];▼
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo =
}, (_,
0 < xs.length ? (▼
// pred :: Enum a => a -> a
const pred = x => x - 1;
// reverse :: [a] -> [a]
const reverse = xs =>
xs.
// transpose :: [[a]] -> [[a]]
const transpose =
// The columns of the input transposed
// into new
// Simpler version of transpose, assuming input
// rows of even
Boolean(rows.length)
) : [];
// unlines :: [String] -> String
const unlines = xs =>
// A single string formed by the intercalation
// of a list of strings with the newline character.
// MAIN ---
Line 2,658 ⟶ 2,617:
})();</lang>
{{Out}}
<pre> 0 1 2 3 4
15 16 17 18 5
14 23 24 19 6
13 22 21 20 7
12 11 10 9 8</pre>
=={{header|jq}}==
|