Spiral matrix: Difference between revisions

1,142 bytes removed ,  2 years ago
→‎JS ES6: Updated primitives and output.
m (→‎{{header|Phix}}: syntax coloured)
(→‎JS ES6: Updated primitives and output.)
Line 2,536:
{{Trans|Haskell}}
<lang JavaScript>(() => {
'"use strict'";
 
// GENERIC FUNCTIONS ------------------ SPIRAL MATRIX ------------------
// main :: () -> String
const main = () =>
unlines(
map(unwords, spiral(5))
);
 
// spiral :: Int -> [[Int]]
const spiral = n => {
const go = (rows, cols, start) =>
0 < Boolean(rows) ? [
enumFromTo(start, )(start + pred(cols)),
...maptranspose(
reverse,go(
transpose( cols,
gopred(rows),
start + cols,
pred(rows),
start + cols
)
)
).map(reverse)
] : [
[]
];
 
return go(n, n, 0);
};
 
// GENERIC FUNCTIONS ----------------------------------
 
// ---------------------- TEST -----------------------
// comparing :: (a -> b) -> (a -> a -> Ordering)
const// comparingmain =:: f() =-> String
const main = (x, y) => {
constunlines(
a = fspiral(x5),.map(
bxs => f(y);
return a < b ? -1 : xs.map(ax => b`${x}`.padStart(3, ?" 1 : 0"));
}; ).join("")
unlines( )
);
 
 
// concatMap :: (a -> [b]) -> [a] -> [b]
// --------------------- GENERIC ---------------------
const concatMap = (f, xs) =>
0 < xs.length ? (() => {
const unit = 'string' !== typeof xs ? (
[]
) : '';
return unit.concat.apply(unit, xs.map(f))
})() : [];
 
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = (m, n) =>
mn <=> n ? iterateUntilArray.from({
xlength: =>1 + n <=- x,m
}, (_, xi) => 1m + x,i);
m
) : [];
 
// iterateUntil :: (a -> Bool) -> (a -> a) -> a -> [a]
const iterateUntil = (p, f, x) => {
const vs = [x];
let h = x;
while (!p(h))(h = f(h), vs.push(h));
return vs;
};
 
// length :: [a] -> Int
const length = xs => xs.length;
 
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
 
// Ordering: (LT|EQ|GT):
// GT: 1 (or other positive n)
// EQ: 0
// LT: -1 (or other negative n)
 
// maximumBy :: (a -> a -> Ordering) -> [a] -> a
const maximumBy = (f, xs) =>
0 < xs.length ? (
xs.slice(1)
.reduce((a, x) => 0 < f(x, a) ? x : a, xs[0])
) : undefined;
 
 
// pred :: Enum a => a -> a
const pred = x => x - 1;
 
 
// reverse :: [a] -> [a]
const reverse = xs =>
'"string'" !=== typeof xs ? (
xs.slicesplit(0"").reverse()
) : xs.split('').reverse() .join(''"");
0) <: xs.length ? slice(0).reverse();
 
// replicate :: Int -> a -> [a]
const replicate = (n, x) =>
Array.from({
length: n
}, () => x);
 
// transpose :: [[a]] -> [[a]]
const transpose = tblrows => {
// The columns of the input transposed
const
// into new gaps = replicate(rows.
// Simpler version of transpose, assuming input
length(maximumBy(comparing(length), tbl)), []
// rows of even ),length.
Boolean(rows.length) ? rows = [0].map(xs => xs.concat(gaps.slice(xs.length)), tbl);
return map (_, i) => rows.flatMap(
(_, col) => concatMap(row v => v[row[coli]], rows),
rows[0])
) : [];
 
};
 
// unlines :: [String] -> String
const unlines = xs => xs.join('\n');
// A single string formed by the intercalation
// of a list of strings with the newline character.
})xs.join("\n") : [];
 
// unwords :: [String] -> String
const unwords = xs => xs.join(' ');
 
// 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}}==
9,655

edits