Date post: | 12-Aug-2015 |
Category: |
Software |
Upload: | alina-dolgikh |
View: | 2,410 times |
Download: | 2 times |
–James Gosling
“If����������� ������������������ I����������� ������������������ were����������� ������������������ to����������� ������������������ pick����������� ������������������ a����������� ������������������ language����������� ������������������ to����������� ������������������ use����������� ������������������ today����������� ������������������ other����������� ������������������ than����������� ������������������ Java,����������� ������������������ it����������� ������������������
would����������� ������������������ be����������� ������������������ Scala.”����������� ������������������
–James Iry, “A Brief, Incomplete, and Mostly Wrong History of Programming Languages”
“A����������� ������������������ drunken����������� ������������������ Martin����������� ������������������ Odersky����������� ������������������ sees����������� ������������������ a����������� ������������������ Reese's����������� ������������������ Peanut����������� ������������������ Butter����������� ������������������ Cup����������� ������������������ ad����������� ������������������ featuring����������� ������������������ somebody's����������� ������������������ peanut����������� ������������������ butter����������� ������������������ getting����������� ������������������ on����������� ������������������ somebody����������� ������������������ else's����������� ������������������
chocolate����������� ������������������ and����������� ������������������ has����������� ������������������ an����������� ������������������ idea.����������� ������������������ He����������� ������������������ creates����������� ������������������ Scala,����������� ������������������ a����������� ������������������ language����������� ������������������ that����������� ������������������ unifies����������� ������������������ constructs����������� ������������������ from����������� ������������������ both����������� ������������������ object����������� ������������������ oriented����������� ������������������ and����������� ������������������ functional����������� ������������������ languages.����������� ������������������ This����������� ������������������ pisses����������� ������������������ off����������� ������������������ both����������� ������������������ groups����������� ������������������ and����������� ������������������ each����������� ������������������ promptly����������� ������������������
declares����������� ������������������ jihad.”����������� ������������������
Lightweight syntax
object Main extends Application { val phonebook = Map( "Alice" -> "212-34-56", "Bob" -> "265-43-21" ) phonebook += ("Carl" -> "298-76-54") println(phonebook("Alice"))
for (i <- 1 to 10) { println(i) } }
Scala
Lightweight syntax
public class Person { private String firstName; private String lastName; String getFirstName() { return firstName; } void setFirstName(String firstName) { this.firstName = firstName; } String getLastName() { return lastName; } void setLastName(String lastName) { this.lastName = lastName; } int hashCode() { .... } boolean equals(Object o) { .... } }
Java
Lightweight syntax
boolean hasUpperCase = false; for (int i = 0; i < name.length(); ++i) { if (Character.isUpperCase(name.charAt(i))) { hasUpperCase = true; break; } }
Java
Lightweight syntax
Predicate<Character> isUpperCase = new Predicate<Character>() { public Character apply(Character ch) { return Character.isUpperCase(ch); } }; boolean hasUpperCase = Iterables.any(Lists.charactersOf(name), isUpperCase);
Java + Guava
Lightweight syntax
Predicate<Character> isUpperCase = (ch) -> Character.isUpperCase(ch);
boolean hasUpperCase = Iterables.any(Lists.charactersOf(name), isUpperCase);
Java 8 + Guava
Lightweight syntax
boolean hasUpperCase = name.chars().anyMatch((ch) -> Character.isUpperCase(ch));
Java 8
Lightweight syntax
boolean hasUpperCase = name.chars().anyMatch((ch) -> Character.isUpperCase(ch));
Java 8
Scala
val hasUpperCase = name.exists(_.isUpperCase)
LambdasJava 8
•Can only access final variables •Cannot mutate variables in outer scope
Scala
•Can access any variables •Can mutate any accessible variables
TraitsJava 8
•Can define default methods in interfaces (Java 9 allows private methods)
•“Diamond” inheritance is forbidden
Scala
•No restrictions on method implementation •Method invocation chain determined by inheritance order
Composableimport com.dull.lib
class Eggs { import org.cool.lib
def bacon { import org.other.cool.lib … } }
Pattern matchingabstract class ChatProtocol case class Message(userId:Int, text:String) case class Join(userId:Int, name:String) case class Leave(userId:Int)
command match { case Message(id, text) => println(s"${users(id)}: ${text}") case Join(id, name) => { users += (id, name) println(s"${name} joined") } case Leave(id) => { println(s"${users(id)} left") users -= id } }
–Alan Perlis
“Syntactic����������� ������������������ sugar����������� ������������������ causes����������� ������������������ cancer����������� ������������������ of����������� ������������������ the����������� ������������������ semi-colons.”����������� ������������������
Operator overloading abuse
val intermediateHandler = (:/(host, port) / target <<? params >:> identity)
https://github.com/dispatch/reboot
def root = jsonObj | jsonArray
def jsonObj = "{" ~> repsep(objEntry, ",") <~ "}" ^^ { case vals:List[_] => JSONObject(Map(vals:_*)) }
def jsonArray = "[" ~> repsep(value, ",") <~ "]" ^^ { case vals:List[_] => JSONArray(vals) }
def objEntry = stringVal ~ (":" ~> value) ^^ { case x ~ y => (x, y) }
https://github.com/scala/scala-parser-combinators
Unicode operators in ScalaZ: ≠ ∋ ⇏ ★ ∅
–Paul Graham
“you����������� ������������������ could����������� ������������������ get����������� ������������������ smarter����������� ������������������ programmers����������� ������������������ to����������� ������������������ work����������� ������������������ on����������� ������������������ a����������� ������������������ Python����������� ������������������ project����������� ������������������ than����������� ������������������ you����������� ������������������ could����������� ������������������ to����������� ������������������ work����������� ������������������
on����������� ������������������ a����������� ������������������ Java����������� ������������������ project.”����������� ������������������
Java ❤ Scala
• Scala Maven plugin - http://davidb.github.io/scala-maven-plugin/