2

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);
Me NoLonely
  • 167
  • 11
  • Why do you want to calm this code from **Java** that `IntList` is jus a simplified collection to learn. In real programs you should be using the collections provided in the stdlib and if interacting with **Java** then convert them to **Java** collections using the `JavaConverters` – Luis Miguel Mejía Suárez Mar 10 '21 at 13:06

1 Answers1

3

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

Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
  • @Tomer_Shetah, is it possible for you to explain to me what exactly the while loop does? I've never really done it like this before :) – Me NoLonely Mar 11 '21 at 16:36
  • @MeNoLonely, Sure I can, but I think it is pretty straight forward. Can you please tell me what don't you understand? – Tomer Shetah Mar 11 '21 at 17:33
  • When you have a while with `instanceof` what does that do? and I think I understand the rest, `CalcDraw.Cons cons = (CalcDraw.Cons) lists` gets all the cons, and then I just get the `(x, y)` values from `hd: (int, int)` like you told me `cons.hd()` – Me NoLonely Mar 11 '21 at 18:12
  • 1
    @MeNoLonely it checks the type of `l`. It might be either `CalcDraw.Cons` or `CalcDraw.Nil`. If it is the first, it means we can extract the head. Once we got to `CalcDraw.Nil`, there are no more items, and we stop the loop. Does that make sense for you? – Tomer Shetah Mar 11 '21 at 20:06
  • @MeNoLonely, I've answered your other question. Is my answer helpful for you? – Tomer Shetah Mar 15 '21 at 05:22