ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
§±§à§é§Ö§Þ§å §á§à§â§Ñ
§Ú§ã§á§à§Ý§î§Ù§à§Ó§Ñ§ä§î Scala
§±§à§é§Ö§Þ§å §Ú§Þ§Ö§ß§ß§à Scala?
§±§à§é§Ö§Þ§å §Ú§Þ§Ö§ß§ß§à Scala?
§´§í §Ù§Ñ§Ò§í§Ý §á§â§à §ï§ä§à§ä §ã§Ý§Ñ§Û§Õ, §Ú§Þ§á§â§à§Ó§Ú§Ù§Ú§â§å§Û!
§±§â§à§ã§ä§à§Û POJO
public class User {
private String name;
private String email;
private DateTime registerDate;
}
§±§â§à§ã§ä§à§Û POJO
public User() {
}
public User(String name, String email,
DateTime registerDate) {
this.name = name;
this.email = email;
this.registerDate = registerDate;
}
§±§â§à§ã§ä§à§Û POJO
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public DateTime getRegisterDate() {
return registerDate;
}
public void setRegisterDate(DateTime registerDate) {
this.registerDate = registerDate;
}
§±§â§à§ã§ä§à§Û POJO
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
if (name != null ? !name.equals(user.name) : user.name != null) return false;
if (email != null ? !email.equals(user.email) : user.email != null) return false;
return registerDate != null ? registerDate.equals(user.registerDate) : user.registerDate == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (email != null ? email.hashCode() : 0);
result = 31 * result + (registerDate != null ? registerDate.hashCode() : 0);
return result;
}
§±§â§à§ã§ä§à§Û POJO
public class User {
private String name;
private String email;
private DateTime registerDate;
public User() {
}
public User(String name, String email,
DateTime registerDate) {
this.name = name;
this.email = email;
this.registerDate = registerDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public DateTime getRegisterDate() {
return registerDate;
}
public void setRegisterDate(DateTime registerDate) {
this.registerDate = registerDate;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
if (name != null ? !name.equals(user.name) : user.name != null) return false;
if (email != null ? !email.equals(user.email) : user.email != null) return false;
return registerDate != null ? registerDate.equals(user.registerDate) : user.registerDate == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (email != null ? email.hashCode() : 0);
result = 31 * result + (registerDate != null ? registerDate.hashCode() : 0);
return result;
}
}
§±§â§à§ã§ä§à§Û case class
case class User(name: String, email: String,
registerDate: DateTime = DateTime.now)
val obj1 = User(?Some name?, ?Some email?)
val obj2 = obj1.copy()
val obj3 = obj1.copy(name = ?New name?)
§³§à§á§à§ã§ä§Ñ§Ó§Ý§Ö§ß§Ú§Ö §ã §à§Ò§â§Ñ§Ù§è§à§Þ
public abstract class Homo
pubic class HomoHabilis extends Homo {
}
public class HomoHeidelbergensis extends Homo {
}
public class HomoSapiens extends Homo {
}
public void evolve(Homo homo) {
if (homo instanceof HomoHabilis) {
HomoHabilis h = (HomoHabilis) homo
//die
return;
}
if (homo instanceof HomoHeidelbergensis) {
//cast
//die
return;
}
if (homo instanceof HomoSapiens) {
//cast
//evolve
return;
}
}
§³§à§á§à§ã§ä§Ñ§Ó§Ý§Ö§ß§Ú§Ö §ã §à§Ò§â§Ñ§Ù§è§à§Þ
abstract class Homo
class HomoHabilis extends Homo
class HomoHeidelbergensis extends Homo
class HomoSapiens extends Homo
def evolve(homo: Homo) {
homo match {
case habilis: HomoHabilis => //die
case heidelbergensis: HomoHeidelbergensis => //die
case sapiens: HomoSapiens => //love scala ^__^
}
}
§¯§Ñ §Ó§Ü§å§ã§ß§à§Ö
? §Ý§ð§Ò§à§Ö §Ó§í§â§Ñ§Ø§Ö§ß§Ú§Ö §Ó§à§Ù§Ó§â§Ñ§ë§Ñ§Ö§ä §â§Ö§Ù§å§Ý§î§ä§Ñ§ä
? §Ô§Ö§ß§Ö§â§Ñ§ä§à§â§í §è§Ú§Ü§Ý§à§Ó + §â§Ö§Ù§å§Ý§î§ä§Ñ§ä §Ó §Ó§Ú§Õ§Ö §ã§á§Ú§ã§Ü§Ñ
? §á§Ñ§â§ã§Ö§â-§Ü§à§Þ§Ò§Ú§ß§Ñ§ä§à§â§í
? xml §ß§Ñ §å§â§à§Ó§ß§Ö §ã§Ú§ß§ä§Ñ§Ü§ã§Ú§ã§Ñ §ñ§Ù§í§Ü§Ñ
? null-safe (§Ù§Ñ §Ú§ã§á§à§Ý§î§Ù§à§Ó§Ñ§ß§Ú§Ö null §Ò§î§ð§ä §ß§à§Ô§à§Û §Ó §Ý§Ú§è§à)
? §Ý§Ö§Ô§Ü§à §ã§ä§â§à§Ú§ä§î DSL
? () §Ú ; §ß§Ö §ß§å§Ø§ß§í!
? §æ§å§ß§Ü§è§Ú§ð §Þ§à§Ø§ß§à §Ó§í§Ù§Ó§Ñ§ä§î §Ò§Ö§Ù §ä§à§é§Ü§Ú
? == §â§Ñ§Ò§à§ä§Ñ§Ö§ä §Ü§Ñ§Ü equals
? §à§Õ§Ú§ß §æ§Ñ§Û§Ý != §à§Õ§ß§à§Þ §Ü§Ý§Ñ§ã§ã
§¯§Ö§Þ§ß§à§Ô§à §Þ§Ñ§Ô§Ú§Ú
val xml = <root>
<first>
<action>
<name>Say</name>
<value>Hello</value>
</action>
<action>
<name>Don't say</name>
<value>Goodbye</value>
</action>
</first>
<first>
<action>
<name>Love</name>
<value>Scala</value>
</action>
<action>
<name>Use</name>
<value>FP</value>
</action>
</first>
</root>
object XmlParserApp {
case class MyAction(name: String, value: String)
def main(args: Array[String]): Unit = {
val result = for {firstEl <- xml  "first"
actionEl <- firstEl  "action"} yield {
MyAction(
name = (actionEl  "name").text,
value = (actionEl  "value").text
)
}
result.foreach(println(_))
}
}
§®§Ú§ß§å§ã§í
? §é§Ñ§ã§ä§î §Ò§Ú§Ò§Ý§Ú§à§ä§Ö§Ü §Ù§Ñ§Ò§Ñ§Ô§à§Ó§Ñ§ß§í
? §ß§Ö§ä §ã§à§Ó§Þ§Ö§ã§ä§Ú§Þ§à§ã§ä§Ú §Ò§Ñ§Û§ä§Ü§à§Õ§Ñ §Þ§Ö§Ø§Õ§å §Ó§Ö§â§ã§Ú§ñ§Þ§Ú §ñ§Ù§í§Ü§Ñ
? §é§Ñ§ã§ä§î §Ú§ã§ç§à§Õ§ß§Ú§Ü§à§Ó §ß§Ö§Ó§à§Ù§Þ§à§Ø§ß§à §é§Ú§ä§Ñ§ä§î. §³§à§Ó§ã§Ö§Þ.
? §ß§Ö§ñ§Ó§ß§í§Ö §á§â§Ö§à§Ò§â§Ñ§Ù§à§Ó§Ñ§ß§Ú§ñ(implicit)
? §Ü§Ñ§Ü-§Ò§ï §ã§à§Ó§Þ§Ö§ã§ä§Ú§Þ§à§ã§ä§î §ã Java
? §ã§Ó§à§ñ §â§Ö§æ§Ý§Ö§Ü§ã§Ú§ñ
? §Þ§Ö§Õ§Ý§Ö§ß§ß§í§Û §Ü§à§Þ§á§Ú§Ý§ñ§ä§à§â
? §å sbt §ã§Ó§à§Ú §ä§Ñ§â§Ñ§Ü§Ñ§ß§í
? Scala §á§Ý§Ñ§Ô§Ú§ß §Õ§Ý§ñ Idea §ä§å§á§Ú§ä §Ú §ä§à§â§Þ§à§Ù§Ú§ä
? ?§Õ§â§å§Ø§Ö§Ý§ð§Ò§ß§à§Ö" §Ü§à§Þ§î§ð§ß§Ú§ä§Ú
§³§á§Ñ§ã§Ú§Ò§à
§¢§à§â§Ú§ã §´§Ñ§ê§Ü§å§Ý§à§Ó
boris.tashkulov@dz.ru
@crashkin

More Related Content

[Expert Fridays] Java MeetUp - §¢§à§â§Ú§ã §´§Ñ§ê§Ü§å§Ý§à§Ó (Teamlead Enterprise): "§±§à§é§Ö§Þ§å §á§à§â§Ñ §Ú§ã§á§à§Ý§î§Ù§à§Ó§Ñ§ä§î Scala"

