Skip to content

Instantly share code, notes, and snippets.

@philipschwarz
Created July 23, 2011 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philipschwarz/1100862 to your computer and use it in GitHub Desktop.
Save philipschwarz/1100862 to your computer and use it in GitHub Desktop.
Functional answer to Naresh Jain's challenge using Jedi
int calculateAveragePreviousPercentageComplete() { return reduceAndAverageActivitiesBy(PREVIOUS_PERCENTAGE_COMPLETE); }
int calculateAverageCurrentPercentageComplete() { return reduceAndAverageActivitiesBy(CURRENT_PERCENTAGE_COMPLETE); }
int calculateAverageProgressPercentage() { return reduceAndAverageActivitiesBy(PROGRESS_PERCENTAGE); }
int reduceAndAverageActivitiesBy( Functor2<Integer, StudentActivityByAlbum, Integer> functor){
return fold(0, activities, functor) / activities.size(); }
static final Functor2<Integer, StudentActivityByAlbum, Integer> PREVIOUS_PERCENTAGE_COMPLETE =
new Functor2<Integer, StudentActivityByAlbum, Integer>(){
@Override public Integer execute(Integer accumulator, StudentActivityByAlbum value) {
return accumulator + value.getPreviousPercentageCompleted(); }};
static final Functor2<Integer, StudentActivityByAlbum, Integer> CURRENT_PERCENTAGE_COMPLETE =
new Functor2<Integer, StudentActivityByAlbum, Integer>(){
@Override public Integer execute(Integer accumulator, StudentActivityByAlbum value) {
return accumulator + value.getPercentageCompleted(); }};
static final Functor2<Integer, StudentActivityByAlbum, Integer> PROGRESS_PERCENTAGE =
new Functor2<Integer, StudentActivityByAlbum, Integer>(){
@Override public Integer execute(Integer accumulator, StudentActivityByAlbum value) {
return accumulator + value.getProgressPercentage(); }};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment