Post on 12-Apr-2017
Scala-JS
About me
Evgeniy Safronov
I am from Kherson
Software engineer in RIFL Media LLC
I love to write in JS and talk about JS
contacts:
evgeniy.safronov@outlook.com
skype: lambda.omega1
github.com/javacodegeek
coffeescript, typescript, clojurescript etc.
List of languages that compile to JS
OMG
more than 300
https://github.com/jashkenas/coffeescript/wiki/List-of-languages-that-compile-to-JS
Curious javascript problems
javascript> ["10", "10", "10", "10"].map(parseInt)
[10, NaN, 2, 3]
wtfjs.com
Scala profile● started in 2001 at the École Polytechnique Fédérale de Lausanne by Martin
Odersky
● functional programming and object oriented programming
● very strong static type system
● was released in 2004 on JVM and .NET
● v2.0 followed in March 2006
www.scala-lang.org
Martin Oderskyinvented Scala
co-author Java generics
he wrote a compiler for Java
he wrote a compiler for Scala
www.coursera.org/course/progfun
Companies which choose scala
Elitism ScalaScala developers are expensive
High barrier to entry
The successful combination of functional and object-oriented paradigms
Wide range of applications
Functional programming with immutable variables, lambdas, closures, lists, and so on
Java codepublic class Person {
private final String firstName;
private final String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
Scala code
class Person(val firstName: String, val lastName: String)
Back to curious javascript problems
javascript> ["10", "10", "10", "10"].map(parseInt)
[10, NaN, 2, 3]
scala> List("10", "10", "10", "10").map(parseInt)
List(10, 10, 10, 10)
Scala.js developers
1207 commits 702 commits
Code examples: classes
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
fullName() {
return `${this.firstName} ${this.lastName}`;
}
}
ES6class Person(val firstName: String, val
lastName: String) {
def fullName(): String =
s"$firstName $lastName"
}
Scala.js
Code examples: collections
const personMap = new Map([
[10, new Person("Roger", "Moore")],
[20, new Person("James", "Bond")]
]);
const names = [];
for (const [key, person] of personMap) {
if (key > 15) {
names.push(`${key} = ${person.firstName}`);
}
}
ES6val personMap = Map(
10 -> new Person("Roger", "Moore"),
20 -> new Person("James", "Bond")
)
val names = for {
(key, person) <- personMap
if key > 15
} yield s"$key = ${person.firstName}"
Scala.js
Compiler output code
object Main extends js.JSApp{
def main() = {
var x = 0
while(x < 10) x += 3
println(x)
// 12
}
}
Scala.jsScalaJS.c.LMain$.prototype.main__V =
(function() {
var x = 0;
while ((x < 10)) {
x = ((x + 3) | 0)
};
ScalaJS.m.s_Predef$()
.println__O__V(x)
// 12
});
ES5
How to install?install sbt (scala build tool)
config plugin ”sbt-scalajs”
Rhino presents out of the box, but you can setup V8.
http://www.scala-js.org/tutorial/basic/
HelloApppackage helloapp.webapp
import scala.scalajs.js.JSApp
object HelloApp extends JSApp {
def main(): Unit = {
println("Hello world!")
}
}
Using the DOMAdding the DOM library to build.sbt
import org.scalajs.dom
import dom.document
def appendPar(targetNode: dom.Node, text: String): Unit = {
val parNode = document.createElement("p")
val textNode = document.createTextNode(text)
parNode.appendChild(textNode)
targetNode.appendChild(parNode)
}
Reacting on User Inputimport scala.scalajs.js.annotation.JSExport
@JSExport
def addClickedMessage(): Unit = {
appendPar(document.body, "You clicked the button!")
}
<button id=”click-me-button” type=”button” onClick=”tutorial.webapp.MyApp().
addClickedMessage()”>Click me!</button>
Using jQuery
Adding the DOM library to build.sbt
import org.scalajs.jquery.jQuery
jQuery("body").append("<p>[message]</p>")
Testingpackage tutorial.webapp
import utest._
import org.scalajs.jquery.jQuery
object MyTest extends TestSuite {
// Initialize App
HelloApp.setupUI()
def tests = TestSuite {
HelloWorld {
assert(jQuery("p:contains('Hello World')").length == 1)
}
}
}
github.com/lihaoyi/utest
Why you should try Scala.js
New paradigms
All power of Scala
Very good IDE support (IntelliJ Scala and Eclipse ScalaDE)
Scala community
Universal application
expansion of consciousness
Scala.js the official Scala to JavaScript compiler http://www.scala-js.org/
https://github.com/scala-js/scala-js
https://gitter.im/scala-js/scala-js
Github info:
● 3121 commits● 41 contributors ● issues 82/1064 open/close● 36 releases● 2098 stars ● 188 forks
Benchmarks
Where to use it?SPA
Enterprise application
Games много кода
?