Crystal: Add Number#positive? and #negative?

Created on 19 Mar 2020  路  4Comments  路  Source: crystal-lang/crystal

Summary

For example, when an array [-2, -1, 0, 1, 2] is given, I'd like to write

[-2, -1, 0, 1, 2].select(&.positive?)
[-2, -1, 0, 1, 2].select(&.negative?)

instead of

[-2, -1, 0, 1, 2].select { |i| i > 0 }
[-2, -1, 0, 1, 2].select { |i| i < 0 }

So I want to add Number#positive? and #negative? like Ruby.

feature discussion stdlib

Most helpful comment

I probably wasn't around when this was rejected, but I think it's harmless to add those two methods, and they might be a bit more descriptive than { |x| x > 0 } or &.>(0).

All 4 comments

Duplicate of #6552

IMO it's sad they were rejected.

I probably wasn't around when this was rejected, but I think it's harmless to add those two methods, and they might be a bit more descriptive than { |x| x > 0 } or &.>(0).

I'll argue against bloating stdlib API, as such use-case isn't standard, and app specific functionality should be treated differently. IMHO stdlib does provide enough mechanism to extend and shouldn't be bloated to cater all use-cases needs. Just my two cents

Was this page helpful?
0 / 5 - 0 ratings