0

I am working on Scala to convert list of lists to list of customized object "Point"

class Point(val x: Int, val y: Int) {
   var cX: Int = x
   var cY: Int = y
  }  

Should I use Foreach or should I use Map or foreach in this case

def list_To_Point(_listOfPoints :List[List[String]]) : List[Point] = { 

    var elem = 
    lazy val _list:  List[Point] = _listOfPoints.map(p=> new Point(p[0],p[1])
      _list
  }   

I couldn't figure out where the problem exactly ?

DataT
  • 85
  • 1
  • 2
  • 7

2 Answers2

4
 def listToPoint(l:List[List[String]]):List[Point] = 
     l.collect({case x::y::Nil => new Point(x.toInt,y.toInt)})

But you really shouldn't use a List[String] to represent what is basically (Int,Int)

Marth
  • 23,920
  • 3
  • 60
  • 72
1

ugly as hell and untested but it should work (pls consider making your structures immutable) :

case class Point(x:Int,y:Int) 




object Point {


  def listToPoint(listOfPoints:List[List[String]]):List[Point] =
    listOfPoints.map(p => new Point(p(0).toInt,p(1).toInt))
}
Stefan Kunze
  • 741
  • 6
  • 15