intersect-range ( range1 range2 -- range3 )


Vocabulary
ranges.private

Inputs
range1an object
range2an object


Outputs
range3an object


Definition


:: intersect-range ( range1 range2 -- range3 )
range1 empty? range2 empty? or
[ empty-range ] [
range1 >forward-range< :> ( start1 stop1 step1 ) range2
>forward-range< :> ( start2 stop2 step2 ) step1 step2
gcd :> ( x g ) start1 start2 -
g /mod :> ( z y ) y zero? not
[ empty-range ] [
start1 x z step1 * * - :> b step1 step2 lcm
:> a start1 start2 [ b over - a rem + ] bi@ max
:> m stop1 stop2 [ dup b - a rem - ] bi@ min
:> n m n a <range>
] if
] if ;