scala - Access element of an Array and return a monad? -
if access index outside bounds of array
, arrayindexoutofboundsexception
, eg:
val = new array[string](3) a(4) java.lang.arrayindexoutofboundsexception: 4
is there method return monad instead (eg: option)? , why doesn't default collections apply method array support this?
you can use lift
:
a.lift(4) // none a.lift(2) // some(null)
array[t]
partialfunction[int, t]
, lift
creates function[int, option[t]]
index option of element type.
Comments
Post a Comment