330 likes | 425 Vues
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 ); }.
E N D
Retro Gaming with Lambdas Alexander BelokrylovPrograms and Community manager @gigabel
Common case for(Orange o:orangebox){ if(o.getColor()==GREEN) o.setColor(ORANGE); } http://www.californiaoranges.com/40lb-box-bushel-organic-valencia-oranges.html
JDK 8 Lambdas? In my Java?
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
Default methods interface Collection<T>{ ... defaultvoidforEach(Block<T> action){ for(T t:this) action.apply(t); } }
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
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));
Iterating with Lambdas Stream iterate() s.getAnimals().addAll(Stream .iterate(tail, lamb ->newSpriteView.Lamb(lamb)) .substream(1,8) .collect(Collectors.toList()));
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));
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)); }
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));}
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));}
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));}
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))}
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();
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())); }
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())); }
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
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.