1 / 33

Retro Gaming with Lambdas

Retro Gaming with Lambdas. Alexander Belokrylov Programs and Community manager @ gigabel. http://www.behance.net/gallery/Barbie/2143713. Common case. for ( Orange o : orangebox ) { if ( o . getColor () == GREEN ) o . setColor ( ORANGE ); }.

deva
Télécharger la présentation

Retro Gaming with Lambdas

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Retro Gaming with Lambdas Alexander BelokrylovPrograms and Community manager @gigabel

  2. http://www.behance.net/gallery/Barbie/2143713

  3. Common case for(Orange o:orangebox){ if(o.getColor()==GREEN) o.setColor(ORANGE); } http://www.californiaoranges.com/40lb-box-bushel-organic-valencia-oranges.html

  4. http://www.behance.net/gallery/Barbie/2143713

  5. JDK 8 Lambdas? In my Java?

  6. Common case with Lambdas orangebox.forEach(o → { if(o.getColor()==GREEN) o.setColor(ORANGE); }) http://www.californiaoranges.com/40lb-box-bushel-organic-valencia-oranges.html

  7. Collection.forEach()

  8. Default methods interface Collection<T>{ ... defaultvoidforEach(Block<T> action){ for(T t:this) action.apply(t); } }

  9. Up to you

  10. Small change – big impact

  11. Mary had a little Lambda Mary had a little lambda Whose fleece was white and snow And everywhere that Mary went Lambda was sure to go! https://github.com/steveonjava/MaryHadALittleLambda

  12. Iterating with Lambdas

  13. Iterating with Lambdas Stream generation Group cells =newGroup(IntStream .range(0, HORIZONTAL_CELLS) .mapToObj(i-> IntStream .range(0, VERTICAL_CELLS) .mapToObj(j ->{ // Logic goes here returnrect;})) .flatMap(s -> s) .toArray(Rectangle[]::new));

  14. Iterating with Lambdas

  15. Iterating with Lambdas Stream iterate() s.getAnimals().addAll(Stream .iterate(tail, lamb ->newSpriteView.Lamb(lamb)) .substream(1,8) .collect(Collectors.toList()));

  16. Iterating with Lambdas • Collection streaming • Collection.stream(); • Set of objects • Stream.of(bananas, oranges, apples); • Numeric range • IntStream.range(0, 100); • Itaratively • Stream.iterate(tail, lamb -> new Lamb(lamb));

  17. Filtering Stream with Lambdas

  18. Filtering Stream with Lambdas publicvoidvisit(Shepherd s){ s.getAnimals().stream().filter(a ->a.getNumber()%4==1) .forEach(a ->a.setColor(null)); s.getAnimals().stream().filter(a ->a.getNumber()%4==2) .forEach(a ->a.setColor(Color.YELLOW)); s.getAnimals().stream().filter(a ->a.getNumber()%4==3) .forEach(a ->a.setColor(Color.CYAN)); s.getAnimals().stream().filter(a ->a.getNumber()%4==0) .forEach(a ->a.setColor(Color.GREEN)); }

  19. Filtering Stream with Lambdas Predicate publicstatic Predicate<SpriteView>checkIfLambIs(Integer number){ return lamb ->lamb.getNumber()%4== number;} @Override publicvoidvisit(Shepherd s){ s.getAnimals().stream().filter(checkIfLambIs(1)) .forEach(a ->a.setColor(null)); s.getAnimals().stream().filter(checkIfLambIs(2)) .forEach(a ->a.setColor(Color.YELLOW)); s.getAnimals().stream().filter(checkIfLambIs(3)) .forEach(a ->a.setColor(Color.CYAN)); s.getAnimals().stream().filter(checkIfLambIs(0)) .forEach(a ->a.setColor(Color.GREEN));}

  20. Filtering Collections with Lambdas

  21. Filtering Collection with Lambdas Function static Function<Color, Predicate<SpriteView>>checkIfLambColorIs = color ->{Predicate<SpriteView>checkIfColorIs = lamb ->lamb.getColor()== color; returncheckIfColorIs;}; @Override publicvoidvisit(Shepherd s){ mealsServed.set(mealsServed.get()+ s.getAnimals() .filtered(checkIfLambColorIs.apply(todayEatableColor)) .size()); s.getAnimals() .removeIf(checkIfLambColorIs.apply(todayEatableColor));}

  22. Filtering Collectionswith Lambdas Function static Function<Color, Predicate<SpriteView>>checkIfLambColorIs = color ->{Predicate<SpriteView>checkIfColorIs = lamb ->lamb.getColor()== color; returncheckIfColorIs;}; @Override publicvoidvisit(Shepherd s){ mealsServed.set(mealsServed.get()+ s.getAnimals() .filtered(checkIfLambColorIs.apply(todayEatableColor)) .size()); s.getAnimals() .removeIf(checkIfLambColorIs.apply(todayEatableColor));}

  23. Filtering Collectionswith Lambdas Function @Override publicvoidvisit(Shepherd s){ Function<Color, Predicate<SpriteView>>checkIfLambColorIs= color -> lamb ->lamb.getColor()== color; mealsServed.set(mealsServed.get()+ s.getAnimals() .filtered(checkIfLambColorIs.apply(todayEatableColor)) .size()); s.getAnimals() .removeIf(checkIfLambColorIs.apply(todayEatableColor))}

  24. Filtering Collection with Lambdas • Removes all elements that match the predicate • Collection.removeIf(); • Filtering and replacement • Collection.replaceAll(); • Returns collection filtered by predicate • ObservableCollection.filtered();

  25. Mapping Stream with Lambda

  26. Mapping Stream with Lambda Single Map @Override publicvoidvisit(Shepherd s){ // single map: s.getAnimals() .setAll(s.getAnimals() .stream() .map(sv->new Eggs(sv.getFollowing())) .collect(Collectors.toList())); }

  27. Mapping Stream with Lambda Double Map @Override publicvoidvisit(Shepherd s){ // double map: s.getAnimals() .setAll(s.getAnimals() .stream() .map(SpriteView::getFollowing) .map(Eggs::new) .collect(Collectors.toList())); }

  28. Mary had a little Lambda • OpenSource project to demonstrate Lambda features • Visual representation of strams, filters, maps • Created by Stephen Chin, @steveonjava https://github.com/steveonjava/MaryHadALittleLambda

  29. Reading

  30. Reading

  31. The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract.It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

More Related