.pop() equivalent in scala -
i have worked on python in python there function .pop() delete last value in list , return deleted value ex. x=[1,2,3,4] x.pop() return 4
i wondering there scala equivalent function?
if wish retrieve last value, can call x.last
. won't remove last element list, however, immutable. instead, can call x.init
obtain list consisting of elements in x
except last 1 - again, without changing x
. so:
val lastel = x.last val rest = x.init
will give last element (lastel
), list of bar last element (rest
), , still have original list (x
).
Comments
Post a Comment