Identity matrix: Difference between revisions

Content added Content deleted
(Frink)
Line 1,665: Line 1,665:


===ES6===
===ES6===

<lang JavaScript>(() => {
<lang JavaScript>(() => {


// idMatrix :: Int -> [[0 | 1]]
// identityMatrix :: Int -> [Int]
const idMatrix = n => Array.from({
const identityMatrix = n =>
Array.from({
length: n
length: n
}, (_, i) => Array.from({
}, (_, i) => Array.from({
Line 1,675: Line 1,675:
}, (_, j) => i !== j ? 0 : 1));
}, (_, j) => i !== j ? 0 : 1));


// show :: a -> String
const show = JSON.stringify;


// ----------------------- TEST ------------------------
// TEST
return idMatrix(5)
return identityMatrix(5)
.map(show)
.map(JSON.stringify)
.join('\n');
.join('\n');
})();</lang>
})();</lang>

{{Out}}
{{Out}}
<pre>[1,0,0,0,0]
<pre>[1,0,0,0,0]