Andre Q Andre Q - 15 days ago 9
Scala Question

Formatting output scala lists

Currently I can search for a value, say

SK5
and it will return all values higher than
SK5
. However the format it is returned in as shown below;


List((SK6,List(2, 7, 5, 9, 1, 9, 8, 4, 1, 7, 3, 7, 0, 8, 4, 5, 9, 2,
4, 4, 8, 7, 9, 2, 2, 7, 9, 1, 6, 9)), (SK4,List(2, 9, 5, 7, 0, 8, 6,
6, 7, 9, 0, 1, 3, 1, 6, 0, 0, 1, 3, 8, 5, 4, 0, 9, 7, 1, 4, 5, 2, 8)),
(SK8,List(2, 8, 8, 3, 1, 1, 0, 8, 5, 9, 0, 3, 1, 6, 8, 7, 9, 6, 7, 7,
0, 9, 5, 2, 5, 0, 2, 1, 8, 6)), (SK9,List(7, 1, 8, 8, 4, 4, 2, 2, 7,
4, 0, 6, 9, 5, 5, 4, 9, 1, 8, 6, 3, 4, 8, 2, 7, 9, 7, 2, 6, 6)),
(SK5,List(2, 6, 8, 0, 3, 5, 5, 2, 5, 9, 4, 5, 3, 5, 7, 8, 8, 2, 5, 9,
3, 8, 6, 7, 8, 7, 4, 1, 2, 3)))


What I want is SK6 - 9, SK4 - 8 etc etc

All bundled together, How would I split this up and only show the last number in the list? I thought I had already filtered this out however apparently not.
Below is my code.
Mapdata
is saved as
Map(String, List[Int])


//functionality to find the last tail element, the "Current" stock price
def findLast(list:List[Int]) = list.last


//8 - Show Stocks Higher Than (W) THIS ONE THIS ONE THIS ONE
def handleEight(): Boolean = {
mnuShowPointsForStockHigher(higherThan2)
true
}

//Returns a list value
def mnuShowPointsForStockHigher(stock: (String) => List[(String, List[Int])]) = {
print("Enter Stock > ")
val data = stock(readLine)
println(s"${data}")
//println(s"${data._1}: ${data._2.last}")
}

def higherThan2(stock: String): List[(String, List[Int])] = {
mapdata.toList.sortWith(_._2.last > _._2.last).takeWhile(row => row._2.last > mapdata.get(stock).map(findLast(_)).getOrElse(0))
}

Answer

If you are trying to get the last value in each list, Best option will be map + last. Sorry, don't need to use flatten. My bad .. Should be something line: list.map(x => x.last)