I have to check whether 2 is present in the range (2,5) .
How do I check that using Scala?
Ranges in Scala are created using the methods to
and until
:
2 to 5
would give you inclusive-inclusive range with numbers 2,3,4,5
2 until 5
would give you inclusive-exclusive range 2,3,4
Range
s provide the method contains
to check for element membership, so
a to b contains x
would check whether x
is in the range a to b
, in your case, it would be
2 to 5 contains 2
if you wanted inclusive-inclusive, or
(2 + 1) until 5 contains 2
if you wanted exclusive-exclusive etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With