Write Android like Scala

93bc8fb48f57c11e417dad9d26a2fb8a?s=47 petitviolet
November 17, 2015

Write Android like Scala

Presentation in http://roppongi-aar.connpass.com/event/21907/
Write Android like Scala.
Introduction of my library Android-Monad.
Enjoy Android programming with Monadic data types!
https://github.com/petitviolet/Android-Monad

93bc8fb48f57c11e417dad9d26a2fb8a?s=128

petitviolet

November 17, 2015
Tweet

Transcript

  1. 2.

    About me • Hiroki Komurasaki • @petitviolet • Fringe81 Co.,

    Ltd. • Scala: Ad server • Android (iOS): Ad SDK 
  2. 4.

    

  3. 5.

    

  4. 7.

     Nice point of Scala • first class function •

    `Awesome` data structures • Option, List, Either, Future, Try, …
  5. 8.

     Nice point of Scala • first class function •

    `Awesome` data structures • Option, List, Either, Future, Try, … • Functional Programming • Monad
  6. 11.
  7. 12.

    

  8. 13.

     
 Maybe<Integer> itemId = Maybe.of(x); // Maybe<Item> findItemById(int id);

    itemId.flatMap(this::findItemById)
 .map(Item::getCost) .map(integer -> integer + 5)
 .filter(integer -> integer % 2 == 0)
 .foreach(integer -> {
 Log.d(TAG, "result: " + integer);
 }); Maybe
  9. 14.

     
 Maybe<Integer> itemId = Maybe.of(x); // Maybe<Item> findItemById(int id);

    itemId.flatMap(this::findItemById)
 .map(Item::getCost) .map(integer -> integer + 5)
 .filter(integer -> integer % 2 == 0)
 .foreach(integer -> {
 Log.d(TAG, "result: " + integer);
 }); Maybe
  10. 15.

     
 Maybe<Integer> itemId = Maybe.of(x); // Maybe<Item> findItemById(int id);

    itemId.flatMap(this::findItemById)
 .map(Item::getCost) .map(integer -> integer + 5)
 .filter(integer -> integer % 2 == 0)
 .foreach(integer -> {
 Log.d(TAG, "result: " + integer);
 }); Maybe
  11. 16.

     maybeInt.flatMap(new Function.F1<Integer, Maybe<Item>>() {
 @Override
 public Maybe<Item> invoke(Integer integer)

    {
 return findItemById(integer);
 }
 }).map(new Function.F1<Item, Integer>() {
 @Override
 public Integer invoke(Item item) {
 return item.getCost();
 }
 }).map(new Function.F1<Integer, Integer>() {
 @Override
 public Integer invoke(Integer integer) {
 return integer + 5;
 }
 }).filter(new Function.F1<Integer, Boolean>() {
 @Override
 public Boolean invoke(Integer integer) {
 return integer % 2 == 0;
 }
 }).foreach(new Function.F<Integer>() {
 @Override
 public void invoke(Integer integer) {
 Log.d(TAG, "result: " + integer);
 }
 });
  12. 17.

    Maybe • Null safe programming in Java • if (foo

    != null) • Maybe Monad • of, flatMap • Useful APIs • map, getOrElse, foreach, filter, … 
  13. 18.

     ListM<Integer> categoryIds = ListM.of(1, 2, 3); ListM<Item> findItemsByCId(int id);

    int sumCost = categoryIds .flatMap(this::findItemsByCId)
 .filter(item -> item.getStatus() == 1)
 .map(Item::getCost)
 .foldLeft(0, (acc, i) -> acc + i); ListM
  14. 19.

     ListM<Integer> categoryIds = ListM.of(1, 2, 3); ListM<Item> findItemsByCId(int id);

    int sumCost = categoryIds .flatMap(this::findItemsByCId)
 .filter(item -> item.getStatus() == 1)
 .map(Item::getCost)
 .foldLeft(0, (acc, i) -> acc + i); ListM
  15. 20.

     ListM<Integer> categoryIds = ListM.of(1, 2, 3); ListM<Item> findItemsByCId(int id);

    int sumCost = categoryIds .flatMap(this::findItemsByCId)
 .filter(item -> item.getStatus() == 1)
 .map(Item::getCost)
 .foldLeft(0, (acc, i) -> acc + i); ListM
  16. 21.

     ListM<ViewGroup> someViewListM = ListM.of(…); int sumOfInput = someViewListM
 .map(l

    -> ((EditText) l.findViewById(R.id.input)))
 .map(editText -> editText.getText().toString())
 .filterNot(s -> TextUtils.isEmpty(s))
 .map(Integer::parseInt)
 .foldLeft(0, (acc, i) -> acc + i); ListM
  17. 22.

     ListM<ViewGroup> someViewListM = ListM.of(…); int sumOfInput = someViewListM
 .map(l

    -> ((EditText) l.findViewById(R.id.input)))
 .map(editText -> editText.getText().toString())
 .filterNot(s -> TextUtils.isEmpty(s))
 .map(Integer::parseInt)
 .foldLeft(0, (acc, i) -> acc + i); ListM
  18. 23.

     ListM<ViewGroup> someViewListM = ListM.of(…); int sumOfInput = someViewListM
 .map(l

    -> ((EditText) l.findViewById(R.id.input)))
 .map(editText -> editText.getText().toString())
 .filterNot(s -> TextUtils.isEmpty(s))
 .map(Integer::parseInt)
 .foldLeft(0, (acc, i) -> acc + i); ListM
  19. 24.

    

  20. 25.

    ListM • List Monad • of, unit, flatMap • for(int

    i = 0; i <= list.size(); i++) { // } • Useful Collection APIs • map, foreach, filter, filterNot, partition • foldLeft, foldRight, bindMaybe