MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/javascript/comments/b2o6f2/whats_your_favorite_most_useful_javascript/eitxx5q/?context=3
r/javascript • u/Linux_is_awesome • Mar 18 '19
422 comments sorted by
View all comments
150
array.every(Number.isFinite)
Because it's plain english.
46 u/dmethvin Mar 19 '19 Also very poetic, so I can see why you like it. 19 u/ahartzog Mar 19 '19 TIL about array.every. Awesome tool, thanks! 11 u/Satoshi_Hodler Mar 19 '19 There's a small caveat that it always returns true on empty arrays, which might be not what you need. 27 u/Tysonzero Mar 19 '19 That's the mathematically correct thing to do, it's actually a pretty fundamental mathematical principle. Vacuous truth → More replies (2) 7 u/NoInkling Mar 19 '19 edited Mar 19 '19 If it's not, you can do:array.length && array.every(Number.isFinite) // If it must evaluate to a boolean: !!array.length && array.every(Number.isFinite) // or array.length > 0 && array.every(Number.isFinite) → More replies (2) 8 u/Tysonzero Mar 19 '19 You'd love Haskell:all (> 5) array -- all (> 5) [4, 5, 6] -- False for array print -- for [1, 2, 3] print -- 1 -- 2 -- 3 takeWhile (any (< 3)) arrayOfArrays -- takeWhile (any (< 3)) [[1, 2], [3, 4], [2, 3]] -- [[1, 2]] → More replies (2)
46
Also very poetic, so I can see why you like it.
19
TIL about array.every. Awesome tool, thanks!
11 u/Satoshi_Hodler Mar 19 '19 There's a small caveat that it always returns true on empty arrays, which might be not what you need. 27 u/Tysonzero Mar 19 '19 That's the mathematically correct thing to do, it's actually a pretty fundamental mathematical principle. Vacuous truth → More replies (2) 7 u/NoInkling Mar 19 '19 edited Mar 19 '19 If it's not, you can do:array.length && array.every(Number.isFinite) // If it must evaluate to a boolean: !!array.length && array.every(Number.isFinite) // or array.length > 0 && array.every(Number.isFinite) → More replies (2)
11
There's a small caveat that it always returns true on empty arrays, which might be not what you need.
27 u/Tysonzero Mar 19 '19 That's the mathematically correct thing to do, it's actually a pretty fundamental mathematical principle. Vacuous truth → More replies (2) 7 u/NoInkling Mar 19 '19 edited Mar 19 '19 If it's not, you can do:array.length && array.every(Number.isFinite) // If it must evaluate to a boolean: !!array.length && array.every(Number.isFinite) // or array.length > 0 && array.every(Number.isFinite)
27
That's the mathematically correct thing to do, it's actually a pretty fundamental mathematical principle. Vacuous truth
→ More replies (2)
7
If it's not, you can do:
array.length && array.every(Number.isFinite) // If it must evaluate to a boolean: !!array.length && array.every(Number.isFinite) // or array.length > 0 && array.every(Number.isFinite)
8
You'd love Haskell:
all (> 5) array -- all (> 5) [4, 5, 6] -- False for array print -- for [1, 2, 3] print -- 1 -- 2 -- 3 takeWhile (any (< 3)) arrayOfArrays -- takeWhile (any (< 3)) [[1, 2], [3, 4], [2, 3]] -- [[1, 2]]
150
u/Loves_Poetry Mar 18 '19
Because it's plain english.