  • 3. §±§à§é§Ö§Þ§å §Ú§Þ§Ö§ß§ß§à Scala? §´§í §Ù§Ñ§Ò§í§Ý §á§â§à §ï§ä§à§ä §ã§Ý§Ñ§Û§Õ, §Ú§Þ§á§â§à§Ó§Ú§Ù§Ú§â§å§Û!
  • 4. §±§â§à§ã§ä§à§Û POJO public class User { private String name; private String email; private DateTime registerDate; }
  • 5. §±§â§à§ã§ä§à§Û POJO public User() { } public User(String name, String email, DateTime registerDate) { this.name = name; this.email = email; this.registerDate = registerDate; }
  • 6. §±§â§à§ã§ä§à§Û POJO public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public DateTime getRegisterDate() { return registerDate; } public void setRegisterDate(DateTime registerDate) { this.registerDate = registerDate; }
  • 7. §±§â§à§ã§ä§à§Û POJO @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (name != null ? !name.equals(user.name) : user.name != null) return false; if (email != null ? !email.equals(user.email) : user.email != null) return false; return registerDate != null ? registerDate.equals(user.registerDate) : user.registerDate == null; } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + (email != null ? email.hashCode() : 0); result = 31 * result + (registerDate != null ? registerDate.hashCode() : 0); return result; }
  • 8. §±§â§à§ã§ä§à§Û POJO public class User { private String name; private String email; private DateTime registerDate; public User() { } public User(String name, String email, DateTime registerDate) { this.name = name; this.email = email; this.registerDate = registerDate; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public DateTime getRegisterDate() { return registerDate; } public void setRegisterDate(DateTime registerDate) { this.registerDate = registerDate; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (name != null ? !name.equals(user.name) : user.name != null) return false; if (email != null ? !email.equals(user.email) : user.email != null) return false; return registerDate != null ? registerDate.equals(user.registerDate) : user.registerDate == null; } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + (email != null ? email.hashCode() : 0); result = 31 * result + (registerDate != null ? registerDate.hashCode() : 0); return result; } }
  • 9. §±§â§à§ã§ä§à§Û case class case class User(name: String, email: String, registerDate: DateTime = DateTime.now) val obj1 = User(?Some name?, ?Some email?) val obj2 = obj1.copy() val obj3 = obj1.copy(name = ?New name?)
  • 10. §³§à§á§à§ã§ä§Ñ§Ó§Ý§Ö§ß§Ú§Ö §ã §à§Ò§â§Ñ§Ù§è§à§Þ public abstract class Homo pubic class HomoHabilis extends Homo { } public class HomoHeidelbergensis extends Homo { } public class HomoSapiens extends Homo { } public void evolve(Homo homo) { if (homo instanceof HomoHabilis) { HomoHabilis h = (HomoHabilis) homo //die return; } if (homo instanceof HomoHeidelbergensis) { //cast //die return; } if (homo instanceof HomoSapiens) { //cast //evolve return; } }
  • 11. §³§à§á§à§ã§ä§Ñ§Ó§Ý§Ö§ß§Ú§Ö §ã §à§Ò§â§Ñ§Ù§è§à§Þ abstract class Homo class HomoHabilis extends Homo class HomoHeidelbergensis extends Homo class HomoSapiens extends Homo def evolve(homo: Homo) { homo match { case habilis: HomoHabilis => //die case heidelbergensis: HomoHeidelbergensis => //die case sapiens: HomoSapiens => //love scala ^__^ } }
  • 12. §¯§Ñ §Ó§Ü§å§ã§ß§à§Ö ? §Ý§ð§Ò§à§Ö §Ó§í§â§Ñ§Ø§Ö§ß§Ú§Ö §Ó§à§Ù§Ó§â§Ñ§ë§Ñ§Ö§ä §â§Ö§Ù§å§Ý§î§ä§Ñ§ä ? §Ô§Ö§ß§Ö§â§Ñ§ä§à§â§í §è§Ú§Ü§Ý§à§Ó + §â§Ö§Ù§å§Ý§î§ä§Ñ§ä §Ó §Ó§Ú§Õ§Ö §ã§á§Ú§ã§Ü§Ñ ? §á§Ñ§â§ã§Ö§â-§Ü§à§Þ§Ò§Ú§ß§Ñ§ä§à§â§í ? xml §ß§Ñ §å§â§à§Ó§ß§Ö §ã§Ú§ß§ä§Ñ§Ü§ã§Ú§ã§Ñ §ñ§Ù§í§Ü§Ñ ? null-safe (§Ù§Ñ §Ú§ã§á§à§Ý§î§Ù§à§Ó§Ñ§ß§Ú§Ö null §Ò§î§ð§ä §ß§à§Ô§à§Û §Ó §Ý§Ú§è§à) ? §Ý§Ö§Ô§Ü§à §ã§ä§â§à§Ú§ä§î DSL ? () §Ú ; §ß§Ö §ß§å§Ø§ß§í! ? §æ§å§ß§Ü§è§Ú§ð §Þ§à§Ø§ß§à §Ó§í§Ù§Ó§Ñ§ä§î §Ò§Ö§Ù §ä§à§é§Ü§Ú ? == §â§Ñ§Ò§à§ä§Ñ§Ö§ä §Ü§Ñ§Ü equals ? §à§Õ§Ú§ß §æ§Ñ§Û§Ý != §à§Õ§ß§à§Þ §Ü§Ý§Ñ§ã§ã
  • 13. §¯§Ö§Þ§ß§à§Ô§à §Þ§Ñ§Ô§Ú§Ú val xml = <root> <first> <action> <name>Say</name> <value>Hello</value> </action> <action> <name>Don't say</name> <value>Goodbye</value> </action> </first> <first> <action> <name>Love</name> <value>Scala</value> </action> <action> <name>Use</name> <value>FP</value> </action> </first> </root> object XmlParserApp { case class MyAction(name: String, value: String) def main(args: Array[String]): Unit = { val result = for {firstEl <- xml "first" actionEl <- firstEl "action"} yield { MyAction( name = (actionEl "name").text, value = (actionEl "value").text ) } result.foreach(println(_)) } }
  • 14. §®§Ú§ß§å§ã§í ? §é§Ñ§ã§ä§î §Ò§Ú§Ò§Ý§Ú§à§ä§Ö§Ü §Ù§Ñ§Ò§Ñ§Ô§à§Ó§Ñ§ß§í ? §ß§Ö§ä §ã§à§Ó§Þ§Ö§ã§ä§Ú§Þ§à§ã§ä§Ú §Ò§Ñ§Û§ä§Ü§à§Õ§Ñ §Þ§Ö§Ø§Õ§å §Ó§Ö§â§ã§Ú§ñ§Þ§Ú §ñ§Ù§í§Ü§Ñ ? §é§Ñ§ã§ä§î §Ú§ã§ç§à§Õ§ß§Ú§Ü§à§Ó §ß§Ö§Ó§à§Ù§Þ§à§Ø§ß§à §é§Ú§ä§Ñ§ä§î. §³§à§Ó§ã§Ö§Þ. ? §ß§Ö§ñ§Ó§ß§í§Ö §á§â§Ö§à§Ò§â§Ñ§Ù§à§Ó§Ñ§ß§Ú§ñ(implicit) ? §Ü§Ñ§Ü-§Ò§ï §ã§à§Ó§Þ§Ö§ã§ä§Ú§Þ§à§ã§ä§î §ã Java ? §ã§Ó§à§ñ §â§Ö§æ§Ý§Ö§Ü§ã§Ú§ñ ? §Þ§Ö§Õ§Ý§Ö§ß§ß§í§Û §Ü§à§Þ§á§Ú§Ý§ñ§ä§à§â ? §å sbt §ã§Ó§à§Ú §ä§Ñ§â§Ñ§Ü§Ñ§ß§í ? Scala §á§Ý§Ñ§Ô§Ú§ß §Õ§Ý§ñ Idea §ä§å§á§Ú§ä §Ú §ä§à§â§Þ§à§Ù§Ú§ä ? ?§Õ§â§å§Ø§Ö§Ý§ð§Ò§ß§à§Ö" §Ü§à§Þ§î§ð§ß§Ú§ä§Ú