The hasNext method can compare the position of the cursor against the length of the result. Since during iterations, hasNext is called only once per iteration, we can close the cursor when there is nothing more to fetch.

The method next is as simple, it will move the cursor to one place and then extract the right positions.

def next: (T, U, V) = {
c.moveToNext()
???
}

Here we have a new problem. There is no such method as c.get[T](0) (take the first element of the cursor, and cast it to T). To the contrary, there are some methods such as c.getString, c.getLong, c.getInt, etc.

This is the time to set up a constraint: T, U and V can only be of type that the cursor can extract. We can enfore this by requiring that there exist implicit objects which can extract the type T out the cursor. For that, we define the trait Iter and two implicit objects implementing it, in the top-level object:

Nice, but how to be able to include them now in the next function? Simply, we can use implicitly[Iter[T]] to retrieve the implicit object and call its apply method. Sorry, we cannot use the syntactic sugar of removing the word "apply" since the method implicitly does not take parameters and this would be ambiguous. Thus:

Now the compiler complains that there is no guarantee that there is an implicitly[Iter[T]]. How can we ensure that so that T is restrained? There is a funcitonality in scala: implicit parameters. If the compiler can resolve them, no need to specify them ! This means that the method toIterator[T, U, V] introducing the three type parameters requires the implicit objects to work, that is:

There is also an even better syntax for that in scala, syntactic sugar:

def toIterator[T: Iter, U: Iter, V: Iter]

What's next? Abstraction ! We did it for three parameters, but perhaps we want to have one, two or more ! The refactoring is left as an exercise for the reader. For the impatient reader, the solution is below:

Use of the javascript "in" keywordif(key in obj) has to be translated to if(!js.isUndefined(obj(key)))

for...infor(a in obj) has to be translated to:for(a <- js.Object.keys(obj))

String extraction: Instead of extracting chars withstring(index)
prefer the followingstring.substring(index, 1)
which returns a String instead of a Char. It is hard to convert from Char to String.

Zero-argument functions: Careful when you translate zero-arg functions to def. If you pass them as parameter, you need to add the modifier _ so that they are not evaluated. For example:

function loadSomething() { ... }
$("#button").change(loadSomething)

has to be translated to:

def loadSomething() { ... }
$("#button").change(loadSomething _)

setTimeout: You will have to translate setTimeout using another construct, with a lazy evaluated block. Thus, this:setTimeout(function() { ... }, 999)
becomes:js.timers.setTimeout(999){ ... }Do not put anonymous function inside the brackets like { () => ...} else they the function body will never be evaluated !

console.log: In order to use console.log directly in your code, import the following which provides a statically typed object

import org.scalajs.dom._
console.log("Whatever")

or alternatively, you can use the dynamic global variable:

import js.Dynamic.{global => g}
g.console.log("Whatever")

Call object's main method. Last but not least. If you define your methods in an object, especially some jQuery onLoad event, you need to call at least one method from the object for it to be initialized. Indeed, objects are lazily created.
For example, if your initializer object is:

If you use JQuery and observe $("something").value()*1, since value() returns a js.Dynamic, simply converting this to $("something").value().toInt will fail at run-time (the toInt method is not defined on object of type string). However, you can do the following:$("something").value().asInstanceOf[String].toInt

If you find the pattern 1*s , where s is a string which might be null, don't use s.toInt directly in scala. It fails if s is null, whereas 1*null == 0 in javascript. Instead, do the following:

Now, after running sbt inside mainprojectfolder I, you can launch the following new tasks:
- fastOptJS which compiles the new scala file above to javascript
- runWithJS which performs the fastOptJS above plus the run command (you can customize this according to your needs).

These two tasks both create the following files inside mainprojectfolder/js/target/scala-2.11 :
- jsproject-fastopt.js: This file needs to be included in the final html.
- jsproject-fastopt.js.map: This is the source map
- jsproject-jsdeps.js: This file needs to be included in the final html. It contains all the remaining dependencies.

You may be interested to copy the generated javascript files to another folder like mainprojectfolder/public/js. For that, add the following line in the build.sbt file: