Left truncate Scala function - list from integer -


i totally new in scala, me simple function - input parameter integer, function should return list of integers first entry input integer , , rest gotten omitting left digit 1 one. example,

if input 0, returns list(0), input =5678 returns list(5678,678,78,8).

def lefttrunc(input:int):list[int] 

thanks lot in advance

5678.tostring.tails.tolist.init.map(_.toint)  //> res0: list[int] = list(5678, 678, 78, 8) 

convert number string. tails want. except it's iterator, convert list, , has empty string @ end, use init return last element. they're strings, use map convert them int again

but i'm pretty sure instructor expecting numerically :)

here's numerical version, in case deliberately uncommented can work out how works

val n = 5678  val digits = n.tostring.size list.iterate(10, digits)(10*) map { n % _}   

edit: requested in comment, other way around uses inits instead of tails (and reverse requested ordering)

5678.tostring.inits.tolist.init.reverse.map(_.toint) //> res0: list[int] = list(5, 56, 567, 5678) 

and numerical 1 easier way around

list.iterate(n, digits)(_/10).reverse  

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -