This code provides a straightforward solution:
const findNonZeroRows = items => items
.map ( (_, i) => i )
.filter ( i => ! items [i] .some (x => x == 0) )
var items = [ [1, 0, 1], [3, 3, 3], [5, 6, 5] ];
console .log (
findNonZeroRows(items)
)
The map
function converts the outer list into a list of indices.
Then filter
is used along with some
to check if any element in each row at that index has a value of 0.
If you prefer the actual rows instead of their indices, a simpler approach would be:
const findNonZeroRows = items => items .filter (row => ! row .some (x => x == 0))
.
Update
In response to a query:
Could you please simplify the syntax? :(
While the current version is already simple, we can improve it by using a dedicated range
function like this:
const range = (lo, hi) => Array .from ( {length: hi - lo}, (_, i) => i )
This enables usage as follows:
const findNonZeroRows = (items) =>
range (0, items.length)
.filter ( i => ! items [i] .some (x => x == 0) )
This rearrangement doesn't simplify matters further but does enhance the structure. It's considered a better alternative.
We could also alter the function within filter
. Instead of
.filter ( i => ! items [i] .some (x => x == 0) )
we could modify it to:
.filter ( i => items [i] .every (x => x != 0) )
Using the not-equal
sign might enhance readability compared to standalone not
, although both are equally simple.
Javascript limitations may prevent further simplification beyond this point. Perhaps what you're looking for is familiarity rather than simplicity. For more insight on complexity and simplicity, I recommend Rich Hickey's talk on Simple Made Easy.
I've demonstrated several steps that could make the code appear more familiar to certain individuals. However, these changes illustrate why prioritizing simplicity over easiness is crucial. Please refrain from implementing them.
Utilizing Function Expressions
An initial modification involves replacing arrow functions with traditional function expressions:
const findNonZeroRows = function (items) {
return items.map (function (item, index) {
return index;
})
.filter (function (index) {
return ! items [index] .some (function(value) {
return value == 0;
})
})
}
This version adds explicit function
and return
statements while changing single-expression bodies to curly braces. Although visually cluttered, no reduction in complexity occurs.
Let's proceed to worsen matters.
Note that the function's structure remains a series of one-liners such as
(input).map(someFunction).filter(anotherFunction)
, thereby lacking space for debugging statements or named intermediate variables.
Naming Intermediate Values
This updated version permits insertion of an essential console.log
statement midway through processing:
const findNonZeroRows = function (items) {
const indices = items .map (function (item, index) {
return index;
});
// console.log(`Indices found`, indices);
const nonZeroesIndices = indices .filter (function (index) {
return ! items [index] .some (function(value) {
return value == 0;
});
})
return nonZeroesIndices;
}
Introducing variable assignments veers away from simplicity and introduces extra elements to the codebase.
Transitioning to a Single Loop
To address potential performance concerns, particularly iterating over rows twice, consider switching to plain for
loops:
const findNonZeroRows = function (items) {
const nonZeroesIndices = [];
for (let rowIndex = 0; rowIndex < items.length; rowIndex++) {
const row = items[rowIndex]
let anyZeros = false
for (let index = 0; index < row.length; index++) {
if (row[index] === 0) {
anyZeros = true;
break;
}
}
if (!anyZeros) {
nonZeroesIndices.push(rowIndex)
}
}
return nonZeroesIndices;
}
Conclusion
The sarcasm used is not directed towards anyone specifically but originates from experiences during my career. Embracing advanced coding techniques leads to enhanced skills and career progression. Remember to prioritize simplicity over easiness when writing code.
1 The digression regarding optimization was intended humorously. Avoid complicating your code unless profiling demonstrates specific sections causing severe performance bottlenecks.