Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Write Android like Scala

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

petitviolet

November 17, 2015
Tweet

More Decks by petitviolet

Other Decks in Programming

Transcript

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

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

  3. 

  4.  Nice point of Scala • first class function •

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

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

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

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

  19. 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