12. Scala is Hard.
http://apocalisp.wordpress.com/2010/10/26/
type-level-programming
-in-scala-part-7-natural-transformation%C2%A0literals/
Sunday, December 16, 12
13. Scala is Hard.
object
Param
{
implicit
def
pToT[A[_],
B[_]](f:
(p:
Param[A])
=>
B[p.T]):
A~>B
=
new
(A
~>
B)
{
def
apply[s](a:
A[s]):
B[s]
=
{
val
v:
Param[A]
{
type
T
=
s}
=
new
Param[A]
{
type
T
=
s
def
in
=
a
}
f(v)
}
}
}
http://apocalisp.wordpress.com/2010/10/26/
type-level-programming
-in-scala-part-7-natural-transformation%C2%A0literals/
Sunday, December 16, 12
30. Another look at the Java code
public class MakeANoteActivity extends RoboActivity
implements View.OnClickListener
{
public static final String PHOTO_PATH = "picture_path";
public static final String PHOTO_ATTACHED = "photo_attached";
@Inject SessionManager sessionManager;
@Inject LocationHelper locationHelper;
@Inject PhotoHelper photoHelper;
@Inject Application context;
@Inject CalibrationHelper calibrationHelper;
@InjectView(R.id.attach_photo) Button attachPhoto;
@InjectView(R.id.note_text) EditText noteText;
@InjectView(R.id.save_button) Button save;
@InjectView(R.id.date) TextView dateText;
@InjectView(R.id.share) Button share;
@InjectResource(R.string.im_aircasting) String imAircasting;
@InjectResource(R.string.share_with) String shareWith;
// ...
}
Sunday, December 16, 12
31. What if... Scala?
public class MakeANoteActivity extends RoboActivity
implements View.OnClickListener
{
public static final String PHOTO_PATH = "picture_path";
public static final String PHOTO_ATTACHED = "photo_attached";
@Inject SessionManager sessionManager;
@Inject LocationHelper locationHelper;
@Inject PhotoHelper photoHelper;
@Inject Application context;
@Inject CalibrationHelper calibrationHelper;
@InjectView(R.id.attach_photo) Button attachPhoto;
@InjectView(R.id.note_text) EditText noteText;
@InjectView(R.id.save_button) Button save;
@InjectView(R.id.date) TextView dateText;
@InjectView(R.id.share) Button share;
@InjectResource(R.string.im_aircasting) String imAircasting;
@InjectResource(R.string.share_with) String shareWith;
// ...
}
Sunday, December 16, 12
32. What if... Scala?
class MakeANoteActivity extends ScalaActivity
with View.OnClickListener
with PhotoOperations with CalibrationOperations
{
val PhotoPath = "picture_path"
val PhotoAttached = "photo_attached"
lazy val sessionManager = SessionManager.get
lazy val locationManager = LocationManager.get
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share = findView(TR.share)
val imAircasting = findResource[String](R.string.im_aircasting)
val shareWith = findResource[String](R.string.share_with)
def toastConferenceName() = "KrakDroid".toast()
}
Sunday, December 16, 12
33. What if... Scala?
class MakeANoteActivity extends ScalaActivity
with View.OnClickListener
with PhotoOperations with CalibrationOperations
{
public is default. = "picture_path"
val PhotoPath
val PhotoAttached = "photo_attached"
lazy val sessionManager = SessionManager.get
lazy val locationManager = LocationManager.get
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share = findView(TR.share)
val username = findResource[String](R.string.my_username)
val friends = findResource[List[String]](R.string.friends)
def toastConferenceName() = "KrakDroid".toast()
}
Sunday, December 16, 12
34. What if... Scala?
class MakeANoteActivity extends ScalaActivity
with View.OnClickListener
with PhotoOperations with CalibrationOperations
{
val PhotoPath = "picture_path"
ScalaActivity gives us some
val PhotoAttached = "photo_attached"
magic methods
lazy val sessionManager = SessionManager.get
lazy val locationManager = LocationManager.get
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share = findView(TR.share)
val username = findResource[String](R.string.my_username)
val friends = findResource[List[String]](R.string.friends)
def toastConferenceName() = "KrakDroid".toast()
}
Sunday, December 16, 12
35. What if... Scala?
class MakeANoteActivity extends ScalaActivity
It’s so easy,
with View.OnClickListener YOU can implement it!
with PhotoOperations with CalibrationOperations
{
val PhotoPath = "picture_path"
ScalaActivity gives us some
val PhotoAttached = "photo_attached"
magic methods
lazy val sessionManager = SessionManager.get
lazy val locationManager = LocationManager.get
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share = findView(TR.share)
val username = findResource[String](R.string.my_username)
val friends = findResource[List[String]](R.string.friends)
def toastConferenceName() = "KrakDroid".toast()
}
Sunday, December 16, 12
36. What if... Scala?
class MakeANoteActivity extends ScalaActivity
It’s so easy,
with View.OnClickListener YOU can implement it!
with PhotoOperations with CalibrationOperations
{
val PhotoPath = "picture_path"
ScalaActivity gives us some
val PhotoAttached = "photo_attached"
magic methods
lazy val sessionManager = SessionManager.get
lazy val locationManager = LocationManager.get
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share = findView(TR.share)
val username = findResource[String](R.string.my_username)
val friends = findResource[List[String]](R.string.friends)
def toastConferenceName() = "KrakDroid".toast()
}
Sunday, December 16, 12
37. What if... Scala?
class MakeANoteActivity extends ScalaActivity
with View.OnClickListener
with PhotoOperations with CalibrationOperations
Instead of “...Helper”,
{ use Traits.
val PhotoPath = "picture_path"
val PhotoAttached = "photo_attached"
lazy val sessionManager = SessionManager.get
lazy val locationManager = LocationManager.get
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share = findView(TR.share)
val username = findResource[String](R.string.my_username)
val friends = findResource[List[String]](R.string.friends)
def toastConferenceName() = "KrakDroid".toast()
}
Sunday, December 16, 12
38. What if... Scala?
class MakeANoteActivity extends ScalaActivity
with View.OnClickListener
with PhotoOperations with CalibrationOperations
{
val PhotoPath = "picture_path"
val PhotoAttached = "photo_attached"
Type Inference
lazy val sessionManager = SessionManager.get
lazy val locationManager = LocationManager.get
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share = findView(TR.share)
val username = findResource[String](R.string.my_username)
val friends = findResource[List[String]](R.string.friends)
def toastConferenceName() = "KrakDroid".toast()
}
Sunday, December 16, 12
39. What if... Scala?
class MakeANoteActivity extends ScalaActivity
with View.OnClickListener
with PhotoOperations with CalibrationOperations
{
val PhotoPath = "picture_path"
val PhotoAttached = "photo_attached"
lazy val sessionManager = SessionManager.get
lazy val locationManager = LocationManager.get
Who’s missing here?
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share = findView(TR.share)
val username = findResource[String](R.string.my_username)
val friends = findResource[List[String]](R.string.friends)
def toastConferenceName() = "KrakDroid".toast()
}
Sunday, December 16, 12
40. What if... Scala?
class MakeANoteActivity extends ScalaActivity
with View.OnClickListener
with PhotoOperations with CalibrationOperations
{
val PhotoPath = "picture_path"
val PhotoAttached = "photo_attached"
lazy val sessionManager = SessionManager.get
lazy val locationManager = LocationManager.get
lazy initialization
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share = findView(TR.share)
val username = findResource[String](R.string.my_username)
val friends = findResource[List[String]](R.string.friends)
def toastConferenceName() = "KrakDroid".toast()
}
Sunday, December 16, 12
41. What if... Scala?
class MakeANoteActivity extends ScalaActivity
with View.OnClickListener
with PhotoOperations with CalibrationOperations
{
val PhotoPath = "picture_path"
val PhotoAttached = "photo_attached"
lazy val sessionManager = SessionManager.get
lazy val locationManager = LocationManager.get
Needs Context
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share = findView(TR.share)
val username = findResource[String](R.string.my_username)
val friends = findResource[List[String]](R.string.friends)
def toastConferenceName() = "KrakDroid".toast()
}
Sunday, December 16, 12
42. What if... Scala?
class MakeANoteActivity extends ScalaActivity
with View.OnClickListener
with PhotoOperations with CalibrationOperations
{
val PhotoPath = "picture_path"
val PhotoAttached = "photo_attached"
lazy val sessionManager = SessionManager.get
lazy val locationManager = LocationManager.get
What’s the type here?!?
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share = findView(TR.share)
val username = findResource[String](R.string.my_username)
val friends = findResource[List[String]](R.string.friends)
def toastConferenceName() = "KrakDroid".toast()
}
Sunday, December 16, 12
43. What if... Scala?
class MakeANoteActivity extends ScalaActivity
with View.OnClickListener
with PhotoOperations with CalibrationOperations
{
val PhotoPath = "picture_path"
val PhotoAttached = "photo_attached"
lazy val sessionManager = SessionManager.get
lazy val locationManager = LocationManager.get
What’s the type here?!?
Typed Resource - “a better R”
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share = findView(TR.share)
val username = findResource[String](R.string.my_username)
val friends = findResource[List[String]](R.string.friends)
def toastConferenceName() = "KrakDroid".toast()
}
Sunday, December 16, 12
44. What if... Scala?
class MakeANoteActivity extends ScalaActivity
with View.OnClickListener
with PhotoOperations with CalibrationOperations
{
val PhotoPath = "picture_path"
val PhotoAttached = "photo_attached"
lazy val sessionManager = SessionManager.get
lazy val locationManager = LocationManager.get
What’s the type here?!?
Typed Resource - “a better R”
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share = findView(TR.share)
XML Type changes... Type here changes!
val username = findResource[String](R.string.my_username)
val friends = findResource[List[String]](R.string.friends)
def toastConferenceName() = "KrakDroid".toast()
}
Sunday, December 16, 12
45. What if... Scala?
class MakeANoteActivity extends ScalaActivity
with View.OnClickListener
with PhotoOperations with CalibrationOperations
{
val PhotoPath = "picture_path"
val PhotoAttached = "photo_attached"
lazy val sessionManager = SessionManager.get
lazy val locationManager = LocationManager.get
What’s the type here?!?
Typed Resource - “a better R”
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share: Button = findView(TR.share)
You can be explicit though!
val username = findResource[String](R.string.my_username)
val friends = findResource[List[String]](R.string.friends)
def toastConferenceName() = "KrakDroid".toast()
}
Sunday, December 16, 12
46. What if... Scala?
class MakeANoteActivity extends ScalaActivity
with View.OnClickListener
with PhotoOperations with CalibrationOperations
{
val PhotoPath = "picture_path"
val PhotoAttached = "photo_attached"
lazy val sessionManager = SessionManager.get
lazy val locationManager = LocationManager.get
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share = Like @InjectResource, but
findView(TR.share) smarter
val username = findResource[String](R.string.my_username)
val friends = findResource[List[String]](R.string.friends)
def toastConferenceName() = "KrakDroid".toast()
}
Manifests vs. Type Erasure
Sunday, December 16, 12
47. What if... Scala?
class MakeANoteActivity extends ScalaActivity
with View.OnClickListener
with PhotoOperations with CalibrationOperations
{
val PhotoPath = "picture_path"
val PhotoAttached = "photo_attached"
lazy val sessionManager = SessionManager.get
lazy val locationManager = LocationManager.get
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share = findView(TR.share)
Does String have a toast()
val username = findResource[String](R.string.my_username) method?
val friends = findResource[List[String]](R.string.friends)
def toastConferenceName() = "KrakDroid".toast()
}
Implicit Conversion
Sunday, December 16, 12
51. Typed Resource = Better R
class MakeANoteActivity extends ScalaActivity
with View.OnClickListener
with PhotoOperations with CalibrationOperations
{
val PhotoPath = "picture_path"
val PhotoAttached = "photo_attached"
lazy val sessionManager = SessionManager.get
lazy val locationManager = LocationManager.get
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share = findView(TR.share)
val username = findResource[String](R.string.my_username)
val friends = findResource[List[String]](R.string.friends)
def toastConferenceName() = "KrakDroid".toast()
}
Sunday, December 16, 12
52. Typed Resource = Better R
class MakeANoteActivity extends ScalaActivity
with View.OnClickListener
with PhotoOperations with CalibrationOperations
{
val PhotoPath = "picture_path"
val PhotoAttached = "photo_attached"
lazy val sessionManager = SessionManager.get
lazy val locationManager = LocationManager.get
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share = findView(TR.share) Typed Resource
val username = findResource[String](R.string.my_username)
val friends = findResource[List[String]](R.string.friends)
def toastConferenceName() = "KrakDroid".toast()
}
Sunday, December 16, 12
53. Typed Resource = Better R
case class TypedResource[T](id: Int)
case class TypedLayout(id: Int)
object TR {
val columns_pager = TypedResource[ViewPager](R.id.columns_pager)
val workspaces = TypedResource[ExpandableListView](R.id.workspaces)
val subtask_txt = TypedResource[TextView](R.id.subtask_txt)
// ...
}
Sunday, December 16, 12
54. Typed Resource = Better R
case class TypedResource[T](id: Int)
case class TypedLayout(id: Int)
object TR {
val columns_pager = TypedResource[ViewPager](R.id.columns_pager)
val workspaces = TypedResource[ExpandableListView](R.id.workspaces)
val subtask_txt = TypedResource[TextView](R.id.subtask_txt)
// ...
}
Sunday, December 16, 12
55. Typed Resource = Better R
case class TypedResource[T](id: Int)
case class TypedLayout(id: Int) Wrappers
object TR {
val columns_pager = TypedResource[ViewPager](R.id.columns_pager)
val workspaces = TypedResource[ExpandableListView](R.id.workspaces)
val subtask_txt = TypedResource[TextView](R.id.subtask_txt)
// ...
}
Sunday, December 16, 12
56. Typed Resource = Better R
case class TypedResource[T](id: Int)
case class TypedLayout(id: Int)
object TR {
val columns_pager = TypedResource[ViewPager](R.id.columns_pager)
val workspaces = TypedResource[ExpandableListView](R.id.workspaces)
val subtask_txt = TypedResource[TextView](R.id.subtask_txt)
// ...
}
Sunday, December 16, 12
57. Typed Resource = Better R
case class TypedResource[T](id: Int)
case class TypedLayout(id: Int)
object TR {
val columns_pager = TypedResource[ViewPager](R.id.columns_pager)
val workspaces = TypedResource[ExpandableListView](R.id.workspaces)
val subtask_txt = TypedResource[TextView](R.id.subtask_txt)
// ...
}
Sunday, December 16, 12
58. TR in Action
class MakeANoteActivity extends ScalaActivity
with View.OnClickListener
with PhotoOperations with CalibrationOperations
{
val PhotoPath = "picture_path"
val PhotoAttached = "photo_attached"
lazy val sessionManager = SessionManager.get // magic here but
lazy val locationManager = LocationManager.get // we’ll see this later!
lazy val attachPhoto = findView(TR.attach_photo)
lazy val noteText = findView(TR.note_text)
lazy val save = findView(TR.save_button)
lazy val dateText = findView(TR.date)
lazy val share = findView(TR.share)
// magic here too, but we’ll implement this in a few slides!
val imAircasting = findResource[String](R.string.im_aircasting)
val shareWith = findResource[String](R.string.share_with)
// ...
}
Sunday, December 16, 12
75. An Implicit Context
class ScalaActivity
extends Activity
with ImplicitContext
Sunday, December 16, 12
76. An Implicit Context
class ScalaActivity
extends Activity
with ImplicitContext
trait ImplicitContext {
this: Activity =>
implicit val ctx = getContext
}
Sunday, December 16, 12
77. An Implicit Context
class ScalaActivity
extends Activity
with ImplicitContext
“I can be only mixed into
an Activity.”
trait ImplicitContext {
this: Activity =>
implicit val ctx = getContext
}
Sunday, December 16, 12
78. An Implicit Context
class ScalaActivity
extends Activity
with ImplicitContext
“I can be only mixed into
an Activity.”
trait ImplicitContext {
this: Activity =>
implicit val ctx = getContext
}
So I know this method!
Sunday, December 16, 12
81. Manifests vs. Type Erasure
List<Thing> t = marshaller.parseList("[]")
Java
Sunday, December 16, 12
82. Manifests vs. Type Erasure
Uhm... List<Object>...?
List<Thing> t = marshaller.parseList("[]")
Java
Sunday, December 16, 12
83. Manifests vs. Type Erasure
Uhm... List<Object>...?
List<Thing> t = marshaller.parseList("[]")
Java
List<Thing> t = marshaller.parseList("[]", Thing.class)
Sunday, December 16, 12
84. Manifests vs. Type Erasure
Uhm... List<Object>...?
List<Thing> t = marshaller.parseList("[]")
Java Oh! List<Thing>, sure!
List<Thing> t = marshaller.parseList("[]", Thing.class)
Sunday, December 16, 12
85. Manifests vs. Type Erasure
Uhm... List<Object>...?
List<Thing> t = marshaller.parseList("[]")
Java Oh! List<Thing>, sure!
List<Thing> t = marshaller.parseList("[]", Thing.class)
Scala val t: List[Thing] = marshaller.parseList("[]")
Sunday, December 16, 12
86. Manifests vs. Type Erasure
Uhm... List<Object>...?
List<Thing> t = marshaller.parseList("[]")
Java Oh! List<Thing>, sure!
List<Thing> t = marshaller.parseList("[]", Thing.class)
Scala val t: List[Thing] = marshaller.parseList("[]")
List[Thing]!
Sunday, December 16, 12
87. Manifests vs. Type Erasure
val shareWith = findResource[String](R.string.share_with)
Sunday, December 16, 12
88. Manifests vs. Type Erasure
val shareWith = findResource[String](R.string.share_with)
Sunday, December 16, 12
89. Manifests vs. Type Erasure
def findResource[T](id: Int)(implicit ctx: Context, m: Manifest[T]): T =
m.toString match {
case "java.lang.String" =>
ctx.getString(id).asInstanceOf[T]
case other =>
throw new UnableToResolveResource(ofType = other, id = id)
}
val shareWith = findResource[String](R.string.share_with)
Sunday, December 16, 12
90. Manifests vs. Type Erasure
def findResource[T](id: Int)(implicit ctx: Context, m: Manifest[T]): T =
m.toString match {
case "java.lang.String" =>
ctx.getString(id).asInstanceOf[T] implicit
case other =>
throw new UnableToResolveResource(ofType = other, id = id)
}
val shareWith = findResource[String](R.string.share_with)
Sunday, December 16, 12
91. Manifests vs. Type Erasure
def findResource[T](id: Int)(implicit ctx: Context, m: Manifest[T]): T =
m.toString match {
case "java.lang.String" =>
ctx.getString(id).asInstanceOf[T] implicit Manifest
case other =>
throw new UnableToResolveResource(ofType = other, id = id)
}
val shareWith = findResource[String](R.string.share_with)
Sunday, December 16, 12
92. Manifests vs. Type Erasure
def findResource[T](id: Int)(implicit ctx: Context, m: Manifest[T]): T =
m.toString match {
case "java.lang.String" =>
ctx.getString(id).asInstanceOf[T] the type signature
case other =>
throw new UnableToResolveResource(ofType = other, id = id)
}
val shareWith = findResource[String](R.string.share_with)
Sunday, December 16, 12
93. Manifests vs. Type Erasure
def findResource[T](id: Int)(implicit ctx: Context, m: Manifest[T]): T =
m.toString match {
case "java.lang.String" =>
ctx.getString(id).asInstanceOf[T] see the type... at runtime
case "java.util.ArrayList[java.lang.String]" =>
newArrayList(ctx.getString(id).asInstanceOf[T])
case other =>
throw new UnableToResolveResource(ofType = other, id = id)
}
val shareWith = findResource[String](R.string.share_with)
Sunday, December 16, 12
94. Manifests vs. Type Erasure
def findResource[T](id: Int)(implicit ctx: Context, m: Manifest[T]): T =
m.toString match {
case "java.lang.String" =>
ctx.getString(id).asInstanceOf[T]
case "java.util.ArrayList[java.lang.String]" =>
newArrayList(ctx.getString(id).asInstanceOf[T])
case "Int" =>
ctx.getString(id).toInt.asInstanceOf[T]
case other =>
throw new UnableToResolveResource(ofType = other, id = id)
}
val shareWith = findResource[String](R.string.share_with)
Sunday, December 16, 12
95. Manifests vs. Type Erasure
def findResource[T](id: Int)(implicit ctx: Context, m: Manifest[T]): T =
m.toString match {
case "java.lang.String" =>
ctx.getString(id).asInstanceOf[T]
case "java.util.ArrayList[java.lang.String]" =>
newArrayList(ctx.getString(id).asInstanceOf[T])
case "Int" =>
ctx.getString(id).toInt.asInstanceOf[T]
case other => java.lang.String has this...?
throw new UnableToResolveResource(ofType = other, id = id)
}
val shareWith = findResource[String](R.string.share_with)
Sunday, December 16, 12
124. public class Person {
private final String name;
private final String nick;
POJO
public Person(String name, String nick) {
this.name = name;
this.nick = nick;
}
public String getName() {
return name;
}
public String getNick() {
return nick;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person)) return false;
Person person = (Person) o;
if (name != null ? !name.equals(person.name) : person.name != null) return false;
if (nick != null ? !nick.equals(person.nick) : person.nick != null) return false;
return true;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (surname != null ? surname.hashCode() : 0);
return result;
}
}
Sunday, December 16, 12
125. public class Person {
private final String name;
private final String nick;
POJO
public Person(String name, String nick) {
this.name = name;
this.nick = nick;
}
public String getName() {
return name;
}
public String getNick() {
return nick;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person)) return false;
Person person = (Person) o;
if (name != null ? !name.equals(person.name) : person.name != null) return false;
if (nick != null ? !nick.equals(person.nick) : person.nick != null) return false;
return true;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (surname != null ? surname.hashCode() : 0);
return result;
}
}
Sunday, December 16, 12
126. POSO*
*Actually just a Case Class
case class Person(name: String, nick: String)
Sunday, December 16, 12
127. POSO*
*Actually just a Case Class
val name: String
case class Person(name: String, nick: String)
Sunday, December 16, 12
128. Case Classes
case class Person(name: String, nick: String)
Sunday, December 16, 12
129. Case Classes
case class Person(name: String, nick: String)
Sunday, December 16, 12
130. Case Classes
case class Person(name: String, nick: String)
val person = Person("Bruce Wayne", "Manbat") // whoops!
Sunday, December 16, 12
131. Case Classes
case class Person(name: String, nick: String)
val person = Person("Bruce Wayne", "Manbat") // whoops!
val fixed = person.copy(nick = "Batman")
Sunday, December 16, 12
132. Case Classes
case class Person(name: String, nick: String)
val person = Person("Bruce Wayne", "Manbat") // whoops!
val fixed = person.copy(nick = "Batman")
val self = Person(nick = "Ktoso" , name = "Konrad")
Sunday, December 16, 12
133. Case Classes
case class Person(name: String, nick: String)
val person = Person("Bruce Wayne", "Manbat") // whoops!
val fixed = person.copy(nick = "Batman")
val self = Person(nick = "Ktoso" , name = "Konrad")
val Person(name, hero) = person
val hello = ”Hello” + name + ” ” + surname
Sunday, December 16, 12
134. Case Classes
case class Person(name: String, nick: String)
val person = Person("Bruce Wayne", "Manbat") // whoops!
val fixed = person.copy(nick = "Batman")
val self = Person(nick = "Ktoso" , name = "Konrad")
val Person(name, hero) = person
val hello = ”Hello” + name + ” ” + surname
fixed.toString should equal (”Person(Bruce Wayne, Batman)”)
Still thinking assertThat().isEqualTo() is clean?
Sunday, December 16, 12
135. Given a list of People,
return all the Hero names.
Sunday, December 16, 12
136. Given a list of People,
return all the Hero names.
public List<String> heroNames(List<Person> allPersons) {
List<String> donePersons =
new ArrayList<String>(allPersons.size());
for (Person person : allPersons) {
if (person.isHero()) {
donePersons.add(person.getName());
}
}
return ImmutableList.copyOf(donePersons);
}
Sunday, December 16, 12
137. Java with Guava at their Best
Given a list of People,
return all the Hero names.
Sunday, December 16, 12
138. Java with Guava at their Best
Given a list of People,
return all the Hero names.
return FluentIterable
.from(people)
.filter(new Predicate<Person>() {
@Override
public boolean apply(Person person) {
return person != null && person.isHero();
}
})
.transform(new Function<Person, String>() {
@Override
public String apply(Person input) {
return input.getName();
}
})
.toImmutableList();
Sunday, December 16, 12
140. Java + Guava at their best
Given a list of People,
return all the Hero names.
Sunday, December 16, 12
141. Java + Guava at their best
Given a list of People,
return all the Hero names.
return FluentIterable
.from(people)
.filter(isSuperHero)
.transform(heroToName)
.toImmutableList();
Java, yet pretty slick :-)
I <3 Guava.
Sunday, December 16, 12
142. Scala Collections at Work
Given a list of People,
return all the Hero names.
Sunday, December 16, 12
143. Scala Collections at Work
Given a list of People,
return all the Hero names.
def heroNames(people: List[Person]) =
people filter(_.isHero) map { _.name}
Sunday, December 16, 12
175. def links =
• Scala Lang http://www.scala-lang.org/
• Scala Koans http://www.scalakoans.org
• Blog.Project13.pl - http://www.blog.project13.pl
• SBT Android Plugin - https://github.com/jberkel/android-plugin
• Kanbanery for Android - https://github.com/ktoso/kanbanery-tv
• Check pl.project13.scala.android.*
• Now OpenSource, in Scala, and Lunar Logic is helping to dev :-)
• New version soon! Pull and play with it!
• Scaloid https://github.com/pocorall/scaloid
• Though I don’t think their impl. is very clean some things are v. nice!
Sunday, December 16, 12
176. def links =
• Scala Lang http://www.scala-lang.org/
• Scala Koans http://www.scalakoans.org
• Blog.Project13.pl - http://www.blog.project13.pl
• SBT Android Plugin - https://github.com/jberkel/android-plugin
• Kanbanery for Android - https://github.com/ktoso/kanbanery-tv
• Check pl.project13.scala.android.*
• Now OpenSource, in Scala, and Lunar Logic is helping to dev :-)
• New version soon! Pull and play with it!
• Scaloid https://github.com/pocorall/scaloid
• Though I don’t think their impl. is very clean some things are v. nice!
Mailing lists rock!
Sunday, December 16, 12
177. Konrad Malawski / @ktosopl
GDG / PJUG / KSUG / SCKRK
KrakDroid 14.12.2012
Sunday, December 16, 12
178. I love feedback! <3 Konrad Malawski / @ktosopl
GDG / PJUG / KSUG / SCKRK
KrakDroid 14.12.2012
Sunday, December 16, 12