Scala IntList to Java

So I have this Scala function which returns a IntList

def drawLine(x0: Int, y0: Int, x1: Int, y1: Int): IntList = {
  val list : IntList = drawLineRec(x0, y0, x1, y1, Math.abs(x1-x0), -Math.abs(y1-y0), Math.abs(x1-x0)-Math.abs(y1-y0))
  println("nline: "+list)
  return list
}

I now want to use it over in my java file, where I call this function and get the return

CalcDraw.IntList drawLine = newDraw.drawLine(x0, y0, x1, y1);

Is there a way to run through this IntList in java, because I tried to use for or foreach, but without any luck.

The return call contains something like this:

Cons((20,20),Cons((21,21),Nil()))

EDIT with more info

So here is a simplified Scala code:

class CalcDraw { // ctrl + shift+ b to evaluate.

    sealed abstract class IntList;
    case class Nil() extends IntList;
    case class Cons(hd: (Int,Int), tl: IntList) extends IntList;

    def concat(list1: IntList, list2: IntList) : IntList = list1 match{
      //concatenates two lists
      case Nil() => list2
      case Cons(head,tail) => Cons(head,concat(tail,list2))
    }
    def concatIf(list1: IntList, list2: IntList, predicate: ()=>Boolean) : IntList = if(predicate()){
      concat(list1,list2)
    }else{
      return list1
    }

    /*
    *
    *  LINE DRAWING
    *
    */

    def next(x0: Int, x1: Int): Int = if(x0 < x1) x0 + 1 else x0 - 1

    def drawLineRec(x0: Int, y0: Int, x1: Int, y1: Int, dx: Int, dy: Int, err: Int): IntList = {
      if(!(x0 == x1 && y0 == y1))
      {
        if((2*err) >= dy)
        {
          if((2*err) <= dx)
          {
            // Change in both x and y0
            Cons((x0, y0), drawLineRec(next(x0, x1), next(y0, y1), x1, y1, dx, dy, err + dx + dy))
          }
          else
          {
            // Change only in x0
            Cons((x0, y0), drawLineRec(next(x0, x1), y0, x1, y1, dx, dy, err + dy))
          }
        }
        else if((2*err) <= dx) {
          // Change only in y0
          Cons((x0, y0), drawLineRec(x0, next(y0, y1), x1, y1, dx, dy, err + dx))
        } else{
          Nil() // This should not happen, so maybe this is an incorrect approach
        }
      } else{
        Cons((x0, y0), Nil())
      }
    }

    def drawLine(x0: Int, y0: Int, x1: Int, y1: Int): IntList = {
      val list : IntList = drawLineRec(x0, y0, x1, y1, Math.abs(x1-x0), -Math.abs(y1-y0), Math.abs(x1-x0)-Math.abs(y1-y0))
      println("nline: "+list)
      return list
    }

  }

the java part is just as simple as calling the function

CalcDraw.IntList drawLine = newDraw.drawLine(x0, y0, x1, y1);

Answer

You can do:

CalcDraw calcDraw = new CalcDraw();
CalcDraw.IntList l = calcDraw.drawLine(1, 2, 3, 4);
while (l instanceof CalcDraw.Cons) {
    CalcDraw.Cons cons = (CalcDraw.Cons) l;
    System.out.println(cons.hd());
    l = cons.tl();
}

In addition, please read Return in Scala, which shouldn’t be used here. You can drop the semicolons. And you probably want to define IntList as sealed trait. You can read more about it at Difference between using sealed trait and sealed abstract class as a base class

Leave a Reply

Your email address will not be published. Required fields are marked *