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. Write Android like Scala @petitviolet Fringe81 Co,. Ltd. 

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

    Ltd. • Scala: Ad server • Android (iOS): Ad SDK 
  3. What is the pain of Android? 

  4. 

  5. 

  6.  ON NOT

  7.  Nice point of Scala • first class function •

    `Awesome` data structures • Option, List, Either, Future, Try, …
  8.  Nice point of Scala • first class function •

    `Awesome` data structures • Option, List, Either, Future, Try, … • Functional Programming • Monad
  9. Monad is • `flatMap` in Scala • `>>=` in Haskell

    • “すごいH本” 
  10. petitviolet/Android-Monad 

  11. Maybe 

  12. 

  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
  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
  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
  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);
 }
 });
  17. Maybe • Null safe programming in Java • if (foo

    != null) • Maybe Monad • of, flatMap • Useful APIs • map, getOrElse, foreach, filter, … 
  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
  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
  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
  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
  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
  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
  24. 

  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 
  26. Android-Monad(0.5.0) • Maybe • ListM • State • Identity 

  27. We are hiring! fringe81.com/recruit