range_operators icon indicating copy to clipboard operation
range_operators copied to clipboard

Additional array methods: subtract and intersection with array

Open hokster7 opened this issue 9 years ago • 1 comments

Thanks for putting this gem together, really useful! I have a peculiar use case working with DateTimes, and I'm glad to say that this gem seems to be working just fine.

It'd be interesting to write a .subtract method on Array with signature subtract(Array<Range>, Array<Range>):Array<Range>, where the result is the first array of ranges subtracted by each of the ranges in the second array. See https://en.wikipedia.org/wiki/Subtraction

It'd also be interesting to write an .intersection method on Array with signature intersection(Array<Range>, Array<Range>):Array<Range>, where the result is first array's of ranges' intersections with each of the ranges in the second array.

hokster7 avatar Jun 02 '16 13:06 hokster7

I've been looking into using this Gem for a project and figured out a way to perform an operation similar to intersection(Array<Range>, Array<Range>):Array<Range>.

Array<Range>.product(Array<Range>).map(&:intersection).rangify

for example

[2, (3..4)].product([(1..3)]).map(&:intersection).rangify => [2..3]

kevindjacobson avatar Jul 03 '18 18:07 kevindjacobson